0

I have 2 linked entities: User and Acess. I want my doctrine User entity to have a field that informs me if the user has acesses or not. I can't do a simple OneToMany relationship between the two tables, because there is thousands of acesses and it would be too costly to get thousands of records from the database once I only need to know if there is any. What I would want is a field linked to a native query like:

select * from accesses where user = <whatever> limit 1

More specifically, something like:

/**
 * USer
 *
 * @ORM\Table(name="user")
 * @ORM\Entity
 */
class User {

    /**
     * @ORM\Column(name="user_id", type="bigint", nullable=false)
     * @ORM\Id
     */
    private $id;

    /**
     * @ORM\Column(name="name", type="string", length=300, nullable=false)
     * @Assert\NotBlank()
     */
    private $name;

    /**
     * @ORM\Query="select exists (select id_acesses from accesses where user = "$id" limit 1)"
     */
    private $hasAcesses;
}

Is this possible ? is there another way to do this ?

Edit:

based on @Otanaught answer below, I have done some tests:

Using a OneToMany relation with EXTRA_LAZY fetch:

user-getAccesses()->isEmpty() selected the whole collection

user-getAccesses()->count() used count(*) in the database which took 243ms to return

for comparasion my query above who did what I want took 12ms in average with peeks of 2ms or even 1ms.

Maybe the good folks at doctrine could implement this at isEmpty for extra lazy queries ?

Thanx @Otanaught

1 Answer 1

1

Doctrine does not provide an annotation that allows you to specify a query for a property of an entity (Annotation reference). You could create a custom method in your repository to implement the check. Did you measure how costly the relation would be? With correct relations and indexes this should be a none issue, because doctrine lazy loads the relation? Check the doctrine documentation about extra lazy collections.

Sign up to request clarification or add additional context in comments.

9 Comments

well actually, reading this docs, extralazy association has what I need. It would be better still if I can access Collection's isEmpty method without triggering the load. Do you know if it's available ?
Actually calling count on extra lazy collections does not load the full collection. The documentation about extra lazy collections states this.
I have read. But it's that when working with large result sets, even a count can be constly. So that's why I thought in using isEmpty instead. But I'm testing. BRB in a couple of minutes with the results.
How do you want to check if something is there without asking? :) Even without an ORM this wouldnt be possible.
Well beacause it depends on the count() implementation. Se the query I gave as an example above ? For my needs it would be much less costly than any count estimate.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.