简体   繁体   中英

How to retrieve results from joined table in doctrine

I'm doing a join between two tables using the doctrine that comes bundled in the current symfony release. This is my controller code:

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Acme\GearDBBundle\Entity\TbGear;
use Acme\GearDBBundle\Entity\TbDships;

class DefaultController extends Controller
{
    public function indexAction()
    {
        $repository = $this->getDoctrine()
            ->getRepository('AcmeGearDBBundle:TbGear');
        $query = $repository->createQueryBuilder('p')
            ->select('p', 'q')
            ->innerJoin('p.fkShip', 'q', 'WITH', 'p.fkShip = q.id')
            ->getQuery();

        $result = $query->getResult();

        foreach ( $result as $p ) {
            $gear[] = array('shortname' => $p->getGearShortName(), 'name' => $p->getGearName(), 'shipname' => $p->getShipName /* Does not work, since the getter is in a different entity */);
        }

        return $this->render('AcmeGearDBBundle::index.html.twig', array('gear' => $gear));
    }
}

The query generated by this is correct and delivers the expected fields if I execute it in phpmyadmin.

SELECT t0_.GEAR_NAME AS GEAR_NAME0, t0_.GEAR_SHORT_NAME AS GEAR_SHORT_NAME1, t0_.STATUS AS STATUS2, t0_.ID AS ID3, t1_.SHIP_NAME AS SHIP_NAME4, t1_.CONTACT_NAME AS CONTACT_NAME5, t1_.CONTACT_EMAIL AS CONTACT_EMAIL6, t1_.ID AS ID7, t0_.FK_SHIP_ID AS FK_SHIP_ID8, t0_.FK_TYPE AS FK_TYPE9 
FROM tb_gear t0_ 
INNER JOIN tb_dships t1_ ON t0_.FK_SHIP_ID = t1_.ID 
AND (t0_.FK_SHIP_ID = t1_.ID)

However, I have no clue how do access those fields in the returned result set. The way I expected it to work ( by accessing the getter of the joined table entity ) does not work. The error message reads: FatalErrorException: Error: Call to undefined method Acme\\GearDBBundle\\Entity\\TbGear::getShipName() in /var/www/symfony/src/Acme/GearDBBundle/Controller/DefaultController.php line 24 which makes sense since the TbGear entity doesn't have a getter method called getShipName() , since that's a method from the joined entity. But how do I access those values? This probably is a stupid question, but I just can't figure it out. Any help is appreciated.

$p->getFkShip()->getShipName() maybe?

This should work since it will retrieve only TbGear that satisfies you relationship. So you could be able to access to all FkShip (I suppose that is a many-to-one relation) that should be only one, and then .... you got it!

EDIT

Of course I suppose that you have correctly designed your class so that you have a getter from TbGear to access the relation with FkShip

Can you add that custom getter: getShipName() ?

public function getShipName(){
    if ( $this->ship != null ){
        return $this->ship->getName();
    }
    return null; // or an empty string
}

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