我只用了几天symfony 2。 我有两个实体,我想创建一个表单并将此表单中的数据保存到数据库中。 UserDetails和Contract有关系OneToOne。 我将一种形式的契约嵌入到UserDetails表单中(在web上形成apper),但是当我将一些数据设置为表单并单击“保存”按钮时,我收到错误。因为我注意到“尝试”分配数组而不是合同我不知道如何在Controler中访问这个新合同。
示例错误:
ContextErrorException:Catchable Fatal Error:传递给Leave \ DatabaseBundle \ Entity \ UserDetails :: setContract()的参数1必须是Leave / DatabaseBundle \ Entity \ Contract的实例,给定数组,在/ var / www / nowyUrlop /中调用第360行的vendor / symfony / symfony / src / Symfony / Component / PropertyAccess / PropertyAccessor.php,在/var/www/nowyUrlop/src/Leave/DatabaseBundle/Entity/UserDetails.php第165行中定义
at UserDetails-> setContract(array('start_contr'=> object(DateTime),'end_contr'=> object(DateTime),'hours_per_week'=>'6'))/ var / www /nowyUrlop/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php第360行
UserDetails实体:
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;
合同实体:
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表格:
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';
}
}
合同表格:
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';
}
}
控制器:
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()
));
}
答案 0 :(得分:10)
您必须将data_class作为第二个参数传递。
在您的UserDetals表格中:执行以下操作
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';
}
}