我只用了几天symfony 2。我有两个实体,我想创建一个表单,并将该表单中的数据保存到数据库中。UserDetails和Contract具有OneToOne关系。我在UserDetails表单中嵌入了一个Contract表单(web上的表单apper),但当我在表单中设置了一些数据并点击"保存"按钮时,我收到了一个错误。当我注意到"尝试"分配一个数组而不是Contract时,我不知道如何在Controler中访问这个新的Contract实体。
错误示例:
ContextErrorException:可捕获的致命错误:传递给Leave\DatabaseBundle\Entity\UserDetails::setContract()的参数1必须是Leave\DDatabaseBundle\Entity\Contract的实例,给定数组,在第360行的/var/www/nowyUrlop/vendor/symfony/symfony/src/symfony/Component/PropertyAccess/PropertyAccessor.php中调用,并在/var/www/nowyUrlop/src/Leave/DatabaseBundle/Entity/UserDetails.php第165行中定义
在/var/www/nowyUrlop/vendor/symfony/symfoy/src/symfony/Component/PropertyAccess/PropertyAccessor.php行中的UserDetails->setContract
UserDetails实体:
use DoctrineORMMapping as ORM;
use DoctrineCommonAnnotationsAnnotationReader;
use LeaveDatabaseBundleEntityUser;
/**
* @ORMEntity
* @ORMTable(name="userDetails")
*/
class UserDetails {
/**
* @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORMOneToOne(targetEntity="Contract", mappedBy="user_details", cascade={"persist"})
*/
protected $contract;
/**
* @ORMOneToOne(targetEntity="User", inversedBy="userDetails")
* @ORMJoinColumn(name="user_id", nullable = true, referencedColumnName="id")
* */
protected $user;
合同主体:
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
/**
* @ORMEntity
* @ORMTable(name="contract")
*/
class Contract {
/**
* @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORMColumn(type="date")
*/
protected $start_contr;
/**
* @ORMColumn(type="date")
*/
protected $end_contr;
/**
* @ORMColumn(type="integer")
*/
protected $type ;
/**
* @ORMColumn(type="integer")
*/
protected $hours_per_week;
/**
* @ORMOneToOne(targetEntity="UserDetails", inversedBy="contract")
* @ORMJoinColumn(name="user_details_id", nullable = true, referencedColumnName="id")
* */
protected $user_details;
public function setUserDetails(LeaveDatabaseBundleEntityUserDetails $userDetails = null)
{
$this->user_details = $userDetails;
return $this;
}
/**
* Get user_details
*
* @return LeaveDatabaseBundleEntityUserDetails
*/
public function getUserDetails()
{
return $this->user_details;
}
UserDetals表单:
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use LeaveDatabaseBundleFormTypeContractFormType;
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 SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
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()
));
}
您必须将data_class作为第二个参数传递。
在您的UserDetals表单中:执行以下
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use LeaveDatabaseBundleFormTypeContractFormType;
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' => 'LeaveDatabaseBundleEntityContract')
)
->add('save', 'submit');
}
public function getName()
{
return 'userDetails';
}
}