2

我整个早上都在试图解决这个问题。从其他问题中尝试了一些东西,但它要么并不真正适用于我的情况,要么不起作用。我有两张桌子:

users = (id,name,username,password,roles,last_edit,language)
french_translations = (id, french_clinical_recommendations, french_tradenames, include_drug, french_category, drug_id, user_id)

用户有许多 french_translations 和 french_translations 属于用户。

当用户添加或编辑 french_translation 时,我希望它将用户 id 保存在 french_translations 表中,即该记录的字段 user_id,然后将药物的通用名称放在用户表的 last_edit 字段中。现在它在每个表中创建一个新记录。在 users 表中,除了 id 和 last_edit 字段(将正确的药物名称放在字段中)之外,所有内容都是空白的。并且 french_translations 表有一条空白记录,其中 user_id 与 users 表中创建的空白记录相同。

控制器:

    function add($id = null) {
    $userid = $session->read('Auth.User.id');
    $drug = $this->FrenchTranslation->Drug->read(
      array(
          'Drug.id','Drug.generic','Drug.ahl','Drug.aap','Drug.rid','Drug.oral','Drug.mw','Drug.clinical_recommendations',
          'Drug.category','Drug.lrc'
      ),
      $id
    );
    $this->set('user',$userid);
    $this->set('drug',$drug);

    if (!empty($this->data)) {
      $french_translation['FrenchTranslation']['id'] = $this->Session->read('id');
            $this->FrenchTranslation->create();
            if ($this->FrenchTranslation->save($this->data)) {
                $this->Session->setFlash(__('The french translation has been saved', true));
                $this->redirect(array('controller'=>'drugs','action' => 'index'));
            } else {
                $this->Session->setFlash(__('The french translation could not be saved. Please, try again.', true));
            }
        }
        $drugs = $this->FrenchTranslation->Drug->find('list');
        $this->set(compact('drugs'));
    }

    function edit($id = null) {
    //$this->FrenchTranslation->id = $id;
    $userid = $this->Auth->user('id');
    $username = $this->Auth->user('name');
  //$this->FrenchTranslation->user_id = $id;
    $drug = $this->FrenchTranslation->Drug->read(
      array(
          'Drug.id','Drug.generic','Drug.ahl','Drug.aap','Drug.rid','Drug.oral','Drug.mw','Drug.clinical_recommendations',
          'Drug.category','Drug.lrc'
      ),
      $id
    );

    $this->set('drug',$drug);
    $this->set('user',$userid);
    $this->set('username',$username);

        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid french translation', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->FrenchTranslation->saveAll($this->data)) {
                $this->Session->setFlash(__('The french translation has been saved', true));
                $this->redirect(array('controller'=>'drugs','action' => 'index'));
            } else {
                $this->Session->setFlash(__('The french translation could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->FrenchTranslation->read(null, $id);
        }
        $drugs = $this->FrenchTranslation->Drug->find('list');
        $this->set(compact('drugs'));
    }

    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for french translation', true));
            $this->redirect(array('action'=>'index'));
        }
        if ($this->FrenchTranslation->delete($id)) {
            $this->Session->setFlash(__('French translation deleted', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->Session->setFlash(__('French translation was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }

编辑视图:

    <?php echo $this->Form->input('User.last_edit',array('type'=>'hidden','value'=>$drug['Drug']['generic'])); ?>    
    <?php echo $this->Form->input('user_id', array('type'=>'hidden','value'=>$user)); ?>
4

2 回答 2

1

这是一个迟到的答案,但你不应该使用视图来设置安全变量,如user_id. 相反,您可以在控制器中设置这些变量:

$this->request->data['User']['last_edit'] = $this->request->data['Drug']['generic']; 
于 2014-04-13T04:10:08.153 回答
0

我想通了,但是,我觉得有一种更好,更安全的方法。我正在使用安全组件,并且一个人必须登录才能查看任何站点,因此它可能足够安全。

我只是为用户 ID 添加了一个 user.id 输入,此时它可以找到合适的用户来存储 last_edit。在控制器中,我有一个名为 $userid 的变量,它使用它获取当前用户 ID,并使用我在药物 user_id 字段的输入中使用相同的用户 ID 值$userid = $this->Auth->user('id');传递给视图。$ths->set('user',$userid);

看法:

<?php echo $this->Form->input('User.id',array('type'=>'hidden','value'=>$user));//The user ID last edit will be stored in ?>
<?php echo $this->Form->input('User.last_edit',array('type'=>'hidden','value'=>$drug['Drug']['generic']));//The drug edited last by the specified user ?>
于 2012-05-07T13:35:50.640 回答