简体   繁体   English

Symfony2 / Doctrine2了解查询结果

[英]Symfony2/Doctrine2 Understanding result of query

So I have an Alert table. 所以我有一个警报表。 I also have an availability table. 我也有一个可用性表。 An alert can have one or more availability. 警报可以具有一个或多个可用性。 This is expressed in my Availability class by 这在我的Availability类中由

/**
 * @var \Nick\AlertBundle\Entity\Alert
 *
 * @ORM\ManyToOne(targetEntity="Nick\AlertBundle\Entity\Alert")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="availability_alert_id", referencedColumnName="id")
 * })
 */
private $availabilityAlert;

So availabilityAlert links to the id field in my Alert table. 因此,AvailabilityAlert链接到我的警报表中的id字段。 So I run a query which essentially joins these two tables together (or so I think) 所以我运行了一个查询,该查询实际上将这两个表连接在一起(或者我认为)

public function getAlertAvailability()
{
    return $this->getEntityManager()
        ->createQuery(
            'SELECT a, u.id
                FROM NickAlertBundle:Availability a
                JOIN a.availabilityAlert u
                WHERE u.id = a.availabilityAlert
                ORDER BY a.classLetter, a.lastUpdated'
        )
        ->getResult();
}

Now if I output the result of that I essentially get 现在,如果我输出的结果基本上是

array(3) { 
    [0]=> array(2) { 
        [0]=> string(37) "Nick\AlertBundle\Entity\Availability" 
        ["id"]=> int(5) 
    } 
    [1]=> array(2) { 
        [0]=> string(37) "Nick\AlertBundle\Entity\Availability" 
        ["id"]=> int(5) 
    } 
    [2]=> array(2) { 
        [0]=> string(37) "Nick\AlertBundle\Entity\Availability" 
        ["id"]=> int(6) 
    } 
} 

So what exactly is this telling me? 那么这到底是在告诉我什么呢? I have access to the whole Availability Entity and the id of the Alert table? 我可以访问整个可用性实体和警报表的ID? If this is the case, why does it allow me to get anything from the Alert table eg 如果是这样,为什么它允许我从Alert表中获取任何信息,例如

\Doctrine\Common\Util\Debug::dump($allAvailability[0][0]->getAvailabilityAlert()->getAlertStatus());

Lastly, in the output of my query above, you can see that element 0 and 1 have the same id. 最后,在上述查询的输出中,您可以看到元素0和1具有相同的ID。 This means they are linked. 这意味着它们已链接。 Is there any way to combine them if they have the same id? 如果它们具有相同的ID,有什么方法可以合并它们?

Thanks 谢谢

In your alert entity, you have an availabilityAlert object (at really, you have a "many to one" relationship); 在您的警报实体中,您有一个AvailabilityAlert对象(实际上,您具有“多对多”关系);
in other words, when you load an alert object, you load an array with the availabilityAlert associated with it. 换句话说,当您加载警报对象时,您将加载具有与其关联的AvailabilityAlert的数组。
This is done by the default, you don´t need a query for it. 默认情况下会完成此操作,您不需要查询。

Also, if you need execute a consult on the database, you can use some " fetchings " provided by doctrine, like find , findAll , findBy and findOneBy ( http://goo.gl/Cvve1i ). 另外,如果您需要在数据库上执行一个咨询,您可以使用学说提供了一些“fetchings”,发现findAll,findByfindOneBy( http://goo.gl/Cvve1i )。
Note that those find options not accept joins , but for your example you don't need it. 请注意,这些查找选项不接受join ,但是对于您的示例,您不需要它。

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

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