我无法让它正常工作。 它将插入两次条目,并且永远不会在project_data表中设置project_id,它始终为0。 我试过插入多种方式..
喜欢这样
//... do some work
$project = new PSD_Model_Entity_Project();
$project->setStatusId($this->_em->find("PSD_Model_Entity_Status", '3'));
$project->addData('hello', $data->data->hello);
$this->_em->persist($project);
$this->_em->flush();
就像这样
$project = new PSD_Model_Entity_Project();
$project->setStatusId($this->_em->find("PSD_Model_Entity_Status", '3'));
$project_data = new PSD_Model_Entity_Project_Data('hello', $data->data->hello,$project);
$this->_em->persist($project);
$this->_em->persist($project_data);
$this->_em->flush();
学说模式:
<?php
use Doctrine\ORM\Mapping as ORM;
/**
* PSD_Model_Entity_Project
* @Entity
* @Table(name="project")
*/
class PSD_Model_Entity_Project
{
/**
* @var integer $project_id
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
private $project_id;
/**
* @var integer $dependent
* @Column(type="integer")
*/
private $dependent;
/**
* @var integer $iduser
* @Column(type="integer")
*/
private $iduser;
/**
* @var datetime $created
* @Column(type="datetime")
*/
private $created;
/**
* @var datetime $modified
* @Column(type="datetime")
*/
private $modified;
/**
* @var PSD_Model_Entity_Status
* @ManyToOne(targetEntity="PSD_Model_Entity_Status")
* @JoinColumn(name="status_id", referencedColumnName="status_id")
*/
private $status_id;
/**
* @OneToMany(targetEntity="PSD_Model_Entity_Project_Data", mappedBy="project_id", cascade={"ALL"}, indexBy="name")
*/
private $data;
public function __construct()
{
$this->data = new \Doctrine\Common\Collections\ArrayCollection();
}
public function addData($name, $value)
{
$this->data[$name] = new PSD_Model_Entity_Project_Data($name, $value, $this);
}
/**
* Get project_id
*
* @return integer
*/
public function getProjectId()
{
return $this->project_id;
}
/**
* Set dependent
*
* @param integer $dependent
* @return PSD_Model_Entity_Project
*/
public function setDependent($dependent)
{
$this->dependent = $dependent;
return $this;
}
/**
* Get dependent
*
* @return integer
*/
public function getDependent()
{
return $this->dependent;
}
/**
* Set iduser
*
* @param integer $iduser
* @return PSD_Model_Entity_Project
*/
public function setIduser($iduser)
{
$this->iduser = $iduser;
return $this;
}
/**
* Get iduser
*
* @return integer
*/
public function getIduser()
{
return $this->iduser;
}
/**
* Set created
*
* @param datetime $created
* @return PSD_Model_Entity_Project
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get created
*
* @return datetime
*/
public function getCreated()
{
return $this->created;
}
/**
* Set modified
*
* @param datetime $modified
* @return PSD_Model_Entity_Project
*/
public function setModified($modified)
{
$this->modified = $modified;
return $this;
}
/**
* Get modified
*
* @return datetime
*/
public function getModified()
{
return $this->modified;
}
/**
* Set status_id
*
* @param PSD_Model_Entity_Status $statusId
* @return PSD_Model_Entity_Project
*/
public function setStatusId(\PSD_Model_Entity_Status $statusId = null)
{
$this->status_id = $statusId;
return $this;
}
/**
* Get status_id
*
* @return PSD_Model_Entity_Status
*/
public function getStatusId()
{
return $this->status_id;
}
}
<?php
use Doctrine\ORM\Mapping as ORM;
/**
* PSD_Model_Entity_Project_Data
* @Entity
* @Table(name="project_data")
*/
class PSD_Model_Entity_Project_Data
{
/**
* @var integer $project_data_id
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
private $project_data_id;
/**
* @var integer $project_id
* @Column(type="integer")
* @ManyToOne(targetEntity="PSD_Model_Entity_Project", inversedBy="data")
*/
private $project_id;
/**
* @var string $name
* @Column(type="string")
*/
private $name;
/**
* @var string $value
* @Column(type="string")
*/
private $value;
public function __construct($name, $value, $project)
{
$this->name = $name;
$this->value = $value;
$this->project_id = $project;
}
/**
* Set project_id
*
* @param PSD_Model_Entity_Project $projectId
* @return PSD_Model_Entity_Project_Data
*/
public function setProjectId(\PSD_Model_Entity_Project $projectId = null)
{
$this->project_id = $projectId;
return $this;
}
/**
* Get project_id
*
* @return integer
*/
public function getProjectId()
{
return $this->project_id;
}
/**
* Set name
*
* @param string $name
* @return PSD_Model_Entity_Project_Data
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set value
*
* @param string $value
* @return PSD_Model_Entity_Project_Data
*/
public function setValue($value)
{
$this->value = $value;
return $this;
}
/**
* Get value
*
* @return string
*/
public function getValue()
{
return $this->value;
}
}
答案 0 :(得分:0)
根据文档,我认为您的数据模型属性应标记为protected
,而不是private
。
$project_id
中对PSD_Model_Entity_Project_Data
的引用目前有:
/**
* @Column(type="integer")
* @ManyToOne(targetEntity="PSD_Model_Entity_Project", inversedBy="data")
*/
您可以尝试将Column
更改为JoinColumn
:
/**
* @JoinColumn()
* @ManyToOne(targetEntity="PSD_Model_Entity_Project", inversedBy="data")
*/
是的,我有一个类似的模型,不需要inversedBy
;但是自从我触及Doctrine以来已经有一段时间了,所以这可能没有任何区别:))
答案 1 :(得分:0)
试试这个。尝试通过集合设置属性,而不是使用构造函数设置属性,并查看是否消除了重复的行。