要将自定义列添加到Propel模型中? [英] Adding custom columns to Propel model?

查看:105
本文介绍了要将自定义列添加到Propel模型中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻,我正在使用以下查询:

At the moment I am using the below query:

$claims = ClaimQuery::create('c')
        ->leftJoinUser()
        ->withColumn('CONCAT(User.Firstname, " ", User.Lastname)', 'name')
        ->withColumn('User.Email', 'email')
        ->filterByArray($conditions)
        ->paginate($page = $page, $maxPerPage = $top);

但是我然后想手动添加列,所以我认为这很简单:

However I then want to add columns manually, so I thought this would simply work:

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时正在剥离.

However when the data is returned Propel or Symfony2 seems to be stripping the custom data when it gets converted to JSON along with all of the superflous model data.

以这种方式手动添加数据的正确方法是什么?

What is the correct way of manually adding data this way?

推荐答案

答案在于toArray()方法,

The answer to this lies in the toArray() method so:

$claims = ClaimQuery::create('c')
    ->leftJoinUser()
    ->withColumn('CONCAT(User.Firstname, " ", User.Lastname)', 'name')
    ->withColumn('User.Email', 'email')
    ->filterByArray($conditions)
    ->paginate($page = $page, $maxPerPage = $top)->getResults()->toArray();

然后您可以根据需要进行修改,这里唯一的问题是当前的toArray方法不返回虚拟列,因此您必须修补该方法以包括它们. (这在PropelObjectCollection类中)

Then you can modify as required, the only issue here is that the current toArray method does not return virtual columns so you would have to patch the method to include them. (This is in the PropelObjectCollection class)

最后,我决定将各个部分分开:

In the end I decided to separate the parts:

return array(
        'claims' => $claims, 
        'count' => $claims->count(),
        'actions' => $this->actions()
    );

通过这种方式,您不必担心虚拟列会丢失,而不必在另一端使用不同的方式来处理数据.

This way you do not have to worry about the virtual columns being lost and jut have to manipulate your data in different ways on the other end.

这篇关于要将自定义列添加到Propel模型中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆