Cakephp将值从其他模型复制到主模型

时间:2013-12-01 01:51:14

标签: cakephp

您好我有型号产品和几个相关型号,输出数组:

array (size=2)
  0 => 
    array (size=6)
      'Product' => 
        array (size=7)
          'id' => string '121' (length=3)
          'code' => string '123' (length=3)
      'ProductLanguage' => 
        array (size=7)
          'id' => string '5' (length=1)
          'product_id' => string '121' (length=3)
          'name' => string 'abc' (length=3)
      'ProductPrice' => 
        array (size=2)
          0 => 
            array (size=9)
           'id' => string '1' (length=1)
           'product_id' => string '121' (length=3)
           'type_id' =>  string '7'
           'price' => string '555' (length=3)

          1 => 
            array (size=9)
           'id' => string '2' (length=1)
           'product_id' => string '121' (length=3)
           'type_id' =>  string '8'
           'price' => string '900' (length=3)
   1 =>
...

是否有可能从第一条记录(从ProductPrice>价格记录0)到产品的复制价格? 我想要这个:

array (size=2)
  0 => 
    array (size=6)
      'Product' => 
        array (size=7)
          'id' => string '121' (length=3)
          'code' => string '123' (length=3)
          'price' => 555
....

1 个答案:

答案 0 :(得分:0)

是的,在你的afterFind回调(在产品型号中)

public function afterFind($results, $primary = false) {
    foreach ($results as $key => $val) {
        if (isset($val['ProductPrice'][0]['price'])) {
            $results[$key]['Product']['price'] = $val['ProductPrice'][0]['price'];
        }
    }
    return $results;
}