1

CakePHP の saveAll() に問題があります。誰かが光を当ててくれることを望んでいました。

個人と問い合わせの 2 つのモデルについて保存する情報を収集するフォームがあります。データは正しく送信されていると思いますが、照会データが保存されていません。検証エラーが返されますが、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 に答える