通过在向数据库添加新实体时添加条件来解决,它检查实体是否不为空...显然有空实体试图保存。现在控制器代码如下所示:
...
$ciudades_id = explode(';', $this->getRequest()->getParam('ciudades_id'));
foreach($ciudades_id as $ciudad_id){
$ciudad = $this->_em->find("Application_Model_Ciudades", intval($ciudad_id));
if($ciudad!= null){
$carrera->getCiudad()->add($ciudad);
}
}
$instituciones_id = explode(';', $this->getRequest()->getParam('instituciones_id'));
foreach($instituciones_id as $institucion_id){
$institucion = $this->_em->find("Application_Model_Instituciones", intval($institucion_id));
if($institucion != null){
$carrera->getInstituciones()->add($institucion);
}
}
...
感谢那些帮助#doctrine IRC频道的人:)
这是我的问题......我得到了一个名为“Carreras”(carreers)的实体,它有一些关联,并且新的carreers被添加到数据库中,带有一个Web表单。对我来说,一个carreer是一个测试,它有问题和其他属性,用户可以选择申请该测试的城市和机构。
因此,当我尝试在实体上保存新数据时,我收到此错误:
An error occurred
Application error
Exception information:
Message: A new entity was found through the relationship
'Application_Model_Carreras#ciudad' that was not configured
to cascade persist operations for entity: Doctrine\ORM\UnitOfWork@.
Explicitly persist the new entity or configure cascading persist
operations on the relationship. If you cannot find out which entity
causes the problem implement 'Application_Model_Ciudades#__toString()'
to get a clue.
这是“Carreras”的模型
use Doctrine\Common\Collections\ArrayCollection;
/**
* @Entity
* @Table(name="carreras")
*/
class Application_Model_Carreras
{
/**
* @Id @Column(type="integer")
* @GeneratedValue
*/
private $id;
/** @Column(type="string") */
private $nombre;
/**
* @ManyToMany(targetEntity="Application_Model_PruebasCarrera")
* @JoinTable(name="Carreras_PruebasCarrera",
* joinColumns={@JoinColumn(name="carreras_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="pruebascarrera_id", referencedColumnName="id")}
* )
*/
private $pruebas;
/** @Column(type="integer") */
private $valor;
/** @Column(type="date") */
private $fechainicio;
/** @Column(type="date") */
private $fechafin;
/**
* This association causes error
* @ManyToMany(targetEntity="Application_Model_Ciudades")
* @JoinTable(name="carrera_ciudades",
* joinColumns={@JoinColumn(name="carrera_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="ciudad_id", referencedColumnName="id")}
* )
*/
private $ciudad;
/**
* @ManyToMany(targetEntity="Application_Model_Instituciones")
* @JoinTable(name="carrera_instituciones",
* joinColumns={@JoinColumn(name="carrera_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="institucion_id", referencedColumnName="id")}
* )
*/
private $instituciones;
public function __construct()
{
$this->pruebas = new ArrayCollection();
$this->ciudad = new ArrayCollection();
$this->instituciones = new ArrayCollection();
}
public function setNombre($nombre){
$this->nombre = $nombre;
}
public function setValor($valor){
$this->valor = $valor;
}
public function setFechainicio($fechainicio){
$this->fechainicio = $fechainicio;
}
public function setFechafin($fechafin){
$this->fechafin = $fechafin;
}
public function getCiudad(){
return $this->ciudad;
}
public function getPruebas(){
return $this->pruebas;
}
public function getInstituciones(){
return $this->instituciones;
}
}
现在控制器上的操作:
/*
* This is an action for adding a new career
*/
public function agregarAction()
{
$formtest = new Admin_Form_AgregarCarrera();
$this->view->formtest = $formtest;
if ($this->getRequest()->isPost() && $this->view->formtest->isValid($this->_getAllParams()))
{
/*
* If the form is okay creating new Carreer model object
* This model has some attributes and three associations (for now)
* you can see them later in detail
*/
$carrera = new Application_Model_Carreras();
$carrera->setNombre($this->getRequest()->getParam("nombre"));
$carrera->setValor($this->getRequest()->getParam("valor"));
$carrera->setFechainicio(new \DateTime($this->getRequest()->getParam("fechainicio")));
$carrera->setFechafin(new \DateTime($this->getRequest()->getParam("fechafin")));
/*
* This is the first association. It's working fine (for now)
*/
$pruebas = $this->getRequest()->getParam("pruebas");
foreach($pruebas as $prueba){
if($prueba != '0'){
$tmp = $this->_em->find("Application_Model_PruebasCarrera", $prueba);
$carrera->getPruebas()->add($tmp);
}
}
/*
* This is the second associations, i'm getting the error with this one
*/
$ciudades_id = explode(';', $this->getRequest()->getParam('ciudades_id'));
foreach($ciudades_id as $ciudad_id){
$ciudad = $this->_em->find("Application_Model_Ciudades", intval($ciudad_id));
$carrera->getCiudad()->add($ciudad);
}
/*
* This is the third one. Nothing to say about this.
*/
$instituciones_id = explode(';', $this->getRequest()->getParam('instituciones_id'));
foreach($instituciones_id as $institucion_id){
$institucion = $this->_em->find("Application_Model_Instituciones", intval($institucion_id));
$carrera->getInstituciones()->add($institucion);
}
$this->_em->persist($carrera);
$this->_em->flush();
//$this->redirector->gotoSimpleAndExit('index','Carrera','admin');
}
}
嗯,我不知道还有什么可以展示......如果你能帮助我,请发表评论:)
- 编辑
我在模型“Carreras”的关联中添加了cascade = {“persist”}并且更改了错误:
An error occurred
Application error
Exception information:
Message: Class Doctrine\ORM\UnitOfWork is not a
valid entity or mapped super class.
现在这是“Ciudades”模型,它存储了可用于测试的城市,并与每个城市中存在的机构相关联。
use Doctrine\Common\Collections\ArrayCollection;
/**
* @Entity
* @Table(name="ciudades")
*/
class Application_Model_Ciudades {
/**
* @Id @Column(type="integer")
* @GeneratedValue
*/
private $id;
/** @Column(type="string") */
private $ciudad;
/** @Column(type="string") */
private $departamento;
/** @Column(type="string") */
private $pais;
/**
* @ManyToMany(targetEntity="Application_Model_Instituciones")
* @JoinTable(name="Ciudades_Instituciones",
* joinColumns={@JoinColumn(name="ciudades_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="instituciones_id", referencedColumnName="id")}
* )
*/
private $instituciones;
public function __construct()
{
$this->instituciones = new ArrayCollection();
}
public function getCiudad(){
return $this->ciudad;
}
public function getId(){
return $this->id;
}
public function getInstituciones(){
return $this->instituciones;
}
}
现在这是“Instituciones”模型,它存储了可用于测试的机构。
/**
* @Entity
* @Table(name="instituciones")
*/
class Application_Model_Instituciones {
/**
* @Id @Column(type="integer")
* @GeneratedValue
*/
private $id;
/** @Column(type="string") */
private $nombre;
public function getId(){
return $this->id;
}
public function getNombre(){
return $this->nombre;
}
}
现在这是“PruebasCarrera”模型,对我来说这个模型实体存储了测试的问题,每个问题都可以有一个支持这个问题的合作伙伴:
use Doctrine\Common\Collections\ArrayCollection;
/**
* @Entity
* @Table(name="pruebas_carrera")
*/
class Application_Model_PruebasCarrera extends Application_Model_PruebasBase{
/**
* @Id @Column(type="integer")
* @GeneratedValue
*/
private $id;
/**
* @ManyToMany(targetEntity="Application_Model_Patrocinadores")
* @JoinTable(name="pruebascarrera_patrocinadores",
* joinColumns={@JoinColumn(name="pruebas_id", referencedColumnName="id", unique="true")},
* inverseJoinColumns={@JoinColumn(name="patrocinadores_id", referencedColumnName="id", unique=false)}
* )
*/
protected $patrocinadores;
/** @Column(type="string") */
private $respuesta;
public function __construct() {
$this->patrocinadores = new ArrayCollection();
}
public function setRespuesta($respuesta){
$this->respuesta = $respuesta;
}
public function getPatrocinadores(){
return $this->patrocinadores;
}
public function getId(){
return $this->id;
}
public function getRespuesta(){
return $this->respuesta;
}
}
答案 0 :(得分:1)
请显示相关实体的代码: Application_Model_Ciudades Application_Model_PruebasCarrera Application_Model_Instituciones
此时我认为你应该将cascade = {" persist"}添加到Application_Model_Ciudades实体。