title: Symfony ArrayCollection vs PersistentCollection
body: <p>As I understood when you query database by repository you get PersistentCollection and when your working with your entities you get ArrayCollection.</p>
  
  <p>so consider I have one to many self referencing relation for my user entity.</p>
  
  <p>and in my user entity I have a setChildren method which get ArrayCollection of users as argument .</p>
  
  <pre><code>&lt;?php
  
  namespace UserBundle\Entity;
  
  use Abstracts\Entity\BaseModel;
  use CertificateBundle\Entity\Certificate;
  use Doctrine\Common\Collections\ArrayCollection;
  use Doctrine\ORM\Mapping as ORM;
  use EducationBundle\Entity\Education;
  use LanguageBundle\Entity\Language;
  use PostBundle\Entity\Post;
  use ProfileBundle\Entity\Company;
  use RoleBundle\Entity\Role;
  use SkillBundle\Entity\Skill;
  use Symfony\Component\Security\Core\User\UserInterface;
  use Symfony\Component\Validator\Constraints as Assert;
  use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  
  /**
   * User
   *
   * @ORM\Table(name="users")
   * @ORM\Entity(repositoryClass="UserBundle\Repository\Entity\UserRepository")
   * @UniqueEntity("email")
   * @UniqueEntity("username")
   */
  class User implements UserInterface
  {
  
  
      use BaseModel;
      /**
       * @var int
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
  
      /**
       * @var string
       *
       * @ORM\Column(name="type", type="string", columnDefinition="ENUM('merchant', 'company', 'customer') ")
       */
      private $type;
  
  
      /**
       * @ORM\Column(type="string", unique=true)
       * @Assert\NotBlank()
       */
      private $username;
  
      /**
       * @var string
       *
       * @ORM\Column(type="string", length=255)
       * @Assert\NotBlank()
       */
      private $email;
  
  
      /**
       * @var string
       * @ORM\Column(type="string", nullable=true)
       */
      private $avatar = null;
      /**
       * @var string
       * @ORM\Column(type="string", nullable=true)
       */
      private $cover = null;
  
  
      /**
       * @ORM\OneToMany(targetEntity="PostBundle\Entity\Post", mappedBy="user", orphanRemoval=true, cascade={"persist", "remove"})
       */
      private $posts;
  
      /**
       * @ORM\OneToMany(targetEntity="EducationBundle\Entity\Education" , mappedBy="user" , orphanRemoval=true, cascade={"persist", "remove"})
       */
      protected $educations;
  
  
      /**
       * @ORM\OneToMany(targetEntity="SkillBundle\Entity\SkillUser" , mappedBy="user" , orphanRemoval=true, cascade={"persist", "remove"})
       */
      protected $skills;
  
      /**
       * @ORM\OneToMany(targetEntity="LanguageBundle\Entity\LanguageUser" , mappedBy="user" , orphanRemoval=true, cascade={"persist", "remove"})
       */
      protected $languages;
  
  
      /**
       * @ORM\OneToMany(targetEntity="ResumeBundle\Entity\Resume" , mappedBy="user" , cascade={"all"})
       */
      protected $resumes;
  
  
      /**
       * @ORM\OneToMany(targetEntity="CertificateBundle\Entity\CertificateUser" , mappedBy="user" , orphanRemoval=true, cascade={"persist", "remove"})
       */
      protected $certificates;
  
  
      /**
       * @ORM\OneToOne(targetEntity="ProfileBundle\Entity\Company", mappedBy="user")
       */
      protected $company;
  
      /**
       * @ORM\OneToOne(targetEntity="ProfileBundle\Entity\Customer", mappedBy="user")
       */
      protected $customer;
  
      /**
       * @ORM\OneToOne(targetEntity="ProfileBundle\Entity\Merchant", mappedBy="user")
       */
      protected $merchant;
  
  
  
  
      /**
       * @var string
       * @Assert\NotBlank()
       * @Assert\Length(min=4)
       * @ORM\Column(name="password", type="string", length=255)
       *
       */
      private $password;
  
      /**
       * @ORM\ManyToMany(targetEntity="RoleBundle\Entity\Role", inversedBy="users", cascade={"persist"})
       * @ORM\JoinTable(name="user_role", joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}, inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")})
       */
      private $roles;
  
  
  
      /**
       * @ORM\ManyToOne(targetEntity="UserBundle\Entity\User", inversedBy="children")
       * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="SET NULL")
       */
      protected $parent;
  
  
      /**
       * @ORM\OneToMany(targetEntity="UserBundle\Entity\User", mappedBy="parent", orphanRemoval=true, cascade={"persist", "remove"})
       *
       */
      protected $children;
  
  
      /**
       * @var array
       */
      public static $fields = [ 'email', 'username', 'id', 'avatar', 'cover', 'type'];
  
      /**
       * User Entity constructor.
       */
      public function __construct(/*EncoderFactoryInterface $encoderFactory*/)
      {
          //$this-&gt;encoderFactory = $encoderFactory;
          $this-&gt;posts        = new ArrayCollection();
          $this-&gt;skills       = new ArrayCollection();
          $this-&gt;languages    = new ArrayCollection();
          $this-&gt;certificates = new ArrayCollection();
          $this-&gt;educations   = new ArrayCollection();
          $this-&gt;children     = new ArrayCollection();
  
          dump($this-&gt;children);
          die();
  
      }
  
  
  
  
      /**
       * @param User $user
       * @return $this
       */
      public function setParent(User $user)
      {
          $this-&gt;parent = $user;
  
          return $this;
      }
  
  
      /**
       * @return $this
       */
      public function removeParent()
      {
          $this-&gt;parent = null;
  
          return $this;
      }
  
  
  
      /**
       * @param User $user
       * @return $this
       */
      public function addChild(User $user)
      {
          if(!$this-&gt;children-&gt;contains($user)){
              $this-&gt;children-&gt;add($user);
          }
  
          return $this;
      }
  
  
      /**
       * @param User $user
       * @return bool
       */
      public function hasChild(User $user)
      {
          return $this-&gt;children-&gt;contains($user);
      }
  
  
  
      /**
       * @param User $user
       * @return bool
       */
      public function isChildOf(User $user)
      {
          return $user-&gt;getChildren()-&gt;contains($this);
      }
  
      /**
       * @return ArrayCollection
       */
      public function getChildren()
      {
          return $this-&gt;children;
      }
  
      /**
       * @param User $user
       * @return $this
       */
      public function removeChild(User $user)
      {
          if($this-&gt;children-&gt;contains($user)){
              $this-&gt;children-&gt;removeElement($user);
          }
  
          return $this;
      }
  
      /**
       * @param ArrayCollection $users
       * @return $this
       */
      public function setChildren(ArrayCollection $users)
      {
          $this-&gt;children = $users;
  
          return $this;
      }
  
  
      /**
       * @return $this
       */
      public function removeChildren()
      {
          $this-&gt;children-&gt;clear();
  
          return $this;
      }
  
  
      /**
       * @param ArrayCollection $certificates
       * @return $this
       */
      public function setCertificates(ArrayCollection $certificates)
      {
          $this-&gt;certificates = $certificates;
  
          return $this;
      }
  
  
      /**
       * @param Certificate $certificate
       * @return $this
       */
      public function addCertificate(Certificate $certificate)
      {
          if(!$this-&gt;certificates-&gt;contains($certificate))
              $this-&gt;certificates-&gt;add($certificate);
  
          return $this;
      }
  
      /**
       * @param Certificate $certificate
       * @return $this
       */
      public function removeCertificate(Certificate $certificate)
      {
          if($this-&gt;certificates-&gt;contains($certificate))
              $this-&gt;certificates-&gt;removeElement($certificate);
  
          return $this;
      }
  
      /**
       * @return $this
       */
      public function removeCertificates()
      {
          $this-&gt;certificates-&gt;clear();
  
          return $this;
      }
  
  
      /**
       * @param ArrayCollection $skills
       * @return $this
       */
      public function setSkills(ArrayCollection $skills)
      {
          $this-&gt;skills = $skills;
  
          return $this;
      }
  
  
      /**
       * @param Skill $skill
       * @return $this
       */
      public function addSkill(Skill $skill)
      {
          if(!$this-&gt;skills-&gt;contains($skill))
              $this-&gt;skills-&gt;add($skill);
  
          return $this;
      }
  
      /**
       * @param Skill $skill
       * @return $this
       */
      public function removeSkill(Skill $skill)
      {
          if($this-&gt;skills-&gt;contains($skill))
              $this-&gt;skills-&gt;removeElement($skill);
  
          return $this;
      }
  
      /**
       * @return $this
       */
      public function removeSkills()
      {
          $this-&gt;skills-&gt;clear();
  
          return $this;
      }
  
  
  
  
      /**
       * @param ArrayCollection $languages
       * @return $this
       */
      public function setLanguages(ArrayCollection $languages)
      {
          $this-&gt;languages = $languages;
  
          return $this;
      }
  
  
      /**
       * @param Language $language
       * @return $this
       */
      public function addLanguage(Language $language)
      {
          if(!$this-&gt;languages-&gt;contains($language))
              $this-&gt;languages-&gt;add($language);
  
          return $this;
      }
  
      /**
       * @param Language $language
       * @return $this
       */
      public function removeLanguage(Language $language)
      {
          if($this-&gt;languages-&gt;contains($language))
              $this-&gt;languages-&gt;removeElement($language);
  
          return $this;
      }
  
      /**
       * @return $this
       */
      public function removeLanguages()
      {
          $this-&gt;languages-&gt;clear();
  
          return $this;
      }
  
  
      /**
       * @param ArrayCollection $posts
       * @return $this
       */
      public function setPosts(ArrayCollection $posts)
      {
          $this-&gt;posts = $posts;
  
          return $this;
      }
  
      /**
       * @param Post $post
       * @return $this
       */
      public function addPost(Post $post)
      {
          $this-&gt;posts-&gt;add($post);
  
          return $this;
      }
  
      /**
       * @param Post $post
       * @return $this
       */
      public function removePost(Post $post)
      {
          $this-&gt;posts-&gt;removeElement($post);
  
          return $this;
      }
  
      /**
       * @return $this
       */
      public function removePosts()
      {
          $this-&gt;posts-&gt;clear();
  
          return $this;
      }
  
  
      /**
       * @param ArrayCollection $educations
       * @return $this
       */
      public function setEducations(ArrayCollection $educations)
      {
          $this-&gt;educations = $educations;
  
          return $this;
      }
  
      /**
       * @param Education $education
       * @return $this
       */
      public function addEducation(Education $education)
      {
          $this-&gt;educations-&gt;add($education);
  
          return $this;
      }
  
      /**
       * @param Education $education
       * @return $this
       */
      public function removeEducation(Education $education)
      {
          $this-&gt;educations-&gt;removeElement($education);
  
          return $this;
      }
  
      /**
       * @return $this
       */
      public function removeEducations()
      {
          $this-&gt;educations-&gt;clear();
  
          return $this;
      }
  
  
      /**
       * Get id
       *
       * @return integer
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * @param integer $id
       * @return $this
       */
      public function setId($id)
      {
          $this-&gt;id = $id;
  
          return $this;
      }
  
      /**
       * @return mixed
       */
      public function getType()
      {
          return $this-&gt;type;
      }
  
      /**
       * @param mixed $type
       * @return $this
       */
      public function setType($type)
      {
          $this-&gt;type = $type;
  
          return $this;
      }
  
  
      /**
       * Set email
       *
       * @param string $email
       * @return User
       */
      public function setEmail($email)
      {
          $this-&gt;email = $email;
  
          return $this;
      }
  
  
      /**
       * @return string
       */
      public function getEmail()
      {
          return $this-&gt;email;
      }
  
  
      /**
       * @param $username
       * @return $this
       */
      public function setUsername($username)
      {
          $this-&gt;username = $username;
          return $this;
      }
  
      /**
       * @return mixed
       */
      public function getUsername()
      {
          return $this-&gt;username;
      }
  
      /**
       * @return array
       */
      public function getRoles()
      {
          return ['ROLE_USER', 'IS_AUTHENTICATED_ANONYMOUSLY'];
      }
  
      /**
       * @param $password
       * @return $this
       */
      public function setPassword($password)
      {
          //$password =$this-&gt;encoderFactory-&gt;getEncoder($this)-&gt;encodePassword($password, $this-&gt;getSalt());
  
          $this-&gt;password = $password;
          return $this;
  
      }
  
      /**
       * @return string
       */
      public function getPassword()
      {
          return $this-&gt;password;
      }
  
      /**
       *
       */
      public function getSalt()
      {
          return md5(sha1('somesalt'));
      }
  
      /**
       *
       */
      public function eraseCredentials()
      {
      }
  
      /**
       * @param $cover
       * @return $this
       */
      public function setCover($cover)
      {
          $this-&gt;cover = $cover;
          return $this;
  
      }
  
      /**
       * @return string
       */
      public function getCover()
      {
          return $this-&gt;cover;
      }
  
      /**
       * @param $avatar
       * @return $this
       */
      public function setAvatar($avatar)
      {
          $this-&gt;avatar = $avatar;
          return $this;
  
      }
  
      /**
       * @return string
       */
      public function getAvatar()
      {
          return $this-&gt;avatar;
  
      }
  
  
      /**
       * @param Role $roles
       */
      public function addRoles(Role $roles)
      {
          $this-&gt;roles[] = $roles;
      }
  
      /**
       * @return mixed
       */
      public function getRoles2()
      {
          return $this-&gt;roles;
      }
  
      /**
       * @return array
       */
      public function getRolesAsArray()
      {
          $rolesArray = [];
          foreach ($this-&gt;getRoles2() as $role) {
              $rolesArray[] = $role-&gt;getName();
  
          }
          return $rolesArray;
  
      }
  
  
      /**
       * @return Company
       */
      public function getCompany()
      {
          return $this-&gt;company;
      }
  
      /**
       * @param Company $company
       * @return $this
       */
      public function setCompany(Company $company)
      {
          $this-&gt;company = $company;
  
          return $this;
      }
  
  }
  </code></pre>
  
  <p>and this is what I want to do </p>
  
  <pre><code>$new_owner = $this-&gt;userRepository-&gt;findOneById($user_id, false);
  
  $children = $old_owner-&gt;getChildren();
  
  $old_owner-&gt;removeChildren();
  $new_owner-&gt;setChildren($children);
  </code></pre>
  
  <p>and I get error which says :</p>
  
  <blockquote>
    <p>Argument 1 passed to
    Proxies__CG__\UserBundle\Entity\User::setChildren() must be an
    instance of Doctrine\Common\Collections\ArrayCollection, instance of
    Doctrine\ORM\PersistentCollection given</p>
  </blockquote>
  
  <p>should I change my type hint in setChildren method to PersistentCollection ??
  or I need to totally change my approach?</p>