Store the same data in 2 seperate tables from 1 controller


I have a form that creates a new Dealer and also has a username and password field to designate to each dealer. I wanted these values to also be placed into the users table from the Dealers controller.

Keep in mind that I am in the Dealers controller and not the users controller.
I created a before function and called it before the save action of the add() function

I should also point out that I had to specify in the controller the $uses variable. This is usually dynamic but since I am working with an extra model for the controller, this is required.

var $uses = array('Store', 'User');

 

function add(){

if($this->beforeSave()){
if ($this->Store->save($this->data)) {
                      //session flash
               }
        }

}

 

function beforeSave(){

$userTblState = $this->data['Store']['state_id'];
       //keep state_id in variable to send it to the User table
$userTblName = $this->data['Store']['name'];
       //keep Store_name in variable to send it to the User table
$userTblUsername = $this->data['Store']['username'];
        //keep Store_username in variable to send it to the User table

  $data = array(
'User' => array(
'id' => '' ,
'name' => $userTblName,
'username' => $userTblUsername,
'password' => 'e4967li7b149767ytu1f42055d168799a73f514',
'state_id' => $userTblState,
)
);
        //Array of the data that needs to be set in the User table
        // leaving id in array blank let's it auto increment
$this->User->save($data);
        // If you notice the add() function up top is saving to a table named "Stores" and the beforeSave() is saving to a table named "User"
}

 
created [2010-11-20 17:41:15]
modified [2010-11-20 17:41:15]