简体   繁体   English

序列化doctrine2实体

[英]serializing doctrine2 entities

I have the following entities: Event :hasOne: User :hasHone: Group 我具有以下实体: Event :hasOne: User :hasHone: Group

I want to render an array (like the getArrayResult() method, but with the linked entities, so here what I've done: 我想呈现一个数组(像getArrayResult()方法一样,但是具有链接的实体),所以这里我要做的是:

// build query
$em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');

$query = $em->createQueryBuilder()
    ->select('evt, usr')
    ->from('Application\Model\Event', 'evt')
    ->join('Application\Model\User', 'usr')
    ->where('evt.userId in (:uid)')
    ->setParameter('uid', $usersId);

if (null !== $from) {
    $query->andWhere('TRUNC(evt.litteralDate) > TO_DATE(:from, \'yyyy-mm-dd\')')
        ->setParameter('from', $from);
}

if (null !== $to) {
    $query->andWhere('TRUNC(evt.litteralDate) < TO_DATE(:to, \'yyyy-mm-dd\')')
        ->setParameter('to', $to);
}


$events = $query->getQuery()->getArrayResult();

The problem is that getArrayResult() flattent all my selected data so I have users and events on the same level, how can I achieve something like this: 问题是getArrayResult()使所有选择的数据平坦化,因此我和用户在同一级别上,如何实现这样的目标:

{
    "events": [
        {
            "id": 1677,
            "userId": 2,
            "type": "T",
            "date": {
                "date": "2013-03-05 00:00:00",
                "timezone_type": 3,
                "timezone": "Europe/Paris"
            },
            "litteralDate": "05-MAR-13",
            "piquet": true,
            "when": "passe",
            "description": "Jour Travaille",
            "user" : {
                "id" : 2,
                "login" : "myUserName",
                "name" : "MY user name"
            }
        },
}

If you want to fetch-join one entity inside another you need to ask for the main entity and the ones you want to join in your select clause. 如果要在另一个实体中fetch-join一个实体,则需要在select子句中要求主要实体和要加入的实体。 Read here on how to fetch-join in dql . 在此处阅读有关如何在dql中进行提取的信息

Right now you reference the entity class for Application\\Model\\User in your join clause, you should reference the association between the event entity and the user entity instead. 现在,您在join子句中引用了Application\\Model\\User的实体类,您应该改为引用事件实体和用户实体之间的关联。

So lets say you are in your repository for your event entity Application\\Model\\Event (for which you use short notation evt ) and you want to join an entity Application\\Model\\User that is associated to the event entity in a property user . 因此,假设您位于事件实体Application\\Model\\Event (使用短记法evt )的存储库中,并且您想加入与属性user的事件实体相关联的实体Application\\Model\\User Then you need to write the join clause like this: 然后,您需要这样编写join子句:

$query = $this->createQueryBuilder('evt') 
              ->select('evt', 'usr') 
              ->leftJoin('evt.user', 'usr')

              ...other parts of the query

              ->getQuery();

$array = $query->getResult(Query::HYDRATE_ARRAY)

Since you are in the Repository for the Application\\Model\\Event entity you don't need your FROM clause at all (you can skip that), since by default the created QueryBuilder will use that entity by default for $this repository. 由于您位于Application\\Model\\Event实体的存储库中,因此根本不需要FROM子句(可以跳过该子句),因为默认情况下,创建的QueryBuilder将默认使用该实体作为$this存储库。

Another tip: 另一个提示:

I think for extracting your result you should refer to the DoctrineObject hydrator . 我认为要提取结果,应参考DoctrineObject水化器

You can extract objects both by value and by reference. 您可以按值和按引用提取对象。 You can read on how to do that here . 您可以在此处阅读如何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM