-1

I have two models, one is Login and the other is Userdetail. I saved data from model Login in both models using the hasone relationship.

Please let me know how it would be possible to edit them.

I used the following code for save in my add.ctp file:

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
            }
        }
    }
makes
  • 6,438
  • 3
  • 40
  • 58
Mehak
  • 11
  • 4

2 Answers2

0

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

Your edit action in controller:

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);
    }
   }
chetanspeed511987
  • 1,995
  • 2
  • 22
  • 34
  • Plz guide how to pass 2nd Model's Id? – Mehak Sep 08 '11 at 08:26
  • Hello Mr. Chetan I used your code But got following error : Notice (8): Undefined property: LoginsController::$Userdetail [APP\controllers\logins_controller.php, line 39] Fatal error: Call to a member function save() on a non-object in C:\xampp\htdocs\cakeexample\app\controllers\logins_controller.php on line 39 Please resolve Thanks – Mehak Sep 08 '11 at 10:03
  • check my updated code in controller "$this->Userdetail->create();"and another addition is that "var $uses = array('Userdetail');" put this code in ur controller. – chetanspeed511987 Sep 08 '11 at 10:29
  • Now below error appeares:Warning (512): SQL Error: 1062: Duplicate entry '5' for key 'PRIMARY' [CORE\cake\libs\model\datasources\dbo_source.php, line 526] Query: INSERT INTO `userdetails` (`first_name`, `last_name`, `designation`, `contact`, `address`, `user_id`) VALUES ('sam', 'thomas', 'TLlllll', 2147483647, 'US', 5) – Mehak Sep 08 '11 at 11:15
  • check my updated code i include "$this->Userdetail->id = $this->data['Userdetail']['id'];" – chetanspeed511987 Sep 08 '11 at 11:58
0

how to edit? Same as the add form, with an entry for echo $form->input('id'); for Login and echo $form->input('Userdetail.id'); for Userdetail

Anh Pham
  • 5,431
  • 3
  • 23
  • 27
  • I did this as follows : echo $form->input('id', array('type' => 'hidden')); echo $form->input('Userdetail.user_id', array('type' => 'hidden')); But even Only data is updating in "Login" Model not in 2nd i.e. "Userdetail" Model – Mehak Sep 08 '11 at 08:29
  • it is Userdetail.id, and are you using saveAll? – Anh Pham Sep 08 '11 at 08:31