Symfony where clause on the entity level
<p>I've got two entities that are linked together by a one-to-many relationship and I'm using soft deletes on both entities. Because I'm using soft deletes however, reading data is a little bit more tricky because I need to check if the deleted flag is set to false before reading it out.</p>

<p>The basic setup of the entities are:</p>

<pre><code>class Division extends MasterData {
    ...

    /**
     * @var Asset
     *
     * @ORM\OneToMany(targetEntity="Asset", mappedBy="division")
     */
   private $assets;


   public function __construct() {
       $this-&gt;assets = new ArrayCollection();
   }

   public function getAssets() {
       return $this-&gt;assets;
   }

   public function addAssets(Asset $asset) {
       $this-&gt;assets[] = $asset;
       return $this;
   }

   ...
}
</code></pre>

<p><br/></p>

<pre><code>class Asset extends MasterData {
    ...

    /**
     * @var Division
     *
     * @ORM\ManyToOne(targetEntity="Division", inversedBy="assets")
     */
    private $division;

    ...
}
</code></pre>

<p><br/></p>

<pre><code>class MasterData {
    /**
     * @ORM\Column(name="deleted", type="boolean", options={"default":0})
     */
    protected $deleted;

   public function __construct() {
       $this-&gt;deleted = 0;
   }

   ...
}
</code></pre>

<p><em>These are only snippets of the entities, not the entire thing.</em></p>

<p>When I am in the controller for a <code>Division</code>, I'd like to pull a list of all the <code>Assets</code> that are related to that division and are not marked as deleted. I can see a couple ways of doing this.</p>

<p>An easy solution would be to create a custom repository to handle the pull of data. This however would provide a limitation when I would like to further filter data (using <code>findBy()</code> for example).</p>

<p>A second solution would be to alter the <code>getAssets()</code> function in the <code>Division</code> entity to only return assets that are not deleted. This however means that I'm pulling all of the data from the database, then filtering it out post which is very inefficient.</p>

<p>Ideally, I'm looking for a way to alter the definition in the entity itself to add a where clause for the asset itself so that way the filtering is happening in the entity removing the needs for custom repositories and a more efficient option. Similar as to how I can define <code>@ORM\OrderBy()</code> in the annotations, is there a way to similar to this that lets me filter out deleted assets pre-execution and without a custom repository?</p>

<p>Thanks in advance :)</p>