1

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

我有一个表格,它收集要为两个模型保存的信息......人员和查询。我相信数据发送正确,但它根本没有保存查询数据。它返回一个验证错误,但在 Inquiry 模型中没有设置验证,如果我从 People 控制器中删除 '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->Person->validationErrors 的值

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

1 回答 1

4

Model::saveAll()是or的包装saveManysaveAssociated

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

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

这意味着saveAssociated被调用。您显然想要做的是与关联()一起保存一个 的。如果你通读手册,你会看到这一段:PersonInquiry

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

因此,您显然需要先保存第Person一个关联,然后再保存所有关联。

于 2013-09-07T15:49:39.523 回答