0
我有这个类:prePersist检查空对象
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
class Parameter{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Project\Bundle\Entity\Anthropometry", inversedBy="parameter")
* @ORM\JoinColumn(name="anthropometry_id", referencedColumnName="id")
*
*/
protected $anthropometry;
/**
* @ORM\Column(name="data", type="string", length=255, nullable=true)
*/
protected $data;
...
}
这:
/**
* @ORM\Table(name="anthropometry")
* @ORM\Entity
*/
class Anthropometry {
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
*
* @ORM\OneToMany(targetEntity="Project\Bundle\Entity\Parameter", mappedBy="anthropometry", cascade={"persist"})
*
*/
protected $parameter;
...
}
在我的控制,我创建一个表单,并在相同的动作验证。 创建表单我需要实例一参数。但不需要坚持他。
所以..当我打电话$ EM->齐平,我得到了错误:
A new entity was found through the relationship ...
为了解决这个问题,我把级联= { “坚持”}在注释:
//Class Anthropometry
...
/**
*
* @ORM\OneToMany(targetEntity="Project\Bundle\Entity\Parameter", mappedBy="anthropometry", cascade={"persist"})
*
*/
protected $parameter;
但现在,在我的数据库中,参数正在持续保存,字段'Data'= NULL
我可以使用prePersist检查字段是否为NULL,然后再保留?
这样的事情?
//class Parameter
/**
*
* @ORM\prePersist
*/
public function prePersist(){
if($this->getData() == NULL){
return false;
}
}
Thx!
我正在开发将停供项目一会儿。但我认为这将起作用。谢谢!! – Munir 2012-04-04 16:31:46