I have a problem using Symfony 2. When I try to fill out a form with data from Entity, I get this error:
"It is expected that form view data will be of type scalar, array, or \ ArrayAccess instance, but it is an instance of the DateTime class. You can avoid this error by setting the" data_class "option to" DateTime "or by adding a view transformer that converts an instance of the DateTime class to scalar, array, or instance of \ ArrayAccess. "
I have little experience with Symfony 2. I use the YAML parameter to create objects.
Thanks.
Controller Code:
public function editAction($id) { $em = $this->getDoctrine()->getManager(); $candidate = $em->getRepository('OUProjectBundle:Candidate')->findOneById($id); $form = $this->createForm(new CandidateType(), $candidate); return $this->render('MyProjectBundle:Candidate:new.html.twig', array( 'form' => $form->createView(), )); }
CandidateType Class:
namespace My\ProjectBundle\Form; use Symfony\Component\Form\AbstractType; use My\ProjectBundle\Entity\Candidate; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; use Doctrine\ORM\EntityRepository; class CandidateType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('name', 'text', array('label'=>'Name:')); $builder->add('surname', 'text', array('label'=>'Surname:')); $builder->add('email', 'text', array('label'=>'Email:')); $builder->add('phone', 'text', array('label'=>'Telephone:')); $builder->add('DOB', 'text', array('label'=>'Date of Birth:')); $builder->add('address', 'text', array('label'=>'Address')); $builder->add('town', 'text', array('label'=>'Town:')); $builder->add('city', 'text', array('label'=>'City:')); $builder->add('post_code', 'text', array('label'=>'Post Code:')); $builder->add('file', 'file', array('label' => 'CV file (.docx)', 'required' => true)); } public function getName() { return 'candidate'; } public function getDefaultOptions(array $options) { return array( 'data_class' => 'My\ProjectBundle\Entity\Candidate', ); } }
Essence:
namespace OU\ProjectBundle\Entity; use Doctrine\ORM\Mapping as ORM; use My\ProjectBundle\Utils\DocReader; class Candidate { private $id; private $name; private $surname; private $dob; private $phone; private $address; private $town; private $city; private $post_code; private $cv_text; private $cv_file; private $createdAt; private $updatedAt; private $deletedAt; private $skill; public $file; public function __construct() { $this->skill = new \Doctrine\Common\Collections\ArrayCollection(); } public function getId() { return $this->id; } public function setName($name) { $this->name = $name; return $this; } public function getName() { return $this->name; } public function setSurname($surname) { $this->surname = $surname; return $this; } public function getSurname() { return $this->surname; } public function setDob($dob) { $this->dob = new \DateTime($dob); return $this; } public function getDob() { return $this->dob; } public function setPhone($phone) { $this->phone = $phone; return $this; } public function getPhone() { return $this->phone; } public function setAddress($address) { $this->address = $address; return $this; } public function getAddress() { return $this->address; } public function setTown($town) { $this->town = $town; return $this; } public function getTown() { return $this->town; } public function setCity($city) { $this->city = $city; return $this; } public function getCity() { return $this->city; } public function setPostCode($postCode) { $this->post_code = $postCode; return $this; } public function getPostCode() { return $this->post_code; } public function setCvText($cvText) { $this->cv_text = $cvText; return $this; } public function getCvText() { return $this->cv_text; } public function setCvFile($cvFile) { $this->cv_file = $cvFile; return $this; } public function getCvFile() { return $this->cv_file; } public function setCreatedAt($createdAt) { $this->createdAt = $createdAt; return $this; } public function getCreatedAt() { return $this->createdAt; } public function setUpdatedAt($updatedAt) { $this->updatedAt = $updatedAt; return $this; } public function getUpdatedAt() { return $this->updatedAt; } public function setDeletedAt($deletedAt) { $this->deletedAt = $deletedAt; return $this; } public function getDeletedAt() { return $this->deletedAt; } public function addSkill(\My\ProjectBundle\Entity\Skill $skill) { $this->skill[] = $skill; return $this; } public function removeSkill(\My\ProjectBundle\Entity\Skill $skill) { $this->skill->removeElement($skill); } public function getSkill() { return $this->skill; } public function preUpload() { if (null !== $this->file) {