Reputation: 710
Hi I just made a function to save data in multiple tables. Even Though, I followed the instruction in Instruction for Cakephp 3.0
It saves data in only one table.
Please see what i missed.
Thank you
public function saveTest()
{
$goods = TableRegistry::get('Goods');
//$data = $this->request->data;
$data = [
'brand' => 'brand',
'name' => 'name',
'dis_price' => 100,
'w_price' => 90,
'line_des' => 'haha',
'line_w_des' => 'hehe',
'hash_tag' => 'hoho',
'price' => 100,
'good_stock' => [
'options' => 'asd',
'stock_count' => 100
]];
Debugger::log($data);
$entity = $goods->newEntity($data, [
'associated' => ['GoodsStocks']
]);
Debugger::log($goods->save($entity));
//Debugger::log('3');
}
public function initialize(array $config)
{
$this->entityClass('App\Model\Entity\Goods');
$this->table('goods');
$this->displayField('name');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->hasMany('GoodsStocks',[
'alias' => 'Users',
'foreignKey' => 'good_id',
'dependent' => true
]);
}
Unfortunately It saves the data in only Goods table.
Thank you
Upvotes: 0
Views: 1076
Reputation: 331
Your array for your GoodStock association should be a lower-underscored version of the association name. Plus, it probably won't be a one-dimensional array thus it's a hasMany association. You can find more information here.
So your code should be moreless as:
$data = [
'brand' => 'brand',
'name' => 'name',
'dis_price' => 100,
'w_price' => 90,
'line_des' => 'haha',
'line_w_des' => 'hehe',
'hash_tag' => 'hoho',
'price' => 100,
'good_stocks' => [
['options' => 'asd', 'stock_count' => 100]
]
];
I hope you find it useful.
PS: Sorry for any grammar mistakes but English is not my native language ;)
Upvotes: 1