title: error upon flushing an object with array of objects in association
body: <p>i'm receiving the following error message upon persisting and flushing an object with its associations:</p>
  
  <pre><code>Catchable Fatal Error: Argument 1 passed to
  Doctrine\Common\Collections\ArrayCollection::__construct() must be of
  the type array, object given, called in  
  .../vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php on line 519 and defined in 
  .../vendor/doctrine/common/lib/Doctrine/Common/Collections/ArrayCollection.php line 48
  </code></pre>
  
  <p>what i have is a single table inheritance with this as the base object:</p>
  
  <pre><code>use Doctrine\ORM\Mapping as ORM;
  
  /**
   * ObjectData
   *
   * @ORM\Table(name="object_data")
   * @ORM\Entity(repositoryClass="Edexp\CoreBundle\Entity\ObjectDataRepository")
   * @ORM\InheritanceType("SINGLE_TABLE")
   * @ORM\DiscriminatorColumn(name="entity_name", type="string")
   * @ORM\DiscriminatorMap({
   *      "request" = "Edexp\MessageBundle\Entity\RequestData"
   * })
   * @ORM\HasLifecycleCallbacks
   */
  class ObjectData
  {
  /**
   * @var integer
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;
  
  /**
   * @var \DateTime
   *
   * @ORM\Column(name="created_at", type="datetime")
   */
  private $created_at;
  
  /**
   * @var User
   *
   * @ORM\OneToMany(targetEntity="User", mappedBy="data")
   */
  private $user;
  
  /**
   * @var string
   *
   * @ORM\Column(name="`key`", type="string", length=255)
   */
  private $key;
  
  /**
   * @var string
   *
   * @ORM\Column(name="data", type="blob")
   */
  private $data;
  
  /**
   * Get id
   *
   * @return integer
   */
  public function getId()
  {
      return $this-&gt;id;
  }
  
  /**
   * Set created_at
   *
   * @param \DateTime $createdAt
   * @return ObjectData
   */
  public function setCreatedAt($createdAt)
  {
      $this-&gt;created_at = $createdAt;
  
      return $this;
  }
  
  /**
   * Get created_at
   *
   * @return \DateTime
   */
  public function getCreatedAt()
  {
      return $this-&gt;created_at;
  }
  
  /**
   * Set user
   *
   * @param User $user
   * @return ObjectData
   */
  public function setUser($user)
  {
      $this-&gt;user = $user;
  
      return $this;
  }
  
  /**
   * Get user
   *
   * @return User
   */
  public function getUser()
  {
      return $this-&gt;user;
  }
  
  /**
   * Set key
   *
   * @param string $key
   * @return ObjectData
   */
  public function setKey($key)
  {
      $this-&gt;key = $key;
  
      return $this;
  }
  
  /**
   * Get key
   *
   * @return string
   */
  public function getKey()
  {
      return $this-&gt;key;
  }
  
  /**
   * Set data
   *
   * @param string $data
   * @return ObjectData
   */
  public function setData($data)
  {
      $this-&gt;data = $data;
  
      return $this;
  }
  
  /**
   * Get data
   *
   * @return string
   */
  public function getData()
  {
      return $this-&gt;data;
  }
  
  /**
   * @ORM\PrePersist
   */
  public function prepareForPersist()
  {
      $this-&gt;created_at = new \DateTime();
  }
  }
  </code></pre>
  
  <p>and an additional object that inherits from ObjectData:</p>
  
  <pre><code>use Doctrine\ORM\Mapping as ORM;
  
  use MyProject\CoreBundle\Entity\ObjectData;
  
  /**
  * RequestData
  *
  * @ORM\Entity()
  */
  class RequestData extends ObjectData
  {
      /**
      * var Request $request
      *
      * @ORM\ManyToOne(targetEntity="Request", inversedBy="data")
      * @ORM\JoinColumn(name="object_id", referencedColumnName="id")
      */
      private $request;
  
      /**
      * Set request
      *
      * @param Request $request
      * @return RequestData
      */
      public function setRequest($request)
      {
          $this-&gt;request = $request;
          return $this;
      }
  
      /**
      * Get request
      *
      * @return Request
      */
      public function getRequest()
      {
          return $this-&gt;request;
      }
  }
  </code></pre>
  
  <p>and here's the object that utilizes the RequestData entity:</p>
  
  <pre><code>use Doctrine\Common\Collections\ArrayCollection;
  use Doctrine\ORM\Mapping as ORM;
  
  /**
  * Request
  *
  * @ORM\Table(name="requests")
  * @ORM\Entity(repositoryClass="RequestRepository")
  * @ORM\HasLifecycleCallbacks()
  */
  class Request
  {
      /**
      * @var integer $id
      *
      * @ORM\Column(name="id", type="integer")
      * @ORM\Id
      * @ORM\GeneratedValue(strategy="AUTO")
      */
      private $id;
  
      /**
      * @var Message $message
      *
      * @ORM\ManyToOne(targetEntity="Message", inversedBy="requests")
      */
      private $message;
  
      /**
      * @var RequestType $type
      *
      * @ORM\ManyToOne(targetEntity="RequestType")
      * @ORM\JoinColumn(name="type_id", referencedColumnName="id")
      */
      private $type;
  
      /**
      * @var ArrayCollection $data
      *
      * @ORM\OneToMany(
      *       targetEntity="RequestData",
      *       mappedBy="request",
      *       cascade={"persist","remove"}
      * )
      */
      private $data;
  
      /**
      * @var datetime $created_at
      *
      * @ORM\Column(name="created_at", type="datetime")
      */
      private $created_at;
  
      /**
      * @var text $comment
      *
      * @ORM\Column(name="comment", type="text")
      */
      private $comment;
  
      /**
      * @var ArrayCollection $responses
      *
      * @ORM\OneToMany(
      *       targetEntity="Response",
      *       mappedBy="request",
      *       cascade={"persist","remove"}
      * )
      */
      private $responses;
  
      public function __construct()
      {
          $this-&gt;data      = new ArrayCollection();
          $this-&gt;responses = new ArrayCollection();
      }
  
      /**
      * Get id
      *
      * @return integer
      */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
      * Set message
      *
      * @param Message $message
      */
      public function setMessage($message)
      {
          $this-&gt;message = $message;
      }
  
      /**
      * Get message
      *
      * @return Message
      */
      public function getMessage()
      {
          return $this-&gt;message;
      }
  
      /**
      * Set type
      *
      * @param RequestType $type
      */
      public function setType($type)
      {
          $this-&gt;type = $type;
      }
  
      /**
      * Get type
      *
      * @return RequestType
      */
      public function getType()
      {
          return $this-&gt;type;
      }
  
      /**
      * Set created_at
      *
      * @param datetime $createdAt
      */
      public function setCreatedAt($createdAt)
      {
          $this-&gt;created_at = $createdAt;
      }
  
      /**
      * Get created_at
      *
      * @return datetime
      */
      public function getCreatedAt()
      {
          return $this-&gt;created_at;
      }
  
      /**
      * Set comment
      *
      * @param text $comment
      */
      public function setComment($comment)
      {
          $this-&gt;comment = $comment;
      }
  
      /**
      * Get comment
      *
      * @return text
      */
      public function getComment()
      {
          return $this-&gt;comment;
      }
  
      /**
      * Add response
      *
      * @param Response $response
      */
      public function addResponse($response)
      {
          foreach ( $this-&gt;responses as $r ) {
              if ( $r-&gt;getUser() == $response-&gt;getUser() )
                  return;
          }
  
          $this-&gt;responses-&gt;add($response);
      }
  
      /**
      * Get responses
      *
      * @return ArrayCollection
      */
      public function getResponses()
      {
          return $this-&gt;responses;
      }
  
      public function hasResponse($user)
      {
          foreach ( $this-&gt;responses as $r ) {
              if ( $r-&gt;getUser() == $user )
                  return $r;
          }
          return null;
      }
  
      public function addData($data)
      {
          $data-&gt;setRequest($this);
          $this-&gt;data-&gt;add($data);
      }
  
      public function getData()
      {
          return $this-&gt;data;
      }
  
      /**
      * @ORM\PrePersist
      */
      public function prePersist()
      {
          $this-&gt;created_at = new \DateTime();
      }
  }
  </code></pre>
  
  <p>and finally the code that results in an error:</p>
  
  <pre><code>            use MyProject\MessageBundle\Entity\Request as MyRequest;
  
      $data = new RequestData();
      $data-&gt;setUser($user);
      $data-&gt;setKey('user_id');
      $data-&gt;setData(6);
  
      $req = new MyRequest();
      $req-&gt;setMessage($message);
      $req-&gt;setComment('bla');
      $req-&gt;setType($doctrine-&gt;getRepository('MessageBundle:RequestType')-&gt;find(1));
      $req-&gt;addData($data);
  
      $em-&gt;persist($req);
      $em-&gt;flush();
  </code></pre>
  
  <p>any suggestions as to what might be wrong here?</p>
  
  <p>cheers</p>