在 Doctrine 中向当前表添加虚拟列? [英] Adding virtual columns to current table in Doctrine?

查看:22
本文介绍了在 Doctrine 中向当前表添加虚拟列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 Doctrine 1.2 与 Symfony 1.4 一起使用.假设我有一个 User 模型,它有一个 Profile.这些定义为:

I'm using Doctrine 1.2 with Symfony 1.4. Let's say I have a User model, which has one Profile. These are defined as:

用户:

  • 身份证
  • 用户名
  • 密码
  • created_at
  • updated_at

简介:

  • 身份证
  • user_id
  • 名字
  • 姓氏
  • 地址
  • 城市
  • 邮政编码

我通常会得到这样的数据:

I would normally get data like this:

$query = Doctrine_Query::create()
    ->select('u.id, u.username, p.first_name, p.last_name')
    ->from('User u')
    ->leftJoin('Profile p')
    ->where('u.username = ?', $username);
$result = $query->fetchOne(array(), Doctrine_Core::HYDRATE_ARRAY);
print_r($result);

这将输出如下内容:

Array (
    "User" => Array (
        "id" => 1,
        "username" => "jschmoe"
    ),
    "Profile" => Array (
        "first_name" => "Joseph",
        "last_name" => "Schmoe"
    )
)

但是,我希望用户包含虚拟"列(不确定这是否是正确的术语),以便配置文件中的字段实际上看起来像是用户的一部分.换句话说,我喜欢看到 print_r 语句看起来更像:

However, I would like for user to include "virtual" columns (not sure if this is the right term) such that fields from Profile actually look like they're a part of User. In other words, I'd like to see the print_r statement look more like:

Array (
    "User" => Array (
        "id" => 1,
        "username" => "jschmoe",
        "first_name" => "Joseph",
        "last_name" => "Schmoe"
    )
)

有没有办法通过我的 schema.yml 文件或通过我的 Doctrine_Query 对象来做到这一点?

Is there a way to do this either via my schema.yml file or via my Doctrine_Query object?

推荐答案

做你想做的事情的方法是使用 自定义 Hydrator.

The way to do what you want is to use a custom Hydrator.

class Doctrine_Hydrator_MyHydrator extends Doctrine_Hydrator_ArrayHierarchyDriver
{
    public function hydrateResultSet($stmt)
    {
        $results = parent::hydrateResultSet($stmt);
        $array = array();

        $array[] = array('User' => array(
            'id'         => $results['User']['id'],
            'username'   => $results['User']['username'],
            'first_name' => $results['Profile']['first_name'],
            'last_name'  => $results['Profile']['last_name'],
        ));

        return $array();
    }
}

然后向连接管理器注册您的 hydrator:

Then register you hydrator with the connection manager:

$manager->registerHydrator('my_hydrator', 'Doctrine_Hydrator_MyHydrator');

然后你像这样水合你的查询:

Then you hydrate your query like this:

$query = Doctrine_Query::create()
    ->select('u.id, u.username, p.first_name, p.last_name')
    ->from('User u')
    ->leftJoin('Profile p')
    ->where('u.username = ?', $username);

 $result = $query->fetchOne(array(), 'my_hydrator');
 print_r($result);

/* outputs */
Array (
    "User" => Array (
        "id" => 1,
        "username" => "jschmoe",
        "first_name" => "Joseph",
        "last_name" => "Schmoe"
    )
)

您可能需要对 hyrdator 逻辑进行一些微调才能获得所需的确切数组结构.但这是可以接受的方式来做你想做的事.

You might have to fines the hyrdator logic a little to get the exact array structure you want. But this the acceptable way to do what you want.

这篇关于在 Doctrine 中向当前表添加虚拟列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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