问题描述:

am learning cakephp 3.0, in the form submit i got the form values as given below:

App\Model\Entity\User Object

(

[new] => 1

[accessible] => Array

(

[*] => 1

)

[properties] => Array

(/*this properties array contain my form values*/

[uName] => adfaf

[password] => $2y$10$z2XFHdQ1qlNuIuPQebQcAODjeKfWFezE2lRPyXtfO2Nj70lJQttzS

[fName] =>

[mName] =>

[lName] =>

[mStatus] => Married

[address] =>

[City] =>

[State] =>

[Mobile] =>

)

[dirty] => Array

(

[uName] => 1

[password] => 1

[fName] => 1

[mName] => 1

[lName] => 1

[mStatus] => 1

[address] => 1

[City] => 1

[State] => 1

[Mobile] => 1

)

[original] => Array

(

)

[virtual] => Array

(

)

[errors] => Array

(

)

[repository] => users

)

And in my controller i have written the code from cake book but i cann't understand what the following code is doing here.Kindly help me and give the clarifactions.

public function addEmployee() {

$users = TableRegistry::get('users');

$users = $users->newEntity($this->request->data());

}

if i put the users variable in var_dump means it gives the above object will be shown.And i have to store the values in different tables like users,employees,contact.Each table's have their foreign keys.Here the problem is i don't know how to save the values in appropriate tables.

网友答案:

first maybe you need read about Saving With Associations, now I can give you tips:

in you controller maybe you need charge the model o you database:

$this->loadModel('Articles');

after of this, maybe you need create entities and save the data you receive in variables:

public function add()
{
    $this->loadModel('Articles');
    $this->loadModel('Users');
    $article = $this->Articles->newEntity();
    $user = $this->Users->newEntity();

    //receive data
    $user->username = $this->request->data('uName');
    $user->password = $this->request->data('password');
    $user->fullname = $this->request->data('fName');

    //receive data
    $article->fullname = $this->request->data('City');
    $article->fullname = $this->request->data('State');
    $article->fullname = $this->request->data('Mobile');

    //save data
    $this->Travels->save($article);
    $this->Travels->save($user);

}
相关阅读:
Top