Pete
Pete

Reputation: 465

CakePHP Issue with saveAll()

I'm having a problem with CakePHP's saveAll() I was hoping someone could shed some light on.

I have a form which collects information to be saved for two models... Person and Inquiry. I believe the data are being sent correctly, but it's simply not saving the Inquiry data. It's returning a validation error, but there is no validation set up in the Inquiry model, and if I remove 'deep' => true from the People controller, it saves those fields fine.

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] => [email protected]
            [preferred_contact] => text_cell
            [Inquiry] => Array
                (
                    [admit_type] => FR
                    [admit_term] => FA2014
                    [parent_first_name] => Mom
                    [parent_last_name] => User
                    [parent_email] => [email protected]
                    [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] => 
        )

)

Value of $this->Person->validationErrors after Posting

Array
(
    [Inquiry] => Array
        (
            [hs_homeschooled] => Array
                (
                )

            [coll_name] => Array
                (
                )

            [coll_ceeb_id] => Array
                (
                )

            [coll_major] => Array
                (
                )

            [coll_year] => Array
                (
                )

        )

)

Model - Inquiry

<?php
class Inquiry extends AppModel {
    public $belongsTo = array('Person');
}

Controller

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

Upvotes: 1

Views: 4579

Answers (1)

user221931
user221931

Reputation: 1852

Model::saveAll() is a wrapper of saveMany or saveAssociated.

Since you're not passing data in a numerical indexed array, i.e.

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

it means saveAssociated is called. What you're apparently trying to do is to save a new Person together with the associations (Inquiry). If you read through the manual you'll come to this paragraph:

If neither of the associated model records exists in the system yet (for example, you want to save a new User and their related Profile records at the same time), you’ll need to first save the primary, or parent model.

So you obviously need to save the Person first and then all associations.

Upvotes: 4

Related Questions