简体   繁体   English

Symfony2 / Doctrine2从两个表中获取数据

[英]Symfony2/Doctrine2 Get data from two tables

I have an Alert Class with some data in it. 我有一个警报类,其中包含一些数据。 I then have an Availability class. 然后,我有一个Availability类。 In my Availability class I have 在我的班次上,我有

/**
 * @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 this is a foreign key back to my Alert class, linked to my Alerts class id. 因此,这是回到我的Alert类的外键,链接到我的Alerts类ID。

I am now doing some work on the Availability data, so I have the DQL query 我现在在可用性数据上做一些工作,所以我有DQL查询

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

The way I call that is like this 我这样称呼

public function getAvailabilityData(){
        $alerts = $this->em->getRepository('NickAlertBundle:Alert')->getActiveAlertIds();

        if (!$alerts) {
            echo "No Availability";
        }

        foreach($alerts as $alert){
            $alertId = (int)$alert['id'];
            $allAvailability = $this->em->getRepository('NickAlertBundle:Availability')->getAlertAvailability($alertId);
        }
}

So I essentially get all my active Alerts Ids, and then pass this to get my Availability for these individual Alerts. 因此,我基本上会获得所有活动的警报ID,然后将其传递以获得这些单个警报的可用性。

Now I have a couple of problems. 现在我有几个问题。

Firstly, in the DQL query I make, I need to also get something from my Alert table (a field called command). 首先,在我进行的DQL查询中,还需要从Alert表中获取一些信息(称为command的字段)。 How would I do a join in this query to get this piece of data? 我将如何在此查询中进行联接以获取此数据?

Secondly, with the data that is returned, how do I access availabilityAlert in my Twig file? 其次,使用返回的数据,如何访问Twig文件中的AvailabilityAlert?

UPDATE Attempt at join 更新尝试加入

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

Doctrine will load that entity as a proxy (for lazy loading) when the Availability entity is loaded. 当加载了可用性实体时,Doctrine将将该实体作为代理加载(用于延迟加载)。

You can access these via a normal getter / property access, but they will typically be lazy loaded by Doctrine. 您可以通过常规的getter / property访问来访问它们,但是它们通常会被Doctrine延迟加载。 You can have them joined via a DQL query and the property will be hydrated with all the linked entities already loaded, see Improving Performance with a Join . 您可以通过DQL查询将它们联接,并且该属性将与已加载的所有链接实体一起被水合,请参阅“通过联接提高性能”

You can then access those associated entities in Twig as any other property. 然后,您可以像其他任何属性一样访问Twig中的那些关联实体。

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

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