0

我有这个实体:

<?php

namespace ProductBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use ProductBundle\DBAL\Types\StatusType;
use ProductBundle\DBAL\Types\FieldType;
use Fresh\Bundle\DoctrineEnumBundle\Validator\Constraints as DoctrineAssert;

/**
 * @ORM\Entity
 * @ORM\Table(name="product_detail")
 */
class ProductDetail {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="ProductDetail")
     * @ORM\JoinColumn(name="parent", referencedColumnName="id")
     */
    protected $parent;

    /**
     * @ORM\Column(type="string", length=255)
     */
    protected $description;

    /**
     * @ORM\Column(type="string", length=255)
     */
    protected $label;

    /**
     * @var string $field_type
     * @DoctrineAssert\Enum(entity="ProductBundle\DBAL\Types\FieldType")
     * @ORM\Column(name="field_type", type="FieldType", nullable=false)
     */
    protected $field_type;

    /**
     * @ORM\Column(name="values_text", type="string")
     */
    protected $values_text;

    /**
     * @ORM\Column(type="string", length=255)
     */
    protected $measure_unit;

    /**
     * @var string $status
     * @DoctrineAssert\Enum(entity="ProductBundle\DBAL\Types\StatusType")
     * @ORM\Column(name="status", type="StatusType", nullable=false)
     */
    protected $status;

    /**
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(name="created", type="datetime")
     */
    protected $created;

    /**
     * @Gedmo\Timestampable(on="update")
     * @ORM\Column(name="modified", type="datetime")
     */
    protected $modified;

    /**
     * @ORM\ManyToMany(targetEntity="CategoryBundle\Entity\Category", inversedBy="pd_detail")
     * @ORM\JoinTable(name="product_detail_has_category",
     *      joinColumns={@ORM\JoinColumn(name="category", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="detail", referencedColumnName="id")}
     *      )
     */
    protected $category;

    /**
     * @ORM\ManyToMany(targetEntity="ProductBundle\Entity\DetailGroup", inversedBy="productDetail")
     * @ORM\JoinTable(name="detail_group_has_product_detail",
     *      joinColumns={@ORM\JoinColumn(name="group", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="detail", referencedColumnName="id")}
     *      )
     */
    protected $detail_group;

    public function __construct() {
        $this->detail_group = new \Doctrine\Common\Collections\ArrayCollection();
        $this->category = new \Doctrine\Common\Collections\ArrayCollection();
    }

    public function getId() {
        return $this->id;
    }

    public function setParent(ProductDetail $parent = null) {
        $this->parent = $parent;
    }

    public function getParent() {
        return $this->parent;
    }

    public function setDescription($description) {
        $this->description = $description;
    }

    public function getDescription() {
        return $this->description;
    }

    public function setLabel($label) {
        $this->label = $label;
    }

    public function getLabel() {
        return $this->label;
    }

    public function setFieldType($field_type) {
        $this->field_type = $field_type;
    }

    public function getFieldType() {
        return $this->field_type;
    }

    public function setValuesText($values_text) {
        $this->values_text = $values_text;
    }

    public function getValuesText() {
        return $this->values_text;
    }

    public function setMeasureUnit($measure_unit) {
        $this->measure_unit = $measure_unit;
    }

    public function getMeasureUnit() {
        return $this->measure_unit;
    }

    public function setStatus($status) {
        $this->status = $status;
    }

    public function getStatus() {
        return $this->status;
    }

    public function setCreated($param) {
        $this->created = $param;
        return true;
    }

    public function getCreated() {
        return $this->created;
    }

    public function setModified($param) {
        $this->modified = $param;
        return true;
    }

    public function getModified() {
        return $this->modified;
    }

    public function setCategory(Category $category) {
        $this->category[] = $category;
    }

    public function getCategory() {
        return $this->category;
    }

    public function setDetailGroup(DetailGroup $detailGroup) {
        $this->detail_group[] = $detailGroup;
    }

    public function getDetailGroup() {
        return $this->detail_group;
    }

}

namespace ProductBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Fresh\Bundle\DoctrineEnumBundle\Validator\Constraints as DoctrineAssert;

/**
 * @ORM\Entity
 * @ORM\Table(name="detail_group")
 */
class DetailGroup {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="DetailGroup")
     * @ORM\JoinColumn(name="parent", referencedColumnName="id")
     */
    protected $parent;

    /**
     * @ORM\Column(type="string", length=255)
     */
    protected $name;

    /**
     * @ORM\Column(type="string", length=255)
     */
    protected $description;

    /**
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(name="created", type="datetime")
     */
    protected $created;

    /**
     * @Gedmo\Timestampable(on="update")
     * @ORM\Column(name="modified", type="datetime")
     */
    protected $modified;

    /**
     * @ORM\ManyToMany(targetEntity="ProductBundle\Entity\ProductDetail", mappedBy="detail_group", cascade={"all"})
     */
    protected $productDetail;

    public function __construct() {
        $this->productDetail = new \Doctrine\Common\Collections\ArrayCollection();
    }

    public function getId() {
        return $this->id;
    }

    public function setParent(DetailGroup $parent = null) {
        $this->parent = $parent;
    }

    public function getParent() {
        return $this->parent;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }

    public function setDescription($description) {
        $this->description = $description;
    }

    public function getDescription() {
        return $this->description;
    }

    public function setCreated($param) {
        $this->created = $param;
        return true;
    }

    public function getCreated() {
        return $this->created;
    }

    public function setModified($param) {
        $this->modified = $param;
        return true;
    }

    public function getModified() {
        return $this->modified;
    }

    public function setProductDetail(ProductDetail $productDetail) {
        $this->productDetail[] = $productDetail;
    }

    public function getProductDetail() {
        return $this->productDetail;
    }

}

这是我的ProductDetailType.php表格:

<?php

namespace ProductBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class ProductDetailType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
                ->add('category', 'entity', array('class' => 'CategoryBundle:Category', 'property' => 'name', 'required' => false, 'multiple' => true, 'expanded' => false))
                ->add('detail_group', 'entity', array('class' => 'ProductBundle:DetailGroup', 'property' => 'name', 'required' => false, 'multiple' => true, 'expanded' => false))
                ->add('parent', 'entity', array('class' => 'ProductBundle:ProductDetail', 'property' => 'label', 'required' => false))
                ->add('description', 'text', array('required' => false))
                ->add('label')
                ->add('field_type', 'choice', ['choices' => \ProductBundle\DBAL\Types\FieldType::getChoices()])
                ->add('values_text', 'collection', array('type' => 'text', 'allow_add' => true, 'allow_delete' => true, 'by_reference' => false))
                ->add('measure_unit', 'text', array('required' => false))
                ->add('status', 'choice', ['choices' => \ProductBundle\DBAL\Types\StatusType::getChoices()]);
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'ProductBundle\Entity\ProductDetail'
        ));
    }

    public function getName() {
        return 'prod_detail_create';
    }

}

这是我的createAction()

/**
 * Handle product details creation
 *
 * @Route("/product_detail/create", name="product_detail_create")
 * @Method("POST")
 * @Template("ProductBundle:ProductDetail:new.html.twig")
 */
public function createAction(Request $request) {
    $entity = new ProductDetail();
    $form = $this->createForm(new ProductDetailType(), $entity);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $entity->setValuesText(serialize($form->get('values_text')->getData()));
        $em->persist($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('product_detail_list'));
    }

    return $this->render('ProductBundle:ProductDetail:new.html.twig', array(
                'entity' => $entity,
                'form' => $form->createView(),
    ));
}

当我尝试插入新记录时,出现以下错误:

使用参数 [7, 2] 执行 'INSERT INTO product_detail_has_category (category, detail) VALUES (?, ?)' 时发生异常:

SQLSTATE [23000]:完整性约束违规:1452 无法添加或更新子行:外键约束失败(product_detail_has_category,CONSTRAINT fk_product_detail_has_category_product_detail1FOREIGN KEY(detail)REFERENCES product_detailid)ON UPDATE CASCADE)

我检查日志并看到:

DEBUG - "START TRANSACTION"
DEBUG - INSERT INTO product_detail (description, label, field_type, values_text, measure_unit, status, created, modified, parent) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
DEBUG - INSERT INTO product_detail_has_category (category, detail) VALUES (?, ?)
DEBUG - "ROLLBACK" 

我不知道为什么它不插入 product_detail 并尝试创建关系,有什么问题?

4

2 回答 2

0

我找到了错误所在,请参阅注释中的以下更改:

/**
 * @ORM\ManyToMany(targetEntity="CategoryBundle\Entity\Category", inversedBy="pd_detail", cascade={"persist"})
 * @ORM\JoinTable(name="product_detail_has_category",
 *      joinColumns={@ORM\JoinColumn(name="detail", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="category", referencedColumnName="id")}
 *      )
 */
protected $category;

/**
 * @ORM\ManyToMany(targetEntity="ProductBundle\Entity\DetailGroup", inversedBy="productDetail", cascade={"persist"})
 * @ORM\JoinTable(name="detail_group_has_product_detail",
 *      joinColumns={@ORM\JoinColumn(name="detail", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="kgroup", referencedColumnName="id")}
 *      )
 */
protected $detail_group; 

我已经反转了 joinColumns 和 inversedJoinColumns 中的关系并且一切正常,由于 MySQL 查询错误,还必须将 group 重命名为 kgroup !

于 2013-08-15T16:33:31.440 回答
0

您可以:

  • 设置在$category字段cascade={"PERSIST"}@ManyToMany

    这不需要对控制器本身进行任何更改,但这取决于您是否喜欢定义级联。

  • 在持久化之前Category手动持久化对象。ProductDetail

    $em = ...; // your entity manager
    
    // first, manually persist any category that is found
    foreach ( $productDetail->getCategory() as $c ){ 
        $em->persist($c);
    }
    
    // then, persist top-level object
    $em->persist($productDetail);
    
    // finally, flush everything
    $em->flush();
    

希望这个对你有帮助...

于 2013-08-15T08:14:34.390 回答