Fuel PHP - to_array() 方法和多个 belongs_to 关系和预加载
Fuel PHP - to_array() method and multiple belongs_to relationships and eager loading
我正在尝试将一些遗留数据 models/schemas 迁移到燃料 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);
对于任何在这个问题上寻求帮助的谷歌员工,我似乎能够通过简单地执行 to_array()
方法 [=17] return 所有 关系=]在设置return/results变量之前:
$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);
祝你好运!
我正在尝试将一些遗留数据 models/schemas 迁移到燃料 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);
对于任何在这个问题上寻求帮助的谷歌员工,我似乎能够通过简单地执行 to_array()
方法 [=17] return 所有 关系=]在设置return/results变量之前:
$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);
祝你好运!