这里我的代码有两个简单的类,当我试图通过这个插入数据时。我可以得到一些错误。
头等舱
/**
* @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\vendor\doctrine\orm\lib\Doctrine\ORM\ORMInvalidArgumentException.php:91 堆栈跟踪:#0 C:\xampp\htdocs\dpf\vendor\doctrine\orm\lib\Doctrine \ORM\UnitOfWork。