将数据保存在关联表中

时间:2016-08-18 16:40:22

标签: cakephp cakephp-3.0

我无法在相关表格中保存数据

我有桌子

ProductsTable
ProductsPricesTable

ProductsTable表格中,我宣布了一个像

这样的关系
public function initialize( array $config ) {
  parent::initialize($config);
  ...
  $this->hasMany('ProductsPrices');
}

并且在ProductsController的添加操作中,我有类似

的内容
    if( $this->request->is('post') ) {

      $this->Products->patchEntity( $entity, $this->request->data, [
        'associated' => ['ProductsPrices']
      ]);

      if( $this->Products->save( $entity ) ) {
        ....
        ....
      }
   }

request->data看起来像

[
    'category_id' => '68',
    'code' => '20',
    'name' => 'Tagliatelle Bolognese',
    'description' => 'mit Fleischsauce ',
    'order' => '20',
    'active' => '1',
    'offered' => '0',
    'productsprices' => [
        (int) 0 => [
            'size_id' => '80',
            'price' => '7'
        ]
    ]
]

它只保存prodcts表中的产品,但在products_prices表中没有保存任何内容。其结构如下

id | product_id | size_id | price | created | modified

有什么想法吗?

0 个答案:

没有答案