我有这个实体:
<?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(),
));
}
当我尝试插入新记录时,我收到了这个错误:
执行'INSERT INTO时发生异常 product_detail_has_category(category,detail)VALUES(?,?)'with 参数[7,2]:
SQLSTATE [23000]:完整性约束违规:1452无法添加或 更新子行:外键约束失败 (
product_detail_has_category
,CONSTRAINTfk_product_detail_has_category_product_detail1
外国钥匙 (detail
)参考product_detail
(id
)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和尝试创建关系,有什么不对?
答案 0 :(得分:0)
你可以:
在$category
的{{1}}字段集cascade={"PERSIST"}
上设置。
这不需要控制器本身,但这取决于你是否喜欢定义级联。
在持久@ManyToMany
之前手动Category对象。
ProductDetail
希望这有点帮助...
答案 1 :(得分: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查询错误,还必须将组重命名为kgroup !!