php - 传递给选择字段的实体必须被管理

标签 php symfony

我创建了一个新对象并将其绑定(bind)到一个表单。用户填写表单并转到预览页面。我将用户响应存储在 session 中。

当用户返回编辑表单时,当我尝试从 session 中重新加载对象时,问题就出现了。我得到:

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

有人知道我可能哪里出错了吗?这是 Controller 的代码。

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() 从表单中检索数据并存储在 session 中。后来,在重定向之后,我尝试使用 setData() 重新填充相同表单的另一个实例。

我在原生字段方面没有遇到任何问题。但是,当我的表单包含一个实体时,我收到了同样可怕的消息“必须管理传递到选择字段的实体”。

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

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

希望对您有所帮助!

关于php - 传递给选择字段的实体必须被管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7473872/

相关文章:

php - 使用 PHP Simple HTML DOM Parser 查找和删除 html 标签

php - Mysql 乘法错误

javascript - PHP 如果基于输入信息的函数不起作用

php - FOSOAuthServerBundle,授权 header 不起作用,但查询中的 access_token 起作用

javascript - symfony2 + Assets : how to restrict a css to a print when using {% javascripts %}

php - 多个查询返回错误为什么?

php - 发送带有服务器缓存文件的 header

javascript - 在JS中调用twig模板

php - Swiftmailer 在 'cc' 为空时提示

symfony - 选中表单类型复选框以创建表单 SonataAdminBundle