1

我正在尝试将一些遗留数据模型/模式迁移到燃料 API,并且在to_array()具有两个$_belongs_to属性的模型上遇到了一个奇怪的问题。

当我在不使用该to_array()方法的情况下加载模型时,我会正确地接收到两个相关的项目并通过预先加载,但是一旦我通过此函数将它们传递以转换数据以使其可被新 API 消化,它将删除第二个$_belongs_to属性。如果我重新排序数组中的道具$belongs_to,它将显示数组中的第一个项目。

我的问题是,如何在不丢失第二个关系的情况下将此数据转换为数组?

以下是一些清理后的示例,以方便参考:

交易模式:

protected static $_belongs_to = array(
    'benefactor' => array(
        'key_from' => 'from_user_id',
        'model_to' => 'Model\\Legacy\\User',
        'key_to' => 'id',
    ),
    'user' => array(
        'key_from' => 'user_id',
        'model_to' => 'Model\\Legacy\\User',
        'key_to' => 'id',
    ),
);

事务控制器:

$result = array();
$id = $this->param('id');

if (!empty($id)) {
    $transaction = Transaction::find($id, array('related' => array('user', 'benefactor',)));
    if (!empty($transaction)) {

        // Works -- both benefactor and user are returned
        $result['transaction_works'] = $transaction;

        // Does not work -- only the benefactor is returned
        $result['transaction_doesnt_work'] = $transaction->to_array();
    }
}

return $this->response($result);
4

1 回答 1

0

对于任何在这个问题上寻求帮助的谷歌用户,我似乎能够通过在设置 return/results 变量之前简单地执行该方法来返回所有关系:to_array()

$result = array();
$id = $this->param('id');

if (!empty($id)) {
     $transaction = Transaction::find($id, array('related' => array('user', 'benefactor',)));
    if (!empty($transaction)) {
        $transaction->to_array();
        $result['transaction_works'] = $transaction;
    }
}

return $this->response($result);

祝你好运!

于 2015-03-09T17:59:12.057 回答