I've started playing with new cakephp ORM functionality and i stuck with complex relation updates/saves.
Example relations look like this:
Customer hasMany CustomerDetails
CustomerDetails hasMany Attributes
Attributes hasMany AttributeValues
CustomerController:: view() action pases $customer entity variable (contains all relations) to view.
Variable is then used in view to echo entity data but also is passed to Form->create($customer) to edit part of the data
I build my forms using notation below:
$this->Form->hidden("id");
$this->Form->text("customer_details.0.attributes.0.attribute_values.0.name")
After user sends data i want to populate all $customer entity variable with $this->request->data then try to save it and finally pass to view again (this way all related entities should have validation errors included):
public function view($customer_id){
$this->Customers = TableRegistry::get ( 'Customers' );
/* @var $customer Customer */
$customer=$this->Customers->find()->where(['Customers.id'=>$customer_id])->contain(["CustomerDetails"=>["Attributes"=>["AttributeValues"]]])->first();
if($this->request->is("post")){
$customer->populate($this->request->data,['associated'=>['CustomerDetails'=> ['associated' => ['Attributes'=> ['associated' => ['AttributesValues']]]]]]); //hydrate??
if($this->Customers->save($customer,['associated'=>['CustomerDetails'=> ['associated' => ['Attributes'=> ['associated' => ['AttributesValues']]]]]])){
//Yay
}
}
$this->set(compact('customer'));
}
$this->request->data looks like this after send:
'id' => '1', 'customer_details' => array( (int) 0 => array( 'id' => '1', 'attributes' => array( (int) 0 => array( 'id' => '1', 'attributes_values' => array( (int) 0 => array( 'string_value' => 'test test test' ) ) ), (int) 1 => array( 'name' => 'love', 'attributes_values' => array( (int) 0 => array( 'string_value' => 'me' ) ) ) ) ) ),
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.
No comments:
Post a Comment