这里我的代码有两个简单的类,当我试图通过这个插入数据时。我可以得到一些错误..
头等舱
/**
* @Entity @Table(name="dpf_post_template")
*/
class DpfPostTemplate
{
/**
* @Id @GeneratedValue @Column(type="integer")
* @var string
*/
private $id;
/**
* @Column(type="string")
* @var string
*/
private $NAME;
/**
* @Column(type="datetime")
*/
private $CDATE;
/**
* @Column(type="datetime")
*/
private $UDATE;
/**
* @Column(type="string")
* @var string
*/
private $CBY;
/**
* @Column(type="string")
* @var string
*/
private $UBY;
/**
* @Column(type="integer")
* @var string
*/
private $ISACTIVE;
/**
* @OneToMany(targetEntity="DpfPostTemplateAssigned", mappedBy="assignedProperty")
* @var asProp[]
*/
private $assignedProperties = null;
public function __construct(){
$this->assignedProp = new ArrayCollection();
}
public function assignedProp($prop)
{
$this->assignedProperties[] = $prop;
}
public function getId(){
return $this->id;
}
public function getName(){
return $this->NAME;
}
public function setName($name){
$this->NAME = $name;
}
public function setCDate(DateTime $date){
$this->CDATE = $date;
}
public function setUDate(DateTime $date){
$this->UDATE = $date;
}
public function setIsActive($act){
$this->ISACTIVE = $act;
}
public function setCBy($cby){
$this->CBY = $cby;
}
public function setUBy($uby){
$this->UBY = $uby;
}
}
第二课
use Doctrine\Common\Collections\ArrayCollection;
/**
* @Entity @Table(name="dpf_post_template_assigned")
*/
class DpfPostTemplateAssigned
{
/**
* @Id @GeneratedValue @Column(type="integer")
* @var string
*/
private $id;
/**
* @Column(type="integer")
* @var string
*/
private $POSTID;
/**
* @Column(type="integer")
* @var string
*/
private $PROPID;
/**
* @Column(type="string")
* @var string
*/
private $TEMPLATE;
/**
* @Column(type="string")
* @var string
*/
private $NAME;
/**
* @Column(type="string")
* @var string
*/
private $SIZE;
/**
* @Column(type="integer")
* @var string
*/
private $ISACTIVE;
/**
* @ManyToOne(targetEntity="DpfPostTemplate", inversedBy="assignedProperties")
*/
protected $assignedProperty;
public function getId(){
return $this->id;
}
public function getName(){
return $this->NAME;
}
public function setName($name){
$this->NAME = $name;
}
public function setPost($post)
{
$post->assignedProp($this);
$this->assignedProperty = $post;
}
}
保存操作
$post = new DpfPostTemplate();
$post->setName("dinesh");
$post->setCDate(new DateTime("Now"));
$post->setUDate(new DateTime("Now"));
$post->setCBy("dinesh");
$post->setUBy("dinesh");
$post->setIsActive(1);
//$em->persist($post);
//$em->flush();
$prop = new DpfPostTemplateAssigned();
$prop->setName("dinesh");
$prop->setPost($post);
$em->persist($prop);
$em->flush();
运行此代码时,我会收到这样的错误.....
致命错误:未捕获的异常 “Doctrine \ ORM \ ORMInvalidArgumentException”,消息为“新实体” 通过这段关系找到了 'DpfPostTemplateAssigned#assignedProperty'未配置为 实体的级联持久化操作: DpfPostTemplate @ 00000000149c8a9f0000000036fa642b。要解决这个问题: 在这个未知实体上显式调用EntityManager#persist() 或者配置级联在映射中保持此关联 例如@ManyToOne(..,cascade = {“persist”})。如果你找不到 哪个实体导致问题实施 'DpfPostTemplate #__ toString()'得到一个线索。在 C:\ XAMPP \ htdocs中\ DPF \供应商\原则\ ORM \ LIB \原则\ ORM \ ORMInvalidArgumentException.php:91 堆栈跟踪:#0 C:\ XAMPP \ htdocs中\ DPF \厂商\教义\ ORM \ lib中\学说\ ORM \ UnitOfWork.php(797): 学说\ ORM \ ORMInvalidArgumentException :: newEntityFoundThroughRelationship(阵列, 对象(DpfPostTemplate))#1 C:\ XAMPP \ htdocs中\ DPF \厂商\教义\ ORM \ lib中\学说\ ORM \ UnitOfWork.php(679): Doctrine \ ORM \ UnitOfWork-> computeAssociationChanges in C:\ XAMPP \ htdocs中\ DPF \供应商\原则\ ORM \ LIB \原则\ ORM \ ORMInvalidArgumentException.php 在第91行
答案 0 :(得分:0)
学说告诉你,你必须做什么:
在此未知实体上显式调用EntityManager#persist()或在映射中配置级联持久保存此关联,例如@ManyToOne(..,cascade = {“persist”})
您的代码必须如下所示:
/**
* @ManyToOne(targetEntity="DpfPostTemplate", inversedBy="assignedProperties", cascade={"persist", "remove"})
*/
protected $assignedProperty;
请阅读此内容,以获取更多信息:Doctrine Docs