So, I have a project with the following relationships:
In Listing.php
public $belongsTo = array(
'User' => array(
'foreignKey' => 'user_id'));
In User.php
public $hasMany = array(
'Referral' => array(
'foreignKey' => 'user_id',
'dependent' => true),
'Listing' => array(
'foreignKey' => 'user_id',
'dependent' => true),
'Appointment' => array(
'foreignKey' => 'user_id',
'dependent' => true
));
Basically, when I create a new Listing, I would also like to create a Referral and an Appointment. I have been naming my form fields like so:
name="data[User][Referral][name]", and I also tried name="data[Referral][name]"
and then tried using:
if ($this->request->data['User']['Referral']) {
$this->request->data['User']['id'] = $this->Auth->user('id');
}
$this->request->data['Listing']['user_id'] = $this->Auth->user('id');
debug($this->request->data);
if($this->Listing->SaveAssociated($this->request->data, array('deep' => true))) {
In the case of the former field naming convention, everything fails, and my output looks like:
EDIT: Everything was failing due to a validation error. On success, I get the same result as with the latter naming convention - the fields are created (N of them where N is the number of fields passed in the model, ie 2 for appointment, 4 for Referral) and all their values are blank.
array(
'Listing' => array(
'sale_type' => '',
'property_type' => '',
'referer' => '',
'price' => '',
'access_method' => '',
'bedrooms' => '',
'bathrooms' => '',
'ext_stories' => '',
'levels' => '',
'sq_feet' => '',
'sq_feet_src' => '',
'builder' => '',
'year_built' => '',
'guest_sq_feet' => '',
'user_id' => '2'
),
'User' => array(
'Appointment' => array(
'appointment_date' => '',
'appointment_date2' => ''
),
'Referral' => array(
'name' => 'katie',
'contact' => '',
'relationship' => ''
),
'id' => '2'
)
I can tell this is not quite the way the data is formatted in the book, but I can't figure out how to name the fields to get it that way.
If I use the latter naming convention, I get this as output:
array(
'Listing' => array(
'sale_type' => '',
'property_type' => '',
'referer' => '',
'price' => '',
'access_method' => '',
'bedrooms' => '',
'bathrooms' => '',
'ext_stories' => '',
'levels' => '',
'sq_feet' => '',
'sq_feet_src' => '',
'builder' => '',
'year_built' => '',
'guest_sq_feet' => '',
'user_id' => '2'
),
'Appointment' => array(
'appointment_date' => '',
'appointment_date2' => ''
),
'Referral' => array(
'name' => 'katie',
'contact' => '',
'relationship' => '',
'user_id' => '2'
)
This will at least save, but I will end up with 4 Referral records (one for each line of data supplied) that have NULL for name, contact and relationship.
This is my first time using SaveAssociated deep = true, and I'm not sure what I'm not doing right. I'm assuming I should be going with the former approach (nesting these Models under User), but I'm not sure how to name the fields to get the data to look more like the example supplied here
SOLVED I am an idiot, and finally got it:
name="data[User][Referral][0][name]"