-1

我有两个模型,一个是,一个LoginUserdetail。我使用该关系将模型中的数据保存Login在两个模型中。hasone

请让我知道如何编辑它们。

我使用以下代码保存在我的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
            }
        }
    }
4

2 回答 2

0

你的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);
    }
   }
于 2011-09-08T07:35:37.097 回答
0

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

于 2011-09-08T08:23:46.850 回答