我创建一个新对象并将其绑定到表单.用户填写表单并转到预览页面.我将用户响应存储在会话中.
当用户返回编辑表单时,当我尝试从会话重新加载对象时,问题就出现了.我得到的实体传递给选择字段必须是托管错误.
任何人都知道我可能会出错的地方?下面是控制器的代码.
public function previewdealAction(Request $request){
$session = $this->getRequest()->getSession();
$coupon = $session->get('coupon');
$form = $this->createForm(new CouponType(), $coupon);
if ($request->getMethod() == 'POST') {
//bind the posted form values
$form->bindRequest($request);
//once a valid form is submitted ...
if ($form->isValid()){
//Proceed to Previewing deal
$file = $coupon->getImage();
$file->upload();
$session->set('coupon', $coupon);
$repository = $this->getDoctrine()
->getRepository('FrontendUserBundle:Coupon');
$coupons = $repository->findAll();
return $this->render('FrontendHomeBundle:Merchant:dealpreview.html.twig', array('coupon'=>$coupon, 'coupons'=>$coupons));
}
}
}
public function builddealAction(Request $request){
$em = $this->get('doctrine')->getEntityManager();
$user = $this->container->get('security.context')->getToken()->getUser();
//check for a coupon session variable
$session = $this->getRequest()->getSession();
$coupon = $session->get('coupon');
//If coupon is not set
if($coupon == NULL){
$coupon = new Coupon();
$date = new \DateTime(date("Y-m-d H:i:s"));
$coupon->setStartdate($date);
$coupon->setPosterid($user);
$session->set('coupon', $coupon);
}
$form = $this->createForm(new CouponType(), $coupon);
return $this->render('FrontendHomeBundle:Merchant:builddeal.html.twig', array(
'form' => $form->createView(),
));
}
Run Code Online (Sandbox Code Playgroud)
-
namespace Frontend\HomeBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class CouponType extends AbstractType {
public function buildForm(FormBuilder $builder, array $options) {
$builder->add('couponname', 'text');
$builder->add('description', 'textarea');
$builder->add('price', 'money', array('currency' => 'USD'));
$builder->add('originalprice', 'money', array('currency' => 'USD'));
$builder->add('maxlimit', 'integer');
$builder->add('maxper', 'integer');
$builder->add('startdate', 'date', array(
'years' => array(2011, 2012, 2013, 2014),
));
$builder->add('duration', 'choice', array(
'choices' => array(
'3' => 3,
'7' => 7,
'14' => 14,
'30' => 30,
'60' => 60,
'90' => 90,
),
'expanded' => false,
'multiple' => false,
));
$builder->add('expirationdate', 'choice', array(
'choices' => array(
'30' => 30,
'60' => 60,
'90' => 90,
'180' => 180,
),
'expanded' => false,
'multiple' => false,
));
$builder->add('tip', 'integer');
$builder->add('salestax', 'choice', array(
'choices' => array(
'included' => 'Sales tax is included and will be remitted BY YOU at the appropriate tax jurisdiction',
'exempt' => 'Sales tax is exempt according to seller\'s tax jurisdiction',
'collected' => 'Sales tax will be collected BY YOU at time of deal redemption',
),
'expanded' => true,
'multiple' => false,
));
$builder->add('signature', 'text');
$builder->add('city', 'entity', array(
'class' => 'Frontend\\UserBundle\\Entity\\Cities',
'expanded' => false,
'multiple' => false,
));
$builder->add('category', 'entity', array(
'class' => 'Frontend\\UserBundle\\Entity\\Category',
'expanded' => false,
'multiple' => false,
));
$builder->add('address', new AddressType());
$builder->add('image', new DocumentType());
$builder->add('maxper', 'choice', array(
'choices' => array(
'1' => 1,
'2' => 2,
'3' => 3,
'4' => 4,
'5' => 5,
'6' => 6,
'7' => 7,
'8' => 8,
'9' => 9,
'10' => 10,
),
'expanded' => false,
'multiple' => false,
));
}
public function getDefaultOptions(array $options) {
return array(
'data_class' => 'Frontend\UserBundle\Entity\Coupon',
);
}
public function getName()
{
return 'user';
}
}
Run Code Online (Sandbox Code Playgroud)
继承人优惠券类型
Dar*_*ght 28
我遇到了同样的问题 - 我正在使用getData()和存储在会话中从表单中检索数据.稍后,在重定向之后,我试图使用相应形式重新填充另一个实例setData().
我没有遇到本土字段的问题.但是,当我的表单包含一个实体时,我收到了相同的可怕消息"必须管理传递给选择字段的实体".
经过一番头疼之后,这个问题显然非常简单(不是全部吗?).重定向后,实体已经脱离; 解决方案只是将实体重新包含到EntityManager中EntityManager::merge(),从而将实体恢复为托管对象:)
// an array of form data from session
$entity = $data['my_entity'];
// merge() returns the managed entity
$entity = $this->getDoctrine()->getEntityManager()->merge($entity);
// update the form data array
$data['my_entity'] = $entity;
// Create form with form data
$form = $this->createForm(new MyFormType(), $data);
Run Code Online (Sandbox Code Playgroud)
http://www.doctrine-project.org/api/orm/2.0/doctrine/orm/entitymanager.html
希望这可以帮助!
Mar*_*hli 12
它与解决你的具体问题没有关系,但我想注释:我遇到了同样的问题,可以通过删除 'by_reference' => false这里不必要的问题以及出现此错误的原因来解决问题.
有同样的问题,几乎使用了Daggah的答案,但在实体数组中添加了一个小循环,检查对象:
if ($this->get('session')->has('filters')) {
$filters = $this->get('session')->get('filters');
foreach ($filters as $key => $filter) {
if (is_object($filter)) {
$filters[$key] = $em->merge($filter);
}
}
$filterForm = $this->createForm(new FilterType(), $filters);
}
Run Code Online (Sandbox Code Playgroud)
希望这有助于某人.