Check not records exist for one to many in Symfony/Doctrine
<p>I have a relationship that is one to many. I have maps (each block is a separate map) and rooms (each map will have multiple rooms). So for the entities (I will cut out info not needed) I have:</p>

<pre><code>/**
 * Block
 *
 * @ORM\Table(name="BLOCK")
 * @ORM\Entity
 */
class Block
{
    /**
     * @var integer
     *
     * @ORM\Column(name="PK", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="SEQUENCE")
     * @ORM\SequenceGenerator(sequenceName="SEQ_GET_BLOCK_PK", allocationSize=1, initialValue=1)
     * @ORM\OneToMany(targetEntity="Rooms", mappedBy="blockId")
     */
    private $pk;
}

/**
 * Rooms
 *
 * @ORM\Table(name="ROOMS", indexes={@ORM\Index(name="IDX_EB2CE87E9B70DE02", columns={"MAP_ID"})})
 * @ORM\Entity
 */
class Rooms
{
    /**
     * @var integer
     *
     * @ORM\Column(name="PK", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="SEQUENCE")
     * @ORM\SequenceGenerator(sequenceName="SEQ_GET_ROOMS_PK", allocationSize=1, initialValue=1)
     */
    private $pk;

    /**
     * @var \Block
     *
     * @ORM\ManyToOne(targetEntity="Block")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="BLOCK_ID", referencedColumnName="PK")
     * })
     */
    private $block;
</code></pre>

<p>Now to get what block belongs to a room, that's simple, in the controller:</p>

<pre><code>$roomRepo = $this-&gt;getDoctrine()-&gt;getRepository('AppBundle:Rooms');
$rooms = $roomRepo-&gt;findAll();
</code></pre>

<p>and in Twig (where fieldname is another field in the block table):</p>

<pre><code>{% for room in rooms %}
    {{ room.block.fieldname }}
{% endfor %}
</code></pre>

<p>However, to display a list of blocks and show the rooms under them becomes harder. I would like to be able to get the list of blocks and do something in Twig like:</p>

<pre><code>{% for block in blocks %}
    {% for room in block.rooms %}
        {{ room.name }}
    {% endfor %}
{% endfor %}
</code></pre>

<p>In this situation, I am trying to get a list of blocks that do not have any rooms under them. If the solution above worked, I could just do a count of rooms in each block and only display if there is none. The other option is to make the query do the count in createQueryBuilder and return the entity block with the correct results.</p>

<p>Any ideas? I keep looking down these paths but hit walls after a bit.</p>