title: Cannot set form with populated Entity object get The form's view data is expected to be of type scalar, array or an instance of \ArrayAccess
body: <p>I have created the EntityClass:</p>
  
  <pre><code>&lt;?php
  
  namespace Rota\Bundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\component\OptionsResolver\OptionsResolverIntrerface;
  use Doctrine\Common\Collections\ArrayCollection;
  
  /**
  * RotaObj
  *
  * @ORM\Table()
  * @ORM\Entity
  */
  class RotaMain
  {
     public function __construct()
   {
       $this-&gt;rotaShifts = new ArrayCollection();
   }
  
  /**
   * @var integer
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;
  
  /**
   * @var integer
   *
   * @ORM\Column(name="MyUserId", type="integer")
   */
  private $myUserId;
  
  /**
   * @var date
   * 
   * @ORM\Column(name="RotaStartDate", type="date")
   */
  private $startDate;
  
  /**
   * @var date
   * 
   * @ORM\Column(name="RotaReviewDate", type="date")
   */
  private $reviewDate;
  
  /**
   * @var integer
   * 
   * @ORM\Column(name="RotaRollingWeeks", type="integer")
   */
   private $rollingWeeks;
  
   /**
    * @var integer
    * 
    * @ORM\Column(name="restDayCount", type="integer")
    */
   private $restDayCount;
  
   /**
    * @var integer
    * 
    * @ORM\Column(name="totalDayCount", type="integer")
    */
   private $totalDayCount;
  
  
   /**
    * @ORM\OneToMany(targetEntity="RotaShift",mappedBy="rotaMain",cascade={"persist"})
    */
   protected $rotaShifts;
  
   /**
    * @var string
    * 
    * @ORM\Column(name="shiftOrder",type="string",length=4000,nullable=true)
    */
   private $shiftOrder;
  
  /**
   * Get id
   *
   * @return integer 
   */
  public function getId()
  {
      return $this-&gt;id;
  }
  
  /**
   * Set myUserId
   *
   * @param integer $myUserId
   * @return RotaObj
   */
  public function setMyUserId($myUserId)
  {
      $this-&gt;myUserId = $myUserId;
  
      return $this;
  }
  
  /**
   * Get myUserId
   *
   * @return integer 
   */
  public function getMyUserId()
  {
      return $this-&gt;myUserId;
  }
  
  /**
   * Set startDate
   *
   * @param \DateTime $startDate
   * @return RotaObj
   */
  public function setStartDate($startDate)
  {
      $this-&gt;startDate = $startDate;
  
      return $this;
  }
  
  /**
   * Get startDate
   *
   * @return \DateTime 
   */
  public function getStartDate()
  {
      return $this-&gt;startDate;
  }
  
  /**
   * Set reviewDate
   *
   * @param \DateTime $reviewDate
   * @return RotaObj
   */
  public function setReviewDate($reviewDate)
  {
      $this-&gt;reviewDate = $reviewDate;
  
      return $this;
  }
  
  /**
   * Get reviewDate
   *
   * @return \DateTime 
   */
  public function getReviewDate()
  {
      return $this-&gt;reviewDate;
  }
  
  /**
   * Set rollingWeeks
   *
   * @param integer $rollingWeeks
   * @return RotaObj
   */
  public function setRollingWeeks($rollingWeeks)
  {
      $this-&gt;rollingWeeks = $rollingWeeks;
  
      return $this;
  }
  
  /**
   * Get rollingWeeks
   *
   * @return integer 
   */
  public function getRollingWeeks()
  {
      return $this-&gt;rollingWeeks;
  }
  
  /**
   * Add rotaShifts
   *
   * @param \Rota\Bundle\Entity\RotaShift $rotaShifts
   * @return RotaMain
   */
  public function addRotaShift(\Rota\Bundle\Entity\RotaShift $rotaShift)
  {
      $this-&gt;rotaShifts[] = $rotaShift;
  
      return $this;
  }
  
  /**
   * Remove rotaShifts
   *
   * @param \Rota\Bundle\Entity\RotaShift $rotaShifts
   */
  public function removeRotaShift(\Rota\Bundle\Entity\RotaShift $rotaShifts)
  {
      $this-&gt;rotaShifts-&gt;removeElement($rotaShifts);
  }
  
  /**
   * Get rotaShifts
   *
   * @return \Doctrine\Common\Collections\Collection 
   */
  public function getRotaShifts()
  {
      return $this-&gt;rotaShifts;
  }
  
  
  
  /**
   * Set restDayCount
   *
   * @param integer $restDayCount
   * @return RotaMain
   */
  public function setRestDayCount($restDayCount)
  {
      $this-&gt;restDayCount = $restDayCount;
  
      return $this;
  }
  
  /**
   * Get restDayCount
   *
   * @return integer 
   */
  public function getRestDayCount()
  {
      return $this-&gt;restDayCount;
  }
  
  /**
   * Set totalDayCount
   *
   * @param integer $totalDayCount
   * @return RotaMain
   */
  public function setTotalDayCount($totalDayCount)
  {
      $this-&gt;totalDayCount = $totalDayCount;
  
      return $this;
  }
  
  /**
   * Get totalDayCount
   *
   * @return integer 
   */
  public function getTotalDayCount()
  {
      return $this-&gt;totalDayCount;
  }
  
  
  
  /**
   * Set shiftOrder
   *
   * @param string $shiftOrder
   * @return RotaMain
   */
  public function setShiftOrder($shiftOrder)
  {
      $this-&gt;shiftOrder = $shiftOrder;
  
      return $this;
  }
  
  /**
   * Get shiftOrder
   *
   * @return string 
   */
  public function getShiftOrder()
  {
      return $this-&gt;shiftOrder;
  }
  }
  </code></pre>
  
  <p>This class holds collection of:</p>
  
  <pre><code>&lt;?php
  
  namespace Rota\Bundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
  * RotaShift
  * 
  * @ORM\Table()
  * @ORM\Entity
  */
  class RotaShift
  {
  /**
   * @var integer
   * 
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $_id;
  
  /**
   * @var integer
   * 
   * @ORM\Column(name="startTime", type="integer")
   */
  private $_startTime;
  
  /**
   * @var integer
   * 
   * @ORM\Column(name="endTime", type="integer")
   */
  private $endTime;
  
  /**
   * @var string
   * 
   * @ORM\Column(name="name", type="string", length=50)
   */
  private $name;
  
  
  /**
   * @ORM\ManyToOne(targetEntity="RotaMain", inversedBy="rotaShifts")
   */
  private $rotaMain;
  
  
  /**
   * Get _id
   *
   * @return integer 
   */
  public function getId()
  {
      return $this-&gt;_id;
  }
  
  /**
   * Set _startTime
   *
   * @param integer $startTime
   * @return RotaShift
   */
  public function setStartTime($startTime)
  {
      $this-&gt;_startTime = $startTime;
  
      return $this;
  }
  
  /**
   * Get _startTime
   *
   * @return integer 
   */
  public function getStartTime()
  {
      return $this-&gt;_startTime;
  }
  
  /**
   * Set endTime
   *
   * @param integer $endTime
   * @return RotaShift
   */
  public function setEndTime($endTime)
  {
      $this-&gt;endTime = $endTime;
  
      return $this;
  }
  
  /**
   * Get endTime
   *
   * @return integer 
   */
  public function getEndTime()
  {
      return $this-&gt;endTime;
  }
  
  /**
   * Set name
   *
   * @param string $name
   * @return RotaShift
   */
  public function setName($name)
  {
      $this-&gt;name = $name;
  
      return $this;
  }
  
  /**
   * Get name
   *
   * @return string 
   */
  public function getName()
  {
      return $this-&gt;name;
  }
  
  /**
   * Set rotaMain
   *
   * @param \Rota\Bundle\Entity\RotaMain $rotaMain
   * @return RotaShift
   */
  public function setRotaMain(\Rota\Bundle\Entity\RotaMain $rotaMain = null)
  {
      $this-&gt;rotaMain = $rotaMain;
  
      return $this;
  }
  
  /**
   * Get rotaMain
   *
   * @return \Rota\Bundle\Entity\RotaMain 
   */
  public function getRotaMain()
  {
      return $this-&gt;rotaMain;
  }
  }
  </code></pre>
  
  <p>I have then created a Form EntityType class for the RotaMain object as follows:</p>
  
  <pre><code>&lt;?php
  
  namespace Rota\Bundle\Form\Type;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  
  class RotaMainType extends AbstractType
  {
     public function buildForm(FormBuilderInterface $builder, array $options)
     {
      $builder-&gt;  add('ReviewDate','date');
      $builder-&gt;add('RollingWeeks','integer');
      $builder-&gt;add('StartDate','date');
      $builder-&gt;add('RestDayCount','integer',array('label' =&gt; 'Number of rest days'));
      $builder-&gt;add('TotalDayCount','integer',array('label' =&gt; 'Total number of days in     shift pattern'));
      $builder-&gt;add('rotaShifts','collection',array('type'=&gt; new RotaShiftType(),'label' =&gt; false,'allow_add' =&gt; true,));
      $builder-&gt;add('Save','submit');
  }
  
  public function setDefaultOptions(OptionsResolverInterface $resolver)
  {
       $resolver-&gt;setDefaults(array(
      'data_class' =&gt; 'Rota\Bundle\Entity\RotaMain',
  ));
  }
  
  
  public function getName()
  {
      return 'rotaMain';
  }
  }
  </code></pre>
  
  <p>When I try to set a form with a fully populated RotaMain object:</p>
  
  <pre><code>$form = $this-&gt;createForm(new RotaMainType(),$RotaMain);
  </code></pre>
  
  <p>I get the following error:</p>
  
  <blockquote>
    <p>The form's view data is expected to be of type scalar, array or an instance of 
    \ArrayAccess, but is an instance of class Rota\Bundle\Entity\RotaMain. You can avoid 
    this error by setting the "data_class" option to "Rota\Bundle\Entity\RotaMain" or by 
    adding a view transformer that transforms an instance of class 
    Rota\Bundle\Entity\RotaMain to scalar, array or an instance of \ArrayAccess.</p>
  </blockquote>
  
  <p>I have set the data_class in the RotaMainType object so am not sure where I am going wrong? If I pass an empty RotaMain object to the form it builds as expected.</p>
  
  <p>The population of RotaMain happens in the controller. The form is part one of two part process. If user has completed first part gone away and come back I want reutnr the first part fo them to review before going onto the second part of the process. Controller action:</p>
  
  <pre><code>public function setRotaAction(Request $request,$userId)
  {
      //Check to see if user has part set up a rota - return completed form for review
       $RotaMainRepos = $this-&gt;getDoctrine()-&gt;getManager()-&gt;getRepository("RotaBundle:RotaMain");
       $id = $this-&gt;getUser()-&gt;getID();
       $criteria = array("myUserId"=&gt;$id);
       $result = $RotaMainRepos-&gt;findBy($criteria);
       if($result == null)
       {
         $RotaMain = new RotaMain();
         $RotaMain-&gt;setmyUserId($userId);
         $form = $this-&gt;createForm(new RotaMainType(),$RotaMain);
       }
       else
       {
          $RotaMain = $result[0];
          $rotashiftRepo = $this-&gt;getDoctrine()-&gt;getManager()-&gt;getRepository("RotaBundle:RotaShift");
          $rotaShifts = $rotashiftRepo-&gt;findByRotaMain($RotaMain);
          foreach($rotaShifts as $rotaShift)
          {
              $RotaMain-&gt;addRotaShift($rotaShift);
          }
          $form = $this-&gt;createForm(new RotaMainType(),$RotaMain);
  
       }
  
  
       $form-&gt;handleRequest($request);
  
       if($form-&gt;isValid())
       {
          $early = $form-&gt;getData();
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $em-&gt;persist($early);
  
          $shifts = $early-&gt;getRotaShifts();
          foreach($shifts as $shift)
          {
           $shift-&gt;setRotaMain($early);
           $em-&gt;persist($shift);
          }
          $em-&gt;flush();
          $shiftOrder = new ShiftOrder($shifts,$early-&gt;getRestDayCount(),$early-&gt;getTotalDayCount());
          return $this-&gt;render("RotaBundle:Default:shiftOrder.html.twig",array("shiftOrder" =&gt; $shiftOrder));
       }
  
       return $this-&gt;render('RotaBundle:Default:setRota.html.twig',array("form" =&gt; $form-&gt;createView(),));
  
  
  
  
  }
  </code></pre>