简体   繁体   中英

Select a column (having foreign key) without joining two tables

I want select a column (which has a foreign key constraint) without creating joins on tables. I have two tables named eventupdate and eventcategory . The event column is common in both tables. whenever I try the following code it gives an error.

Please give some suggestion. I don't want to create a join.

$qb2 = $this->em->createQueryBuilder();

$from = 'Entities\EventCategory cat';

$qb2->add('from',$from)

    ->select('cat.event')

    ->Where('cat.id=3);

$query=$qb2->getQuery();

There are two options that I can see:

  1. HINT_INCLUDE_META_COLUMNS together with ArrayHydrator

     $query = $queryBuilder->getQuery(); $query->setHint(\\Doctrine\\ORM\\Query::HINT_INCLUDE_META_COLUMNS, true); var_dump($query->getArrayResult()); // Will return array with raw foreign key column name => value, eg user_id => 5 
  2. Create separate property in Entities\\EventCategory which has the foreign key as primitive type

     /** * @var User * * @ManyToOne(targetEntity="User") * @JoinColumn(name="user_id", referencedColumnName="user_id") */ private $user; /** * @var int * * @Column(name="user_id", type="integer", nullable=false) */ private $userId; 

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