title: Symfony Form Error
body: <p>I am new in symfony and i have this error :</p>
  
  <p>The form's view data is expected to be an instance of class AppBundle\Entity\Competences, but is an instance of class AppBundle\Entity\CompetenceTranslations. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms an instance of class AppBundle\Entity\CompetenceTranslations to an instance of AppBundle\Entity\Competences.</p>
  
  <p>Here is my code of competency entity and translations entity and form type entity.</p>
  
  <p><strong>Competency Admin</strong></p>
  
  <pre><code>&lt;?php
  
  namespace AppBundle\Admin;
  
  use Sonata\AdminBundle\Admin\AbstractAdmin;
  use Sonata\AdminBundle\Datagrid\DatagridMapper;
  use Sonata\AdminBundle\Datagrid\ListMapper;
  use Sonata\AdminBundle\Form\FormMapper;
  use Sonata\AdminBundle\Show\ShowMapper;
  use Symfony\Component\Form\Extension\Core\Type\FileType;
  use Sonata\AdminBundle\Form\Type\CollectionType;
  use AppBundle\Form\CompetencesType;
  use AppBundle\Form\CompetenceTranslationsType;
  use AppBundle\Entity\CompetenceTranslations;
  use AppBundle\Entity\User;
  use Symfony\Component\Security\Core\User\UserInterface;
  use Symfony\Component\HttpFoundation\Response;
  use Sonata\AdminBundle\Route\RouteCollection;
  use Sonata\AdminBundle\Admin\Admin;
  use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  use AppBundle\Entity\Competences;
  use Exporter\Source\DoctrineORMQuerySourceIterator;
  
  /**
   * Class CompetencyAdmin
   * @package AppBundle\Admin
   */
  class CompetencyAdmin extends AbstractAdmin
  {
  
      protected $datagridValues = array(
          '_sort_order' =&gt; 'DESC', // sort direction
          '_sort_by' =&gt; 'id' // field name
      );
  
      protected $perPageOptions = array(10, 20, 30, 40, 50);
  
      protected $maxPerPage = 10;
  
      public function getTemplate($name)
      {
          switch ($name) {
              case 'edit':
                  return 'AppBundle:CompetencyAdmin:edit.html.twig';
                  break;
  
              case 'list':
                  return 'AppBundle:CompetencyAdmin:list.html.twig';
                  break;
  
              default:
                  return parent::getTemplate($name);
                  break;
          }
      }
  
      public function configureRoutes(RouteCollection $collection)
      {
        $collection-&gt;remove('show');
      }
  
  
      public function prePersist($object) {
          parent::prePersist($object);
  
          $container = $this-&gt;getConfigurationPool()-&gt;getContainer();
          $entityManager = $container-&gt;get('doctrine.orm.entity_manager');
          $userId = $this-&gt;getConfigurationPool()-&gt;getContainer()-&gt;get('security.token_storage')-&gt;getToken()-&gt;getUser()-&gt;getId();
          $object-&gt;setStatus(1);
          $object-&gt;setIsMaster(1);
          $object-&gt;setCreatedBy($userId);
          $object-&gt;setUpdatedBy($userId);
          $entityManager-&gt;persist($object);
          return $object;
      }
  
       public function getExportFields() {
          parent::getExportFields();
          $fields = $this-&gt;getModelManager()-&gt;getExportFields($this-&gt;getClass());
          $fields1 = $this-&gt;getModelManager()-&gt;getExportFields(LeadingServiceTranslations::class);
          foreach ($fields1 as $f) 
          {
              if($f == 'name')
              {
                  $fields[] = $f;
              }
          }
          foreach ($this-&gt;getExtensions() as $extension) 
          {
              if (method_exists($extension, 'configureExportFields'))
              {
                  $fields = $extension-&gt;configureExportFields($this, $fields);
              }
          }
  
          unset($fields[1]);
          unset($fields[2]);
          unset($fields[3]);
          unset($fields[4]);
          unset($fields[5]);
          unset($fields[6]);
  
          return $fields;
      }
  
      /**
       * @inheritdoc
      */
      protected function configureFormFields(FormMapper $formMapper)
      {
          // add/edit
  
          $container = $this-&gt;getConfigurationPool()-&gt;getContainer();
          $entityManager = $container-&gt;get('doctrine.orm.entity_manager');
          $subject = $this-&gt;getSubject();
  //        dump($subject); 
          $translation_data = $subject-&gt;getTranslations();
          $lang_added = array();
          foreach($translation_data as $translations){
              $lang_added[] =  $translations-&gt;getLanguageCode();
          }
          $languages = $entityManager-&gt;getRepository('AppBundle:Language')-&gt;findBy(array('status' =&gt; 1));
          $userId = $this-&gt;getConfigurationPool()-&gt;getContainer()-&gt;get('security.token_storage')-&gt;getToken()-&gt;getUser()-&gt;getId();
          foreach($languages as $lang){
              if(!in_array($lang-&gt;getCode(), $lang_added)){
                  $competenceTranslations = new CompetenceTranslations();
                  $competenceTranslations-&gt;setLanguageCode($lang-&gt;getCode());
                  $competenceTranslations-&gt;setCreatedAt(new \DateTime());
                  $competenceTranslations-&gt;setUpdatedAt(new \DateTime());
                  $competenceTranslations-&gt;setCreatedBy($userId);
                  $competenceTranslations-&gt;setCompetences($subject);
                  $competenceTranslations-&gt;setUpdatedBy($userId);
                  $subject-&gt;addTranslations($competenceTranslations);
                  $entityManager-&gt;persist($competenceTranslations);
              }   
          }
          $formMapper
                  -&gt;add('translations', CollectionType::class,['entry_type' =&gt; CompetencesType::class,
                      'entry_options' =&gt; array('label' =&gt; false)]);
      }
  
  
      /**
       * @inheritdoc
       */
      protected function configureDatagridFilters(DatagridMapper $dataGridMapper)
      {   
          $dataGridMapper-&gt;add('translations.name','',array('label' =&gt; 'Name'));
      }
  
      /**
       * @inheritdoc
       */
      protected function configureListFields(ListMapper $listMapper)
      {
          $listMapper-&gt;add('name','')-&gt;add('_action', 'actions', [
                  'actions' =&gt; [
                      'edit' =&gt; [], 'delete' =&gt; [], 
                  ],
              ]);
      }
  
  
      public function createQuery($context = 'list')
      {
          $query = parent::createQuery($context);
          $query-&gt;andWhere($query-&gt;expr()-&gt;eq($query-&gt;getRootAlias() . '.isMaster', 1));
           return $query;
      }
  
      /**
       * @inheritdoc
       */
      protected function configureShowFields(ShowMapper $showMapper)
      {
          $showMapper-&gt;add('name');
  
      }
  
  
  
  }
  </code></pre>
  
  <p><strong>Competency</strong></p>
  
  <pre><code>&lt;?php
  
  namespace AppBundle\Entity;
  
  use Doctrine\Common\Collections\ArrayCollection;
  use Doctrine\ORM\Mapping as ORM;
  
  use Symfony\Component\Validator\Constraints as Assert;
  use Doctrine\Common\Collections\Criteria;
  /**
   * Competences
   *
   * @ORM\Table(name="competences")
   * @ORM\Entity(repositoryClass="AppBundle\Repository\CompetencesRepository")
   */
  class Competences
  {
      private $defaultLang = 'en';
  
      /**
       * @var int
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var int|null
       *
       * @ORM\Column(name="status", type="integer", length=11, nullable=true)
       */
      private $status;
  
      /**
       * @var int|null
       *
       * @ORM\Column(name="is_master", type="integer", length=11, nullable=true)
       */
      private $isMaster;
  
      /**
       * @var \DateTime|null
       *
       * @ORM\Column(name="created_at", type="datetime", nullable=true)
       */
      private $createdAt;
  
      /**
       * @var \DateTime|null
       *
       * @ORM\Column(name="updated_at", type="datetime", nullable=true)
       */
      private $updatedAt;
  
      /**
       * @var int|null
       *
       * @ORM\Column(name="created_by", type="integer", nullable=true)
       */
      private $createdBy;
  
      /**
       * @var int|null
       *
       * @ORM\Column(name="updated_by", type="integer", nullable=true)
       */
      private $updatedBy;
  
      /**
       * @var CompetenceTranslations[]
       * @ORM\OneToMany(targetEntity="CompetenceTranslations", mappedBy="competences")
       */
      private $translations;
  
      /**
       * @var MedicalProviderCompetence[]
       * @ORM\OneToMany(targetEntity="MedicalProviderCompetence", mappedBy="competences")
       */
      private $medicalProviderCompetence;
  
      public function __construct()
      {
          $this-&gt;medicalProviderCompetence = new ArrayCollection();
          $this-&gt;translations = new ArrayCollection();
      }
  
      /**
       * Get id.
       *
       * @return int
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      public function getMedicalProviderCompetence()
      {
          return $this-&gt;medicalProviderCompetence;
      }
  
      public function getTranslations()
      {
          return $this-&gt;translations;
      }
  
      /**
       * Add CompetenceTranslations
       *
       * @param CompetenceTranslations $translations
       *
       * @return Competences
       */
      public function addTranslations(CompetenceTranslations $translations)
      {
          $this-&gt;translations[] = $translations;
  
          return $this;
      }
  
      /**
       * Add CompetenceTranslations
       *
       * @param CompetenceTranslations $translations
       *
       * @return Competences
       */
      public function removeTranslations(CompetenceTranslations $translations)
      {
          $this-&gt;translations-&gt;removeElement($translations);
          $this-&gt;translations = new ArrayCollection(array_values($this-&gt;translations-&gt;toArray()));
      }
  
      /**
       * Remove CompetenceTranslations
       *
       * @param CompetenceTranslations $translations
       */
      public function setAllTranslations($translations)
      {
          $this-&gt;translations = $translations;
      }
  
       /**
       * @return string
       */
      public function __toString()
      {
          $default_string = 'null';
          $translationdata  = $this-&gt;getTranslations();
          //dump($translationdata); die;
          if($translationdata-&gt;count() &gt; 0)
          {
              $criteria = new Criteria();
              $criteria-&gt;where($criteria-&gt;expr()-&gt;contains('languageCode', $this-&gt;defaultLang));
              $default_lang_data = $translationdata-&gt;matching($criteria);
              if($default_lang_data-&gt;count() &gt; 0)
              {
                  $default_string = $default_lang_data-&gt;first()-&gt;getName();
  
              }
              else
              {
                  $default_string = '--NA--';
              }
          }
          else
          {
              $default_string = '--NA--';
          }
          return $default_string?$default_string:'--NA--';
      }
  
      public function getName(){
          return $this-&gt;__toString();
      }
  
      /**
       * Set status.
       *
       * @param int|null $status
       *
       * @return LeadingServices
       */
      public function setStatus($status = null)
      {
          $this-&gt;status = $status;
  
          return $this;
      }
  
      /**
       * Get status.
       *
       * @return tinyint|null
       */
      public function getStatus()
      {
          return $this-&gt;status;
      }
  
  
      /**
       * Set IsMaster.
       *
       * @param int|null $is_master
       *
       * @return LeadingServices
       */
      public function setIsMaster($isMaster = null)
      {
          $this-&gt;isMaster = $isMaster;
  
          return $this;
      }
  
      /**
       * Get IsMaster.
       *
       * @return int|null
       */
      public function getIsMaster()
      {
          return $this-&gt;isMaster;
      }
  
  
      /**
       * Set createdAt.
       *
       * @param \DateTime|null $createdAt
       *
       * @return Competences
       */
      public function setCreatedAt($createdAt = null)
      {
          $this-&gt;createdAt = $createdAt;
  
          return $this;
      }
  
      /**
       * Get createdAt.
       *
       * @return \DateTime|null
       */
      public function getCreatedAt()
      {
          return $this-&gt;createdAt;
      }
  
      /**
       * Set updatedAt.
       *
       * @param \DateTime|null $upadtedAt
       *
       * @return Competences
       */
      public function setUpdatedAt($updatedAt = null)
      {
          $this-&gt;updatedAt = $updatedAt;
  
          return $this;
      }
  
      /**
       * Get updatedAt.
       *
       * @return \DateTime|null
       */
      public function getUpdatedAt()
      {
          return $this-&gt;updatedAt;
      }
  
      /**
       * Set createdBy.
       *
       * @param int|null $createdBy
       *
       * @return Competences
       */
      public function setCreatedBy($createdBy = null)
      {
          $this-&gt;createdBy = $createdBy;
  
          return $this;
      }
  
      /**
       * Get createdBy.
       *
       * @return int|null
       */
      public function getCreatedBy()
      {
          return $this-&gt;createdBy;
      }
  
      /**
       * Set updatedBy.
       *
       * @param int|null $updatedBy
       *
       * @return Competences
       */
      public function setUpdatedBy($updatedBy = null)
      {
          $this-&gt;updatedBy = $updatedBy;
  
          return $this;
      }
  
      /**
       * Get updatedBy.
       *
       * @return int|null
       */
      public function getUpdatedBy()
      {
          return $this-&gt;modifiedBy;
      }
  }
  </code></pre>
  
  <p><strong>Competency Type</strong></p>
  
  <pre><code>&lt;?php
  
  namespace AppBundle\Form;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  use Symfony\Component\Form\Extension\Core\Type\FileType;
  use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolver;
  use Sonata\AdminBundle\Form\Type\CollectionType;
  use AppBundle\Form\CompetenceTranslationsType;
  
  /**
   * Class Competences
   * @package AppBundle\Form
   */
  class CompetencesType extends AbstractType
  {
      /**
       * {@inheritdoc}
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
                  -&gt;add('translations', CollectionType::class,['entry_type' =&gt; CompetenceTranslationsType::class,
                          'entry_options' =&gt; array('label' =&gt; false)])
                  -&gt;add('id','hidden');
      }
  
  
  
      /**
       * {@inheritdoc}
       */
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults(['data_class' =&gt; 'AppBundle\Entity\Competences']);
      }
  
      /**
       * {@inheritdoc}
       */
      public function getBlockPrefix()
      {
          return 'competences';
      }
  }
  </code></pre>
  
  <p>Please help me with this.</p>
  
  <p>Thanks in advance.</p>