Sunday, June 23, 2013

Problem related to save multiple models in one controller

Hi,

I am a student who is participating GSOC 2013. I am doing project with cakephp. I want to save two models in same controller but I am stucked in the middle of that. Could you please help me to solve that problem.

I have two tables

1. Task- > id, name, description, cost
2. Connect-> id, task_id,connect_id  (here task_id is foriegn key)

Code:    

//Task.php

<?php

/**
 *
 */
class Task extends AppModel {
   
    var $name='Task';
    var $hasMany = array('Connect');
}

?>

//Connect.php

<?php

/**
 *
 */
class Connect extends AppModel {
   
    var $name='Connect';
    var $belongsTo =array( 'Task');
}

?>
   
//TasksController.php

<?php
class TasksController extends AppController {

    public $name = 'Tasks';
    var $uses = array('Connect','Task');
    function creat_task() {
        //$this -> loadModel('Connect');
       
        if (!empty($this -> request -> data)) {
            $task = $this -> Task -> save($this -> request -> data);
           
            $this -> request -> data['Connect']['task_id'] = $this -> Task -> id;
            $this -> Session -> setFlash($this -> Task -> id);
            $this -> Task -> Connect -> save($this -> request -> data);
            //$id = $this -> Task -> Connect -> getLastInsertId();
            $this -> redirect(array('action' => 'creat_task'));
            // debug($this->request->data);

        }

    }


  //creat_task.ctp


<?php
echo $this->Form->create('task');
?>

<hr/>
<h1>Task</h1>
<hr/>

<?php
echo $this->Form->input('Task.name', array('label' => "Name"));
echo $this->Form->input('Task.description', array('label' => "Description"));
echo $this->Form->input('Task.cost', array('label' => "Cost"));
?>

<hr/>
<h1>Connected Task</h1>
<hr/>

echo $this->Form->input('Connect.connect_id', array('label' => "Cost"));

<?php echo $this->Form->end('Submit'); ?> 
  

==========================================================================================

When  I try to save it gives following error


Error: Call to a member function save() on a non-object


in line "     $this -> Task -> Connect -> save($this -> request -> data);  "
 

What is the reason for this ?

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: