使用cakePHP 2.X重命名find('all')中的Model Name

时间:2015-04-03 15:52:00

标签: php cakephp model find

我有一个模型订单。

当我在我的表Order中执行find('all')时,cakephp会返回如下数据:

array(
    (int) 0 => array(
        'Order' => array(
            'id' => '10'
        )
    ),
    (int) 1 => array(
        'Order' => array(
            'id' => '11'
        )
    )
)

有没有办法将'Order'重命名为'myTEST'?

1 个答案:

答案 0 :(得分:1)

Order模型中,您可以实施afterFind回调,将[Order]替换为[myTEST]。这样的事可能适合你:

public function afterFind($results, $primary = false) {
    $new_results = array();

    foreach ($results as $key => $val) {
        foreach($val as $v){
            $new_results[$key]['myTEST'] = $v;
        }
    }

    return $new_results;

    // OR you could also take this approach - Less coding and perhaps more efficient

    // foreach ($results as $key => $val){
    //   $results[$key]['myTEST'] = $results[$key]['Order'];
    //   unset($results[$key]['Order']);
    // }

    // return $results;
}

我根本没有测试过这个。