I have a strange issues when i want to persist entity which holds reference to itself.
DB:
Table PROJECT has Column PARENT_ID which holds references to itself.
Model:
/**
* @OneToOne(targetEntity="Model_Name")
* @JoinColumn(name="PARENT_ID", referencedColumnName="ID")
**/
protected $_parentId;
Code:
$project->setParentId(int);
After setting PARENT_ID i persist entity, and all data except PARENT_ID are stored inside DB.
EDIT
Id field/column
/**
* @Id
* @Column(type="integer", name="ID")
* @GeneratedValue(strategy="IDENTITY")
*/
protected $_id;
/**
* @return int
*/
public function getParentId()
{
return $this->_parentId;
}
/**
* @param int $parentProjectId
* @return Application_Model_Data_Project $this
*/
public function setParentId($parentId)
{
$this->_parentId = $parentId;
return $this;
}
Can you post more code? That part looks just fine.
Here’s a SSCCE of a working self-referencing entity. Maybe that’ll help.
EDIT
If you want to save a relationship to another object with doctrine, you must save the object, not the id. Which means you have to set the Model_Name object, and not the id (integer) in your variable tagged as a Model_Name (targetEntity). (Doctrine will use the int automatically in the DB).
See my example above with the ManyToOne.