我创建一个新对象并将其绑定到表单 . 用户填写表单并转到预览页面 . 我将用户响应存储在会话中 .
当用户返回编辑表单时,当我尝试从会话重新加载对象时,问题就出现了 . 我得到的实体传递给选择字段必须是托管错误 .
任何人都知道我可能会出错的地方?下面是控制器的代码 .
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(),
));
}
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';
}
}
继承人优惠券类型
5 回答
我遇到了同样的问题 - 我正在使用
getData()
从表单中检索数据并存储在会话中 . 稍后,在重定向之后,我试图使用setData()
重新填充同一表单的另一个实例 .我没有遇到本土字段的问题 . 但是,当我的表单包含一个实体时,我收到了相同的可怕消息“必须管理传递给选择字段的实体” .
经过一番头疼之后,这个问题显然非常简单(不是全部吗?) . 重定向后,实体已经脱离;解决方案只是使用
EntityManager::merge()
将实体重新包含到EntityManager中,从而将实体恢复为托管对象:)http://www.doctrine-project.org/api/orm/2.0/doctrine/orm/entitymanager.html
希望这可以帮助!
它's not relating to solve your specific problem but I' d喜欢注释:我遇到了同样的问题,并且可以通过 removing
'by_reference' => false
解决它,这里不必要以及这个错误的原因 .有同样的问题,几乎使用了Daggah的答案,但在实体数组中添加了一个小循环,检查对象:
希望这有助于某人 .
我有同样的问题,两个答案都非常有用,但我的问题涉及多维数组,所以为了保持动态,我使用了jahller函数的递归版本 .
希望这有助于某人 .
基于prev评论的更复杂的解决方案 . 支持ArrayCollections和DateTime对象