J
J
Jailbird2014-04-10 19:08:33
PHP
Jailbird, 2014-04-10 19:08:33

Doctrine - how to filter (where) result by ManyToOne field?

Hello. I just can't find an option on how to filter the results of a query built with Query Builder. One of the models has a user_id field

/**
     * @ManyToOne(targetEntity="Users")
     * @JoinColumn(name="user_id", referencedColumnName="id", nullable=true)
     */
    private $user;

, but when I try to add a condition
$qb->where('mc.user_id = :user');
, the error "... has no field or association named user_id" appears.
I can make it work by joining the Users table accordingly and adding a condition on the id from it... But somehow it seems to me wrong - I'm actually doing an unnecessary join.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2014-04-10
@Jailbird

$qb->where('mc.user = :user')
You should operate in DQL/Querybuider only with those fields which are in your object. Otherwise, there's no point in DQL/QueryBuidler, if you need more and how it all fits into the base to know.

J
Jailbird, 2014-04-10
@Jailbird

Physically, this field exists. And in the model there is (a piece of code is indicated in the question). And it was created using ORM. I just want to filter on it without doing a join on the table itself. Doesn't the Doctrine allow doing that?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question