Wednesday, February 19, 2014

Re: 3.0 update/save complex relations

That is unfortunately not done yet, currently it is only possible to hydrate a new entity. Also there are some tasks pending for building the options arrays so you don't have to type 'associated' so many times :P

The only real way you can do it *right now* is to traverse you entity yourself and set the values on your own. Would you like to help us getting this done?

On Tuesday, February 18, 2014 7:25:36 PM UTC+1, Robert Meisner wrote:
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");

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: