Alex Rivera | Logout

Techniques to check if relationship exists in Doctrine2

Asked 2011-06-03T08:41:29.073
11

I can't seem to find any mention in the Doctrine documentation on how to check if an entity has an existing relationship to another entity:

http://www.doctrine-project.org/docs/orm/2.0/en/reference/working-with-associations.html

In Doctrine 1.x there was a method called exists that could be called on an entity to check this:

http://www.doctrine-project.org/documentation/manual/1_2/en/working-with-models#dealing-with-relations:clearing-related-records

In Doctrine 2.0 this is what I've tended to do. What techniques are other people using?

<?php

class Group    {
    private $id;
    protected $name;
    protected $users;

    public function __construct()
    {
        $this->colorgroups = new ArrayCollection();
    }

    public function hasUsers() {
        return count($this->users) > 0;
    } 
}
Edit
Report

1 Answer

-2

I know this is an old question, but it could be useful to someone else or maybe even to you. I think what you were looking for was the relatedExists() method that you can find here: http://docs.doctrine-project.org/projects/doctrine1/en/latest/en/manual/working-with-models.html#clearing-related-records

Hope it will be useful!

answered 2013-03-30T15:16:03.977

Your Answer