How to manage a ManyToOne relation with symfony 2 and doctrine
<p>I’m currently discovering symfony 2 and training myself on this wonderful framework. So I have newbie questions...</p>

<p>I followed this tutorial in order to manage relations with doctrine :
<a href="http://symfony.com/doc/current/book/doctrine.html" rel="nofollow">http://symfony.com/doc/current/book/doctrine.html</a></p>

<p>Well, during my tests I had a question and a problem.</p>

<p><strong>First the question:</strong>
Symphony best practices says that it’s better to use annotations for doctrine mapping instead of yaml in an other .orm.yml file. Good, I totally agree with that. "Using annotations is convenient and allows you to define everything in the same file". So if we choose to use annotations, I guess we do not need an .orm.yml mapping file anymore?
Suppose I have a table named userType. I have created an entity class userType.php and added mapping information with annotations directly in this class. Anything I try, adding the statement:</p>

<pre><code>use Doctrine\ORM\Mapping as ORM;
</code></pre>

<p>or not in my userType.php class, symfony display this error: </p>

<pre><code>No mapping file found named 'UserType.orm.yml' for class 'AppBundle\Entity\UserType
</code></pre>

<p>if I don‘t create the UserType.orm.yml file.</p>

<p>So this my question: If we choose to use only annotations, do we still need an .orm.yml mapping file? If yes, what are the minimum information that we must put in this file?</p>

<p><strong>Second point my problem:</strong>
I have my users roles in a different table (userType) than the user table.</p>

<pre><code>+-------------------------------------------+-------------------------------------------+
|user                                       |   userType                                |
|id    username    password    userType_id  |   userType_id  label          role        |
|1     testuser    something   1            |   1            Administrator  ROLE_ADMIN  |
+-------------------------------------------+-------------------------------------------+
</code></pre>

<p>So I suppose I need to add a manyToOne relation in my user.php entity class (many users can have the only same one role). </p>

<p>So I added thoses lines in my User.orm.yml mapping file</p>

<pre><code>(…)
manyToOne:
        userType:
            targetEntity: UserType
            joinColumn:
                name: userType_id
                referencedColumnName: userType_id
    lifecycleCallbacks: {  }
(…)
</code></pre>

<p>And declared the var userType in my user.php entity class like this</p>

<pre><code>/**
*
*/
private $userType;
</code></pre>

<p>This is my userType.php entity class file:</p>

<pre><code>    namespace AppBundle\Entity;

    use Doctrine\ORM\Mapping as ORM;

    /**
     * @ORM\Entity
     */
    class UserType
    {
        /**
         * @ORM\Id
         * @ORM\Column(type="integer", name="userType_id")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;

        /**
         * @ORM\Column(type="string", name="label")
         */
        private $label;


        /**
         * @ORM\Column(type="simple_array", name="role")
         */
        private $role;


        public function getId()
        {
            return $this-&gt;id;
        }

        /**
         *
         */
        public function getLabel()
        {
            return $this-&gt;label;
        }
        public function setLabel($label)
        {
            $this-&gt;label = $label;
        }

        /**
         *
         */
        public function getRole()
        {
            return $this-&gt;role;
        }
        public function setRole($role)
        {
            $this-&gt;role = $role;
        }
}
</code></pre>

<p>And my UserType.orm.yml file:</p>

<pre><code>AppBundle\Entity\UserType:
    type: entity
    table: userType
    id:
        id:
            type: integer
            id: true
            column: userType_id
            generator:
                strategy: AUTO
    fields:
        label:
            type: string
            length: '150'
            column: label
        role:
            type: simple_array
            length: '100'
            column: role
    lifecycleCallbacks: {  }
</code></pre>

<p>Ok it seems the $userType var of my user.php entity is an object containing the userType data.
But if I do a</p>

<pre><code>var_export($this-&gt;userType, true);
</code></pre>

<p>of  the $userType object I got this :</p>

<pre><code>Proxies\__CG__\AppBundle\Entity\UserType::
__set_state(array( '__initializer__' =&gt; Closure::
__set_state(array( )), '__cloner__' =&gt;
Closure::__set_state(array( )), '__isInitialized__' =&gt;
false, 'id' =&gt; 1, 'label' =&gt; NULL, 'role' =&gt; NULL, ))
</code></pre>

<p>Only the field id is filled others are NULL.
How can I also get the label and role field values in the object $userType? What is my mistake?</p>

<p>Many thanks for your help.</p>