php – 将自定义列添加到Propel模型?
作者:互联网
目前我正在使用以下查询:
$claims = ClaimQuery::create('c')
->leftJoinUser()
->withColumn('CONCAT(User.Firstname, " ", User.Lastname)', 'name')
->withColumn('User.Email', 'email')
->filterByArray($conditions)
->paginate($page = $page, $maxPerPage = $top);
但是我想手动添加列,所以我认为这只会工作:
foreach($claims as &$claim){
$claim->actions = array('edit' => array(
'url' => $this->get('router')->generate('hera_claims_edit'),
'text' => 'Edit'
)
);
}
return array('claims' => $claims, 'count' => count($claims));
但是当返回数据时,Propel或Symfony2似乎在将自定义数据与所有超级模型数据一起转换为JSON时将其剥离.
以这种方式手动添加数据的正确方法是什么?
解决方法:
要将虚拟列导出到out数组,您可以通过以下方式执行此操作:
/**
* Propel result set
* @var \PropelObjectCollection
*/
$claims = ClaimQuery::create('c')-> ... ->getResults();
/**
* Array of data with virtual columns
* @var array
*/
$claims_array = array_map(function (Claim $claim) {
return array_merge(
$claim->toArray(), // using "native" export function
array( // adding virtual columns
'Email' => $claim->getVirtualColumn('email'),
'Name' => $claim->getVirtualColumn('name')
)
);
}, $claims->getArrayCopy()); // Getting array of `Claim` objects from `PropelObjectCollection`
unset($claims); // unsetting unnecessary object if we have further operations to complete
标签:propel,php,orm,symfony-2-1 来源: https://codeday.me/bug/20190901/1783443.html