传递到选择字段的实体必须进行管理 [英] Entities passed to the choice field must be managed

查看:66
本文介绍了传递到选择字段的实体必须进行管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个新对象并将其绑定到表单.用户填写表格并进入预览页面.我将用户响应存储在会话中.

I create a new object and bind it to a form. The user fills out the form and goes to a preview page. I store the user responses in the session.

当用户回去编辑表单时,我尝试从会话中重新加载对象时,问题出现了.我得到了:

The problem crops up when I try to reload the object from the session when the user goes back to edit the form. I get :

错误:必须管理传递给选择字段的实体.

Error: the Entities passed to the choice field must be managed.

任何人都知道我可能要去哪里了吗?这是控制器的代码.

Anyone have an idea of where i might be going wrong? Heres the code for the controllers.

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';
}

}

这是优惠券类型类别

推荐答案

我遇到了同样的问题-我正在使用getData()从表单中检索数据并将其存储在会话中.稍后,在重定向之后,我试图使用setData()重新填充相同格式的另一个实例.

I was experiencing the same problem - I was retrieving data from a form using getData() and storing in the session. Later on, after a redirect, I was attempting to repopulate another instance of the same form using setData().

我对本机字段没有任何问题.但是,当我的表单包含一个实体时,我会收到相同的可怕消息必须管理传递给选择字段的实体".

I experienced no issues with native fields. However, when my form included an entity I was receiving the same dreaded message "Entities passed to the choice field must be managed".

经过一番摸索之后,这个问题表明它非常简单(不是全部吗?).重定向后,实体已分离;解决方案就是使用EntityManager::merge()将实体重新包含到EntityManager中,从而将实体恢复为托管对象:)

After some head scratching the issue revealed itself to be pretty straightforward (aren't they all?). After a redirect, the entity has become detached; the solution is simply to re-include the entity into the EntityManager using EntityManager::merge(), thus reinstating the entity as a managed object :)

// 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);

http://www.doctrine-project.org/api/orm /2.0/doctrine/orm/entitymanager.html

希望这会有所帮助!

这篇关于传递到选择字段的实体必须进行管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆