3

我无法让它正常工作。它将插入条目两次,并且永远不会在 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;
    }
}
4

2 回答 2

0

尝试这个。尝试通过集合设置属性,而不是使用构造函数,看看这是否消除了重复的行。

于 2012-06-07T05:58:17.467 回答
0

根据文档,我相信您的数据模型属性应标记为protected而不是。private

$project_idPSD_Model_Entity_Project_Data当前的内部引用有:

/**
 * @Column(type="integer")
 * @ManyToOne(targetEntity="PSD_Model_Entity_Project", inversedBy="data")
 */

您可以尝试将其更改ColumnJoinColumn

/**
 * @JoinColumn()
 * @ManyToOne(targetEntity="PSD_Model_Entity_Project", inversedBy="data")
 */

顺便说一句,我有一个类似的模型,不需要inversedBy; 但是自从我接触 Doctrine 以来已经有一段时间了,所以这可能没有任何区别:)

于 2012-06-06T05:25:13.730 回答