我在使用CakePHP的saveAll()时遇到了问题,我希望有人能有所了解。
我有一个表格,可以收集要保存两个模型的信息......人和查询。我相信数据已正确发送,但它根本没有保存查询数据。它返回验证错误,但在查询模型中没有设置验证,如果我从人员控制器中删除"deep"=> true,它会很好地保存这些字段。
它发布的数据
Array
(
[Person] => Array
(
[first_name] => Test
[middle_name] =>
[last_name] => User
[gender] => M
[date_of_birth] => Array
(
[month] => 02
[day] => 07
[year] => 1994
)
[address] => 1234 Main St
[address_apt] =>
[address_city] => Somewhere
[address_state] => OH
[address_zip] => 304982
[address_country] => US
[phone_cell] => (555) 555-5555
[permission_to_text] => 1
[phone_home] => (555) 555-5556
[email_address] => test@user.com
[preferred_contact] => text_cell
[Inquiry] => Array
(
[admit_type] => FR
[admit_term] => FA2014
[parent_first_name] => Mom
[parent_last_name] => User
[parent_email] => mom@user.com
[hs_name] => Columbus Downtown High School
[hs_ceeb_id] => 365210
[hs_homeschooled] => 0
[hs_grad_year] => Array
(
[year] => 2014
)
[coll_name] =>
[coll_ceeb_id] =>
[coll_major] =>
[coll_year] =>
[admit_major] => 1
[admit_minor] => 4
)
[social_facebook] =>
)
)
$this->人员->验证的值发布后的错误
Array
(
[Inquiry] => Array
(
[hs_homeschooled] => Array
(
)
[coll_name] => Array
(
)
[coll_ceeb_id] => Array
(
)
[coll_major] => Array
(
)
[coll_year] => Array
(
)
)
)
型号 - 询价
<?php
class Inquiry extends AppModel {
public $belongsTo = array('Person');
}
控制器
<?php
class PeopleController extends AppController {
public $helpers = array('Html', 'Form', 'Country', 'State', 'Major', 'Minor', 'Term');
public function index() {
if ($this->request->is('post')) {
$this->Person->create();
if ($this->Person->saveAll($this->request->data, array('deep' => true))) {
print_r($this->request->data);
$this->Session->setFlash(__('Your post has been saved.'));
return $this->redirect(array('action' => 'index'));
}
print_r($errors = $this->Person->validationErrors);
$this->set('errors', $errors = $this->Person->validationErrors);
$this->Session->setFlash(__('Unable to add.'));
}
}
}
Model::saveAll()
是saveMany
或saveAssociated
的包装器。
由于您没有在数字索引数组中传递数据,即
array(
0 => array(...),
1 => array(...),
)
这意味着saveAssociated
被调用。您显然要做的是将新Person
与关联一起保存(Inquiry
)。如果你通读手册,你会得到这一段:
如果系统中尚不存在任何关联的模型记录 (例如,您要保存新用户及其相关配置文件 同时记录),您需要先保存主数据库,或者 父模型。
因此,您显然需要先保存Person
,然后再保存所有关联。