title: Doctrine One-To-One, Self-referencing not working
body: <p>I am working on a legacy application and I now need to change an existing table (called categories) so that it has a parent_id field which is a foreign key to the primary key of it’s own table (the categories table)</p>
  
  <p>The issue I am having is that I cannot get Doctrine to set the parent field in the database</p>
  
  <p>I have the following entity orm:</p>
  
  <pre><code>ModelBundle\Entity\Category:
      type: entity
      table: category
      indexes:
          fki_parent_to_id:
              columns:
                  - parent_id
      uniqueConstraints:
          uniq_64c19c1989d9b62:
              columns:
                  - slug
          uniq_name:
              columns:
                  - name
      id:
          id:
              type: integer
              nullable: false
              options:
                  unsigned: false
              id: true
              generator:
                  strategy: SEQUENCE
      fields:
          name:
              type: string
              nullable: false
              length: 255
              options:
                  fixed: false
          slug:
              type: string
              nullable: false
              length: 255
              options:
                  fixed: false
          description:
              type: string
              nullable: true
              length: 255
              options:
                  fixed: false
          isActive:
              type: boolean
              nullable: false
              options:
                  default: false
              column: is_active
          displayOrder:
              type: integer
              nullable: true
              options:
                  unsigned: false
              column: display_order
      oneToOne:
          parent:
              targetEntity: Category
              joinColumns:
                  parent_id:
                      referencedColumnName: id
  
      lifecycleCallbacks: {  }
  </code></pre>
  
  <p>Here is the entity class:</p>
  
  <pre><code>&lt;?php
  
  namespace ModelBundle\Entity;
  
  use Doctrine\Common\Collections\Collection;
  use JMS\Serializer\Annotation\Expose;
  
  use Symfony\Cmf\Bundle\SeoBundle\Extractor\TitleReadInterface;
  use Symfony\Cmf\Bundle\SeoBundle\Extractor\DescriptionReadInterface;
  use Symfony\Cmf\Bundle\SeoBundle\Extractor\ExtrasReadInterface;
  
  class Category implements ExtrasReadInterface, TitleReadInterface, DescriptionReadInterface
  {
      /** @var int */
      private $id;
  
      /** @var string */
      private $slug;
  
      /**
       * @Expose
       *
       * @var string
       */
      private $name;
  
      /** @var bool */
      protected $isActive;
  
      /** @var string */
      private $description;
  
      /** @var Collection */
      private $products;
  
      /** @var int */
      private $displayOrder;
  
      /**
       * @var Category
       */
      private $parent;
  
  
      public function getId(): int
      {
          return $this-&gt;id;
      }
  
      public function setName(string $name): Category
      {
          $this-&gt;name = $name;
          return $this;
      }
  
      public function getName(): string
      {
          return $this-&gt;name;
      }
  
      public function setIsActive(bool $isActive): Category
      {
          $this-&gt;isActive = $isActive;
          return $this;
      }
  
      public function getIsActive(): bool
      {
          return $this-&gt;isActive;
      }
  
      public function setDescription(string $description): Category
      {
          $this-&gt;description = $description;
          return $this;
      }
  
      public function getDescription(): string
      {
          return $this-&gt;description ?: '';
      }
  
      public function __construct()
      {
          $this-&gt;products = new \Doctrine\Common\Collections\ArrayCollection();
      }
  
      public function addProduct(Product $products): Category
      {
          $this-&gt;products[] = $products;
          return $this;
      }
  
      public function removeProduct(Product $products)
      {
          $this-&gt;products-&gt;removeElement($products);
      }
  
      public function getProducts(): Collection
      {
          return $this-&gt;products;
      }
  
      public function setSlug(string $slug): Category
      {
          $this-&gt;slug = $slug;
          return $this;
      }
  
      public function getSlug(): string
      {
          return $this-&gt;slug ?: '';
      }
  
      public function getSeoTitle(): string
      {
          return $this-&gt;getName();
      }
  
      public function getSeoDescription(): string
      {
          return $this-&gt;getDescription();
      }
  
      public function getSeoExtras(): array
      {
          return [
              'property' =&gt; [
                  'og:title'       =&gt; $this-&gt;name,
                  'og:description' =&gt; $this-&gt;description,
                  'og:type'        =&gt; 'product:category'
              ],
          ];
      }
  
      function getDisplayOrder(): ?int
      {
          return $this-&gt;displayOrder;
      }
  
      public function setDisplayOrder($displayOrder): Category
      {
          $this-&gt;displayOrder = $displayOrder;
          return $this;
      }
  
      public function getParent() : Category
      {
          return $this-&gt;parent;
      }
  
      public function setParent(Category $parent): void
      {
          $this-&gt;parent = $parent;
      }
  
  }
  </code></pre>
  
  <p>This maps out what I have said above.
    This is similar to what is here in the docs: <a href="https://www.doctrine-project.org/projects/doctrine-orm/en/2.5/reference/association-mapping.html#one-to-one-self-referencing" rel="nofollow noreferrer">https://www.doctrine-project.org/projects/doctrine-orm/en/2.5/reference/association-mapping.html#one-to-one-self-referencing</a>
    There is no yaml in the examples but I got this yaml from doctrine (using the generation commands).</p>
  
  <p>No matter what I do I can never save what is set in the parent. </p>
  
  <pre><code>Example code:
  
  $em = $this-&gt;get('doctrine')-&gt;getManager();
  $repo = $em-&gt;getRepository(Category::class);
  
      $newCat = new Category();
      $newCat-&gt;setName('Test11');
      $newCat-&gt;setSlug('slug1');
      $newCat-&gt;setDescription('desc1');
      $newCat-&gt;setIsActive(true);
      $newCat-&gt;setDisplayOrder(1);
      $newCat-&gt;setParent($repo-&gt;find(11)); //&lt;-- This does not get saved in the DB
  
      var_dump($newCat);
  
      $em-&gt;persist($newCat);
      $em-&gt;flush();
  </code></pre>
  
  <p>The <code>$repo-&gt;find(11)</code> returns a Category entity that has a parent (I set it manually in the database) but the parent field is NULL. (I have also tried with an entity that has no parent, same result)
  Here is the var_dump:</p>
  
  <pre><code>object(ModelBundle\Entity\Category)[421]
    private 'id' =&gt; null
    private 'slug' =&gt; string 'slug1' (length=42)
    private 'name' =&gt; string 'Teste11' (length=33)
    protected 'isActive' =&gt; boolean true
    private 'description' =&gt; string 'desc1' (length=5)
    private 'products' =&gt; 
      object(Doctrine\Common\Collections\ArrayCollection)[422]
        private 'elements' =&gt; 
          array (size=0)
            empty
    private 'displayOrder' =&gt; int 1
    private 'parent' =&gt; 
      object(ModelBundle\Entity\Category)[435]
        private 'id' =&gt; int 11
        private 'slug' =&gt; string 'entertaining' (length=12)
        private 'name' =&gt; string 'Browse Entertaining' (length=19)
        protected 'isActive' =&gt; boolean false
        private 'description' =&gt; string 'Selection of great food for any occasion.' (length=41)
        private 'products' =&gt; 
          object(Doctrine\ORM\PersistentCollection)[458]
            private 'snapshot' =&gt; 
              array (size=0)
            protected 'initialized' =&gt; boolean false
        private 'displayOrder' =&gt; null
        private 'parent' =&gt; null
  </code></pre>
  
  <p>This code creates a new category in the database but the parent field is set to NULL.</p>
  
  <p>What am I doing wrong here? What do I need to do to get this to save the parent to the database.
  NOTE: I don’t want oneToMany I just want one category to have one parent and that is all.</p>
  
  <p>Versions:</p>
  
  <blockquote>
    <p>doctrine/annotations                  v1.3.1</p>
    
    <p>doctrine/cache                        v1.6.1</p>
    
    <p>doctrine/collections                  v1.4.0 </p>
    
    <p>doctrine/common                       v2.7.2 </p>
    
    <p>doctrine/data-fixtures                v1.2.2</p>
    
    <p>doctrine/dbal                         v2.5.12  </p>
    
    <p>doctrine/doctrine-bundle              1.6.7 </p>
    
    <p>doctrine/doctrine-cache-bundle        1.3.0 </p>
    
    <p>doctrine/doctrine-fixtures-bundle     v2.4.0 </p>
    
    <p>doctrine/doctrine-migrations-bundle   v1.2.1</p>
    
    <p>doctrine/inflector                    v1.1.0</p>
    
    <p>doctrine/instantiator                 1.0.5</p>
    
    <p>doctrine/lexer                        v1.0.1</p>
    
    <p>doctrine/migrations                   v1.5.0</p>
    
    <p>doctrine/orm                          v2.5.6</p>
    
    <p>oro/doctrine-extensions               1.2.0 </p>
  </blockquote>
  
  <p>EDIT:
  Sorry guys, after 2 days of trying to get this to work, I noticed that doctrine had the following for caching (in symfony config):</p>
  
  <blockquote>
  <pre><code>orm:
      auto_generate_proxy_classes: "%kernel.debug%"
      entity_managers:
          default:
              naming_strategy: doctrine.orm.naming_strategy.underscore
              auto_mapping: true
              metadata_cache_driver: apcu
              query_cache_driver: apcu
  </code></pre>
  </blockquote>
  
  <p>As soon as I removed that cache, everything started to work.
  I was removing the caching directory manually but failed to see that doctrine was also being cached with apcu.</p>