CakeAP问题与saveAll()

时间:2013-09-07 14:54:43

标签: php mysql cakephp

我遇到了CakePHP的问题saveAll()我希望有人可以解释一下。

我有一个表格,收集两个模型保存的信息......人和查询。我相信数据正在正确发送,但它只是不保存查询数据。它返回了验证错误,但在查询模型中没有设置验证,如果我删除'deep'=>如果是People控制器,它会保存这些字段。

Data It Posts

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.'));
        }
  }
}

1 个答案:

答案 0 :(得分:3)

Model::saveAll()saveManysaveAssociated的{​​{3}}。

由于您没有在数字索引数组中传递数据,即

array(
    0 => array(...),
    1 => array(...),
)

表示调用了saveAssociated。您显然要做的是将 Person与关联(Inquiry)一起保存。如果你wrapper,你会看到这一段:

  

如果系统中尚未存在任何关联的模型记录   (例如,您要保存新用户及其相关个人资料   记录在同一时间),你需要先保存主要,或   父模型。

所以你显然需要先保存Person然后再保存所有关联。