I used symfony 2 only few days. I got two entities and i want to create one form and save data from this form into database. UserDetails and Contract have relations OneToOne. I embed a form of Contract into UserDetails form (form apper on web) but when i set some data into form and click button "save" i get a error. as i notice a "try" to assign an array instead of Contract i don't know how to access this new contract entite in Controler.
example ERRORS:
ContextErrorException: Catchable Fatal Error: Argument 1 passed to Leave\DatabaseBundle\Entity\UserDetails::setContract() must be an instance of Leave\DatabaseBundle\Entity\Contract, array given, called in /var/www/nowyUrlop/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php on line 360 and defined in /var/www/nowyUrlop/src/Leave/DatabaseBundle/Entity/UserDetails.php line 165
at UserDetails->setContract(array('start_contr' => object(DateTime), 'end_contr' => object(DateTime), 'hours_per_week' => '6')) in /var/www/nowyUrlop/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php line 360
UserDetails entity:
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Annotations\AnnotationReader;
use Leave\DatabaseBundle\Entity\User;
/**
* @ORM\Entity
* @ORM\Table(name="userDetails")
*/
class UserDetails {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToOne(targetEntity="Contract", mappedBy="user_details", cascade={"persist"})
*/
protected $contract;
/**
* @ORM\OneToOne(targetEntity="User", inversedBy="userDetails")
* @ORM\JoinColumn(name="user_id", nullable = true, referencedColumnName="id")
* */
protected $user;
Contract Entity:
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
* @ORM\Table(name="contract")
*/
class Contract {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="date")
*/
protected $start_contr;
/**
* @ORM\Column(type="date")
*/
protected $end_contr;
/**
* @ORM\Column(type="integer")
*/
protected $type ;
/**
* @ORM\Column(type="integer")
*/
protected $hours_per_week;
/**
* @ORM\OneToOne(targetEntity="UserDetails", inversedBy="contract")
* @ORM\JoinColumn(name="user_details_id", nullable = true, referencedColumnName="id")
* */
protected $user_details;
public function setUserDetails(\Leave\DatabaseBundle\Entity\UserDetails $userDetails = null)
{
$this->user_details = $userDetails;
return $this;
}
/**
* Get user_details
*
* @return \Leave\DatabaseBundle\Entity\UserDetails
*/
public function getUserDetails()
{
return $this->user_details;
}
UserDetals Form:
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Leave\DatabaseBundle\Form\Type\ContractFormType;
class UserDetailsFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('exp', 'text')
->add('total_leave', 'text')
->add('days_left', 'text')
->add('contract', new contractFormType())
->add('save', 'submit');
}
public function getName()
{
return 'userDetails';
}
}
Contract Form:
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class ContractFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('start_contr', 'date', array('widget' => 'single_text'))
->add('end_contr', 'date', array('widget' => 'single_text'))
->add('hours_per_week', 'text');
}
public function getName()
{
return 'contractForm';
}
}
Controller:
public function editUserAction(Request $request) {
$user = $this->get('security.context')->getToken()->getUser();
$userDetails = new UserDetails();
$form = $this->createForm(new UserDetailsFormType(), $userDetails);
$userDetails->setUser($user);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($userDetails);
$em->flush();
return $this->render('LeaveEmployeeBundle:Employee:editUser.html.twig', array(
'formEditUser' => $form->createView(),
'userDetails' => $userDetails,
'user' => $user
));
}
return $this->render('LeaveEmployeeBundle:Employee:editUser.html.twig', array(
'formEditUser' => $form->createView()
));
}
You have to pass data_class as second parameter.
In your UserDetals Form: do the following
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Leave\DatabaseBundle\Form\Type\ContractFormType;
class UserDetailsFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('exp', 'text')
->add('total_leave', 'text')
->add('days_left', 'text')
->add('contract', new contractFormType(), array(
'data_class' => 'Leave\DatabaseBundle\Entity\Contract')
)
->add('save', 'submit');
}
public function getName()
{
return 'userDetails';
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With