【问题标题】:Edit data b/w two models in cakephp在 cakephp 中编辑数据 b/w 两个模型
【发布时间】:2011-09-08 06:10:50
【问题描述】:

我有两个模型,一个是Login,另一个是Userdetail。我使用hasone 关系在两个模型中保存了模型Login 的数据。

请告诉我如何编辑它们。

我使用以下代码保存在我的add.ctp 文件中:

echo $form->create('Login', array('action'=>'add'));
    echo $form->input('first_name');
    echo $form->input('last_name');
    echo $form->input('email');
    echo $form->input('user_name');
    echo $form->input('password');
    echo $form->input('Userdetail.first_name');
    echo $form->input('Userdetail.last_name');
    echo $form->input('Userdetail.designation');
    echo $form->input('Userdetail.contact');
    echo $form->input('Userdetail.address');
    echo $form->end('Add');
and in controller i used : 
function add() 
    {
        if (!empty($this->data)) 
        {
            if ($this->Login->saveAll($this->data)) 
            {
                // User and Profile created successfully
                $this->Session->setFlash('Your post has been saved.');
                $this->redirect(array('action' => 'index'));
            } 
            else 
            {
                // Error creating user
            }
        }
    }

【问题讨论】:

    标签: cakephp models has-one


    【解决方案1】:

    如何编辑?与添加表单相同,登录为echo $form->input('id');,用户详细信息为echo $form->input('Userdetail.id');

    【讨论】:

    • 我这样做如下: echo $form->input('id', array('type' => 'hidden')); echo $form->input('Userdetail.user_id', array('type' => 'hidden'));但即使只有数据在“登录”模型中更新,而不是在第二个模型中,即“用户详细信息”模型
    • 是Userdetail.id,你在用saveAll吗?
    【解决方案2】:

    你的edit.ctp:

    <?php
    echo $form->create('Login');
    echo $form->input('id');
    echo $form->input('first_name');
    echo $form->input('last_name');
    echo $form->input('email');
    echo $form->input('user_name');
    echo $form->input('password');
    echo $form->input('Userdetail.id');//updated
    echo $form->input('Userdetail.first_name');
    echo $form->input('Userdetail.last_name');
    echo $form->input('Userdetail.designation');
    echo $form->input('Userdetail.contact');
    echo $form->input('Userdetail.address');
    echo $form->end('Submit');
    ?>
    

    您在控制器中的编辑操作:

    function edit($id = null)
    {
    $this->set('title_for_layout', __('Edit', true));
    
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid ', true), 'default', array('class' => 'error'));
            $this->redirect(array('action'=>'index'));
        }
        if (!empty($this->data)) {
            if ($this->Login->save($this->data)) {
                $this->Userdetail->create();//updated code
                $this->Userdetail->id = $this->data['Userdetail']['id'];//updated code
              if ($this->Userdetail->save($this->data['Userdetail'])) {
                $this->Session->setFlash(__('Data has been saved', true), 'default', array('class' => 'success'));
                $this->redirect(array('action'=>'index'));
               }
            } else {
                $this->Session->setFlash(__('Data could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Login->read(null, $id);
        }
       }
    

    【讨论】:

    • 请指导如何传递第二个模型的 ID?
    • 您好 Chetan 先生,我使用了您的代码,但出现以下错误:注意 (8):未定义属性:LoginsController::$Userdetail [APP\controllers\logins_controller.php,第 39 行] 致命错误:调用到第 39 行 C:\xampp\htdocs\cakeexample\app\controllers\logins_controller.php 中非对象的成员函数 save() 请解决谢谢
    • 检查我在控制器“$this->Userdetail->create();”中更新的代码,另一个补充是“var $uses = array('Userdetail');”将此代码放入您的控制器中。
    • 现在出现以下错误:警告(512):SQL 错误:1062:重复条目 '5' 键 'PRIMARY' [CORE\cake\libs\model\datasources\dbo_source.php,第 526 行] 查询:INSERT INTO userdetails (first_name, last_name, designation, contact, address, user_id) VALUES ('sam', 'thomas', 'TLllll67, 214748'3美国',5)
    • 检查我更新的代码,我包括 "$this->Userdetail->id = $this->data['Userdetail']['id'];"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-29
    • 2013-11-29
    • 1970-01-01
    相关资源
    最近更新 更多