简体   繁体   中英

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

/**
 * @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. 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? If this is the case, why does it allow me to get anything from the Alert table eg

\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. This means they are linked. Is there any way to combine them if they have the same id?

Thanks

In your alert entity, you have an availabilityAlert object (at really, you have a "many to one" relationship);
in other words, when you load an alert object, you load an array with the availabilityAlert associated with it.
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 ).
Note that those find options not accept joins , but for your example you don't need it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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