Cake php 3.0.1 在orm中保存数据
Cake php 3.0.1 saving data in orm
我正在学习 cakephp 3.0,在表单提交中我得到了如下所示的表单值:
App\Model\Entity\User Object
(
[new] => 1
[accessible] => Array
(
[*] => 1
)
[properties] => Array
(/*this properties array contain my form values*/
[uName] => adfaf
[password] => y$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());
}
如果我将 users 变量放在 var_dump 中意味着它给出上面的对象将是 shown.And 我必须将值存储在不同的 table 中,例如用户,员工,contact.Each table 有他们的外国 keys.Here 问题是我不知道如何在适当的 tables 中保存值。
首先您可能需要阅读 Saving With Associations,现在我可以给您提示:
在您的控制器中,您可能需要向数据库中的模型收费:
$this->loadModel('Articles');
在此之后,您可能需要创建实体并将收到的数据保存在变量中:
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);
}
我正在学习 cakephp 3.0,在表单提交中我得到了如下所示的表单值:
App\Model\Entity\User Object
(
[new] => 1
[accessible] => Array
(
[*] => 1
)
[properties] => Array
(/*this properties array contain my form values*/
[uName] => adfaf
[password] => y$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());
}
如果我将 users 变量放在 var_dump 中意味着它给出上面的对象将是 shown.And 我必须将值存储在不同的 table 中,例如用户,员工,contact.Each table 有他们的外国 keys.Here 问题是我不知道如何在适当的 tables 中保存值。
首先您可能需要阅读 Saving With Associations,现在我可以给您提示:
在您的控制器中,您可能需要向数据库中的模型收费:
$this->loadModel('Articles');
在此之后,您可能需要创建实体并将收到的数据保存在变量中:
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);
}