php - Cannot save associated data with hasMany through (Join Model) -
hi, new cakephp , doing project on cakephp 2.3.4.
have associate product metal class through has many through association . doesn't seem working.
model code
class metal extends appmodel { public $hasmany = array( 'metalproduct' ); } class product extends appmodel { public $hasmany = array( 'metalproduct' ); } app::uses('appmodel', 'model'); class metalproduct extends appmodel { public $belongsto = array( 'metal' => array( 'classname' => 'metal', 'foreignkey' => 'metal_id' ), 'product' => array( 'classname' => 'product', 'foreignkey' => 'product_id' ) );}
my database table names metal, products , metal_products
i have multiple select option selecting more 1 metal type.
how the list of metals
$metals=$this->metal->find('list'); $this->set(compact('metals'));
formhelper code listbox is
<?php echo $this->form->input('metal',array('type' => 'select', 'multiple' => true)); ?>
the product getting saved associations not.
debug array give me this
$message = array( 'product' => array( 'category_id' => '517a514b-0eb0-4ec9-b018-0b948620d4f0', 'name' => 'mangalsutra diamond', 'slug' => 'mangalsutra_diamond', 'description' => '1212', 'metal' => array( (int) 0 => '5183cb65-bf90-459c-b22e-0b748620d4f0', (int) 1 => '5183ce25-c744-433e-b035-0b748620d4f0' ), 'image' => '121212', 'price' => '12121', 'weight' => '12', 'active' => '1', 'category' => 'mangalsutra' ) )
i had put head through walls no clue why associations not getting saved. way in tutorials seems easy, why not working?
i have doubts not saving because metal array passed this
'metal' => array( (int) 0 => '5183cb65-bf90-459c-b22e-0b748620d4f0', (int) 1 => '5183ce25-c744-433e-b035-0b748620d4f0' ),
it should mention 'id''rather (int) 0 or something.
also, database table metal_products have created manually has
id(primary key) metal_id(foreign key metal.id) product_id(foreign key product.id)
am doing wrong naming conventions or way database created? please give me correct ans cause tried others answer not working
i saving via
$this->product->saveall($this->request->data, array('deep' => true))
in model, of in metalproduct model? if need move
class metal extends appmodel { public $hasmany = array( 'metalproduct' ); }
to metal model ,
class product extends appmodel { public $hasmany = array( 'metalproduct' ); }
to product model
also add belongsto each model
i see have join table. join tables habtm associations. hasmany. need use other options available define foreign key relationships in each model outlined above.
please see examples on cake documentation.
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html
also, if unsure of how set models correctly, can use bake feature of cakephp generate models , code in regard.
book.cakephp.org/2.0/en/console-and-shells/code-generation-with-bake.html
if more of visual learner video tut should through basics
Comments
Post a Comment