title: Symfony 2, Undefined variable, protected member initialized in constructor as ArrayCollection throughs error, that it is undefined
body: <p>I use Symfony 2. I have Entity with protected variable, which is initialized in constructor as ArrayCollection. But than i use command </p>
  
  <pre><code>&gt;php app/console doctrine:fixtures:load -vvv 
  </code></pre>
  
  <p>i am getting error:</p>
  
  <pre><code> [Symfony\Component\Debug\Exception\ContextErrorException]
   Notice: Undefined variable: comments
  
  Exception trace:
   () at C:\Bitnami\wampstack-5.5.30-0\sym_prog\dctr\src\BlogBundle\Entity\Post.ph
  p:183
   Symfony\Component\Debug\ErrorHandler-&gt;handleError() at C:\Bitnami\wampstack-5.5
  .30-0\sym_prog\dctr\src\BlogBundle\Entity\Post.php:183
   BlogBundle\Entity\Post-&gt;addComment() at C:\Bitnami\wampstack-5.5.30-0\sym_prog\
  dctr\src\BlogBundle\DataFixtures\ORM\LoadAuthorData.php:54
  &lt;...&gt;
  </code></pre>
  
  <p>//C:\Bitnami\wampstack-5.5.30-0\sym_prog\dctr\src\BlogBundle\Entity\Post.php</p>
  
  <pre><code>/**
   * Add comment
   *
   * @param \BlogBundle\Entity\Comment $comment
   *
   * @return Post
   */
  public function addComment(\BlogBundle\Entity\Comment $comment)
  {
  182    $this-&gt;comments[] = $comment;
  183    $comments-&gt;setPost($this);
  184    return $this;
  }
  </code></pre>
  
  <p>//Full file: \dctr\src\BlogBundle\Entity\Post.php</p>
  
  <pre><code>&lt;?php
  
  namespace BlogBundle\Entity;
  
  use Doctrine\Common\Collections\ArrayCollection;
  use Doctrine\ORM\Mapping as ORM;
  use Doctrine\ORM\Mapping\Entity;
  use Doctrine\ORM\Mapping\Id;
  use Doctrine\ORM\Mapping\GeneratedValue;
  use Doctrine\ORM\Mapping\Column;
  use Doctrine\ORM\Mapping\OneToMany;
  use Doctrine\ORM\Mapping\ManyToMany;
  use Doctrine\ORM\Mapping\JoinTable;
  use Doctrine\ORM\Mapping\JoinColumn;
  //use Symfony\Component\Validator\Constraints as Assert;
  
  
  /**
   * Post
   *
   * @ORM\Table(indexes={@ORM\Index(name="publication_date_idx",columns="publicationDate")})
   * @ORM\Entity(repositoryClass="BlogBundle\Entity\PostRepository")
   */
  class Post
  {
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var string
       *
       * @ORM\Column(name="title", type="string", length=255)
       */
      private $title;
  
      /**
       * @var string
       *
       * @ORM\Column(name="body", type="text")
       */
      private $body;
  
      /**
       * @var \DateTime
       *
       * @ORM\Column(name="publicationDate", type="datetime")
       */
      private $publicationDate;
  
       /**
       * @var Comment[]
       *
       * @ORM\OneToMany(targetEntity="Comment", mappedBy="post")
       */
      protected $comments;
  
       /**
       * @var Tag[]
       *
       * @ORM\ManyToMany(targetEntity="Tag", inversedBy="posts", fetch="EAGER", cascade={"persist"}, orphanRemoval=true)
       * @ORM\JoinTable(
       *      inverseJoinColumns={@ORM\JoinColumn(name="tag_name", referencedColumnName="name")}
       * )
       */
      protected $tags;
  
      /**
       * @var PostAuthor
       *
       * @ORM\ManyToOne(targetEntity="PostAuthor", inversedBy="posts")
       */
      protected $author;
  
      /**
       * Initializes collections
       */
      public function __construct()
      {
          $this-&gt;comments = new ArrayCollection();
          $this-&gt;tags = new ArrayCollection();
      }
  
      /**
       * Get id
       *
       * @return integer
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set title
       *
       * @param string $title
       *
       * @return Post
       */
      public function setTitle($title)
      {
          $this-&gt;title = $title;
  
          return $this;
      }
  
      /**
       * Get title
       *
       * @return string
       */
      public function getTitle()
      {
          return $this-&gt;title;
      }
  
      /**
       * Set body
       *
       * @param string $body
       *
       * @return Post
       */
      public function setBody($body)
      {
          $this-&gt;body = $body;
  
          return $this;
      }
  
      /**
       * Get body
       *
       * @return string
       */
      public function getBody()
      {
          return $this-&gt;body;
      }
  
      /**
       * Set publicationDate
       *
       * @param \DateTime $publicationDate
       *
       * @return Post
       */
      public function setPublicationDate($publicationDate)
      {
          $this-&gt;publicationDate = $publicationDate;
  
          return $this;
      }
  
      /**
       * Get publicationDate
       *
       * @return \DateTime
       */
      public function getPublicationDate()
      {
          return $this-&gt;publicationDate;
      }
  
  
  
      /**
       * Add comment
       *
       * @param \BlogBundle\Entity\Comment $comment
       *
       * @return Post
       */
      public function addComment(\BlogBundle\Entity\Comment $comment)
      {
          $this-&gt;comments[] = $comment;
          $comments-&gt;setPost($this);
          return $this;
      }
      /* Another important thing, as already explained, 
       * is that Doctrine only manages the owning side of an association. 
       * This is why we call the setPost() method of the Comment entity 
       * in the addComment() method. This allows persisting 
       * with an association from the inverse side. */
  
  
      /**
       * Remove comment
       *
       * @param \BlogBundle\Entity\Comment $comment
       */
      public function removeComment(\BlogBundle\Entity\Comment $comment)
      {
          $this-&gt;comments-&gt;removeElement($comment);
      }
  
      /**
       * Get comments
       *
       * @return \Doctrine\Common\Collections\Collection
       */
      public function getComments()
      {
          return $this-&gt;comments;
      }
  
      /**
       * Add tag
       *
       * @param \BlogBundle\Entity\Tag $tag
       *
       * @return Post
       */
      public function addTag(\BlogBundle\Entity\Tag $tag)
      {
          $this-&gt;tags[] = $tag;
  
          return $this;
      }
  
      /**
       * Remove tag
       *
       * @param \BlogBundle\Entity\Tag $tag
       */
      public function removeTag(\BlogBundle\Entity\Tag $tag)
      {
          $this-&gt;tags-&gt;removeElement($tag);
      }
  
      /**
       * Get tags
       *
       * @return \Doctrine\Common\Collections\Collection
       */
      public function getTags()
      {
          return $this-&gt;tags;
      }
  
      /**
       * Set author
       *
       * @param \BlogBundle\Entity\PostAuthor $author
       *
       * @return Post
       */
      public function setAuthor(\BlogBundle\Entity\PostAuthor $author = null)
      {
          $this-&gt;author = $author;
  
          return $this;
      }
  
      /**
       * Get author
       *
       * @return \BlogBundle\Entity\PostAuthor
       */
      public function getAuthor()
      {
          return $this-&gt;author;
      }
  }
  </code></pre>
  
  <p>//sym_prog\dctr\src\BlogBundle\DataFixtures\ORM\LoadAuthorData.php</p>
  
  <pre><code>line 54 $post-&gt;addComment($comment);
  </code></pre>
  
  <p>//sym_prog\dctr\src\BlogBundle\DataFixtures\ORM\LoadAuthorData.php</p>
  
  <pre><code>&lt;?php
  
  namespace BlogBundle\DataFixtures;
  
  /* This fixture creates instances 
   of Post, PostAuthor, Comment, and CommentAuthor 
   and then persists them to the database.
   */
  
  use BlogBundle\Entity\Comment;
  use BlogBundle\Entity\CommentAuthor;
  use BlogBundle\Entity\Post;
  use BlogBundle\Entity\PostAuthor;
  use Doctrine\Common\DataFixtures\Doctrine;
  use Doctrine\Common\DataFixtures\FixtureInterface;
  use Doctrine\Common\Persistence\ObjectManager;
  
  /**
   * Author fixtures
   */
  class LoadAuthorData implements FixtureInterface
  {
      /**
       * {@inheritDoc}
       */
      public function load(ObjectManager $manager)
      {
          $postAuthor = new PostAuthor();
          $postAuthor-&gt;setName('George Abitbol');
          $postAuthor-&gt;setEmail('gabitbol@example.com');
          $postAuthor-&gt;setBio('L\'homme le plus classe du monde');
  
          $manager-&gt;persist($postAuthor);
  
          $post = new Post();
          $post-&gt;setTitle('My post');
          $post-&gt;setBody('Lorem ipsum');
          $post-&gt;setPublicationDate(new \DateTime());
          $post-&gt;setauthor($postAuthor);
  
          $manager-&gt;persist($post);
  
          $commentAuthor = new CommentAuthor();
          $commentAuthor-&gt;setName('Kévin Dunglas');
          $commentAuthor-&gt;setEmail('dunglas@gmail.com');
  
          $manager-&gt;persist($commentAuthor);
  
          $comment = new Comment();
          $comment-&gt;setBody('My comment');
          $comment-&gt;setAuthor($commentAuthor);
          $comment-&gt;setPublicationDate(new \DateTime());
  
          $post-&gt;addComment($comment);
          $manager-&gt;persist($comment);
  
          $manager-&gt;flush();
      }
  }
  </code></pre>