Check If Record Has References

44 views Asked by At

I wonder if there is any way to find out if a record has references.

I have the following entity:

class Item extends Entity
{
    /**
     * @Id @GeneratedValue
     * @Column(name="id")
     */
    protected $id;

    /**
     * @OneToMany(targetEntity="Citation", mappedBy="citation")
     */
    protected $citedIn;

    /**
     * @Column(name="stamped", type="boolean")
     */
    protected $stamped;

    /** ... rest of the code ... */

}

And I want to perform the following check:

$qb
    ->select("
        COUNT(i.id),
        WHEN (i.stamped = true) THEN 'stamped'
        WHEN (i.citedIn IS NOT NULL) THEN 'cited'
        ELSE 'just started'
        END current_status
    ")
    ->from(Entity\Item::class, 'i')
    ->groupBy('current_status');

Note that I tried to use i.citedIn IS NOT NULL, but it didn't work. What could I use instead?

1

There are 1 answers

0
Katrina On

Since it is a OneToMany relationship I think you need to use IS NOT EMPTY instead of IS NOT NULL. Doctrine will create an empty ArrayCollection for each entity, so technically it's not null, because it's an empty array.

Update

If that doesn't work, try NOT INSTANCE OF YourApp\Model\Citation.