提问者:小点点

Cakephp保存相关模型数据创建带有一个字段的空白记录


我整个上午都在设法解决这个问题。从其他问题中尝试了一些东西,但它不是真的不适用于我的情况,就是不起作用。我有两张桌子:

用户=(id,名称,用户名,密码,角色,last_edit,语言)
french_translations=(id,french_clinical_recommendations,french_tradenames,include_drug,french_category,drug_id,user_id)

用户有许多french_translations和french_translations物品To User.

当用户添加或编辑法语翻译时,我希望它将用户id保存在法语翻译表中,即该记录的字段用户id,然后将药物的通用名称放在用户表的最后一个编辑字段中。现在,它在每个表中创建一条新记录。在users表中,除了id和last_编辑字段(将正确的药物名称放入该字段)外,所有内容均为空。FrimixPieldIt表有空白的记录,UsRyID与用户表中创建的空白表相同。

控制器:

    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)); ?>

共2个答案

匿名用户

这是一个有点晚的答案,但是您不应该使用视图来设置安全变量,如user\u id。相反,您可以在控制器中设置以下变量:

$this->request->data['User']['last_edit'] = $this->request->data['Drug']['generic']; 

匿名用户

我发现了,但是,我觉得有一种更好、更安全的方法。我使用的是安全组件,一个人必须登录才能看到任何一个站点,所以它可能足够安全。

我只是添加了一个用户。用户id的id输入,此时它可以找到适当的用户来存储最后的编辑。在控制器中,我有一个名为$userid的变量,它使用$userid=$this获取当前用户id-

查看:

<?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 ?>