Created
March 29, 2012 18:38
-
-
Save ssmusoke/2241767 to your computer and use it in GitHub Desktop.
Model for Person
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Doctrine\Common\Collections\ArrayCollection; | |
use Doctrine\ORM\Mapping as ORM; | |
/** | |
* Family relationship | |
* | |
* @ORM\Entity | |
* @ORM\Table(name="family") | |
* | |
*/ | |
class Family extends BaseRecord { | |
/** | |
* @ORM\Id | |
* @ORM\Column(type="integer") | |
* @ORM\GeneratedValue | |
*/ | |
protected $id; | |
/** | |
* @ORM\Column(name="`type`", type="integer") | |
*/ | |
protected $type = 1; | |
/** | |
* Father | |
* | |
* @ORM\ManyToOne(targetEntity="Person", inversedBy="ffamilies", cascade={"persist", "remove"}) | |
* @ORM\JoinColumn(name="fatherid", referencedColumnName="id") | |
**/ | |
protected $father; | |
/** | |
* The mother | |
* | |
* @ORM\ManyToOne(targetEntity="Person", inversedBy="mfamilies", cascade={"persist", "remove"}) | |
* @ORM\JoinColumn(name="motherid", referencedColumnName="id") | |
*/ | |
protected $mother; | |
/** | |
* The children | |
* | |
* @ORM\OnetoMany(targetEntity="Person", mappedBy="family") | |
*/ | |
protected $children; | |
/* (non-PHPdoc) | |
* @see BaseRecord::init() | |
*/ | |
public function init() | |
{ | |
$this->children = new ArrayCollection(); | |
} | |
/** | |
* Set the father | |
* | |
* @param integer fatherid The identifier for the father in the family | |
*/ | |
public function setFatherID($fatherid) { | |
if (!isEmptyString($fatherid)) { | |
$this->father = loadEntity('Person', $fatherid); | |
} | |
} | |
/** | |
* Set the mother | |
* | |
* @param integer motherid The identifier for the monther in the family | |
*/ | |
public function setMotherID($motherid) { | |
if (!isEmptyString($motherid)) { | |
$this->mother = loadEntity('Person', $motherid); | |
} | |
} | |
/** | |
* Return the ID of the father or FALSE if there is no father | |
* | |
* @return integer|FALSE | |
*/ | |
function getFatherID() { | |
if (is_null($this->father)) { | |
return false; | |
} | |
return $this->father->getID(); | |
} | |
/** | |
* Return the ID of the mother or FALSE if there is no child | |
* | |
* @return integer|FALSE | |
*/ | |
function getMotherID() { | |
if (is_null($this->mother)) { | |
return false; | |
} | |
return $this->mother->getID(); | |
} | |
/** | |
* Add a child to the family | |
* | |
* @param Person $child | |
*/ | |
function addChild($child) { | |
$child->setFamily($this); | |
$this->children->set($child->getID(), $child); | |
} | |
/** | |
* Add a child to the family using their id | |
* | |
* @param integer $childid | |
*/ | |
function addChildByID($childid) { | |
if (!isEmptyString($childid)) { | |
$this->addChild(loadEntity('Person', $childid)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment