In Doctrine, how to achieve additional model functionalities for Entity
<p>Let's say I have an Doctrine's (version 2) entity as follows:</p>

<pre><code>&lt;?php
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * User
 *
 * @ORM\Table(name="users")
 * @ORM\Entity
 */
class User {
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id

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

    /**
     * @var string
     *
     * @ORM\Column(name="group_id", type="string", length=6, nullable=true)
     */
     private $groupId;

    // Getters and setters...
}
</code></pre>

<p>Now, I would like to manage <code>User</code>'s relation to <code>Group</code>, <strong>but</strong> with some conditions, like:</p>

<ol>
<li>returning <code>NULL</code> (or some sort of skeleton/template of <code>\AppBundle\Entity\Group</code> with fixed values not loaded from database) if <code>Group</code> of <code>users.group_id</code> does not exist, even if it is set to a value (no key restrictions set in the database to prevent this behaviour), so some sort of validation/check required</li>
<li>lazy load <code>Group</code>, when calling <code>$user-&gt;getGroup()</code></li>
</ol>

<p>I am reading Google on and on and I'm confused of how to achieve that <strong>properly</strong> (in the line with Doctrine/Symfony way).</p>

<p>I could add a <em>ManyToOne</em> to the entity's class relation like this:</p>

<pre><code>/**
 * @var \AppBundle\Entity\Group
 *
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Group")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="group_id", referencedColumnName="id")
 * })
 */
private $group;
</code></pre>

<p>But then how to prevent exceptions caused by non-existing foreign keys? I.e. I would like to retrieve user's group details when it is available in my database, but when it is not I do not want <em>Doctrine</em> to throw an exception an crash my application.</p>

<p>People say that using an <em>Entity Manager</em> from within an <code>Entity</code> is a <a href="https://stackoverflow.com/questions/4108291/using-entitymanager-inside-doctrine-2-0-entities">very bad practice</a> and I agree. But I am confused about using <a href="https://stackoverflow.com/questions/4923817/what-is-a-proxy-in-doctrine-2">Proxy Objects</a> or <a href="http://doctrine-orm.readthedocs.org/en/latest/reference/inheritance-mapping.html" rel="nofollow noreferrer">Inheritance Mapping</a> for this purposes.</p>

<p>It seems like using <code>Models</code> could be the way, but I couldn't find any strong documentation of how to properly implement them in <em>Doctrine2</em>.</p>

<p>Please help if you can. In <em>Kohana</em> it was so, so simple (but immature this way).</p>

<hr>

<p><strong>EDIT:</strong></p>

<p><a href="https://stackoverflow.com/users/5917955/massimiliano-fedel">@Massimiliano Fedel</a> suggested trying catching an exception in <code>User::getGroup()</code> method, to eventually make non-existed groups return as <code>NULL</code>.</p>

<p>So I have commited this code:</p>

<pre><code>/**
 * Get group
 *
 * @return \AppBundle\Entity\Group
 */
public function getGroup() {
    try {
        $group = $this-&gt;group;
    } catch (\Doctrine\ORM\EntityNotFoundException $e) {
        $group = null;
    }
    return $group;
}
</code></pre>

<p>Unfortunatelly, it seems that exception cannot be catched this way, because framework exits with an <code>Doctrine\ORM\EntityNotFoundException</code>:</p>

<pre><code>Entity of type 'AppBundle\Entity\Group' for IDs id(999) was not found
</code></pre>

<hr>

<p><strong>EDIT 2:</strong></p>

<p>Below you can find some basing schema of <code>User</code> flow, i.e. why I can not ensure that all <code>Users</code> will have available <code>Groups</code> in my database.</p>

<p><a href="https://i.stack.imgur.com/XsOkg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XsOkg.png" alt="enter image description here"></a></p>