Reputation: 357
I am working on a project of my company. However, I am not familiar with Doctrine. I am the old-styled query-guy.
Table-A and Table-B is in one-to-many relation, linking up by "a_id" on Table-B. In the Entity-B, $a_name is specified.
Table-A
a_id
a_name
a_attr
Table-B
b_id
a_id
b_name
b_attr
Entity-B
/**
* @ORM\ManyToOne(targetEntity="EntityA")
* @ORM\JoinColumn(name="a_id", referencedColumnName="a_id")
* @var Timezone
*/
protected $a_name;
Now I am writing a method to get a set of records using IN()
/**
* @param array $ids
*
* @return array
*/
public function getByIds($ids) {
$query = $this->getEntityManager()->createQuery('SELECT t FROM Entity-B t INDEX BY t.id WHERE t.id IN (:ids)');
}
The above line "INDEX" and "WHERE" with the Entity-B ID. How can I "INDEX" and "WHERE" with Entity-A's ID (a_id on Table-B)?
Thanks.
Upvotes: 0
Views: 934
Reputation: 1055
Try this or similar for your DQL:
SELECT a.a_id, t.b_id, t.b_name, t.b_attr FROM Entity-B t LEFT JOIN t.a_name a INDEX BY a.id WHERE a.a_id IN (:ids)
We just add a join to Entity-A, include a.id in the select and then index by a.id.
As we working with entities in doctrine, we need to join the entity to get at its id to then index by it. Also, the naming of your properties within each entity could be simpler and more intuitive. So rather than Entity-A (a) having properties a_id
, a_attr
, etc, just use id
, attribute
, etc. I assume you just generalised your code for the question and you probably have nicer property names in your project.
Let me know how that DQL works out for you.
Upvotes: 1