123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- <?php
-
-
- namespace Doctrine\Common\Persistence;
-
- use Doctrine\Common\Persistence\Mapping\ClassMetadata;
- use Doctrine\Common\Collections\ArrayCollection;
- use Doctrine\Common\Collections\Collection;
-
-
- abstract class PersistentObject implements ObjectManagerAware
- {
-
-
- private static $objectManager;
-
-
-
- private $cm;
-
-
-
- static public function setObjectManager(ObjectManager $objectManager = null)
- {
- self::$objectManager = $objectManager;
- }
-
-
-
- static public function getObjectManager()
- {
- return self::$objectManager;
- }
-
-
-
- public function injectObjectManager(ObjectManager $objectManager, ClassMetadata $classMetadata)
- {
- if ($objectManager !== self::$objectManager) {
- throw new \RuntimeException("Trying to use PersistentObject with different ObjectManager instances. " .
- "Was PersistentObject::setObjectManager() called?");
- }
-
- $this->cm = $classMetadata;
- }
-
-
-
- private function set($field, $args)
- {
- $this->initializeDoctrine();
-
- if ($this->cm->hasField($field) && !$this->cm->isIdentifier($field)) {
- $this->$field = $args[0];
- } else if ($this->cm->hasAssociation($field) && $this->cm->isSingleValuedAssociation($field)) {
- $targetClass = $this->cm->getAssociationTargetClass($field);
- if (!($args[0] instanceof $targetClass) && $args[0] !== null) {
- throw new \InvalidArgumentException("Expected persistent object of type '".$targetClass."'");
- }
- $this->$field = $args[0];
- $this->completeOwningSide($field, $targetClass, $args[0]);
- } else {
- throw new \BadMethodCallException("no field with name '".$field."' exists on '".$this->cm->getName()."'");
- }
- }
-
-
-
- private function get($field)
- {
- $this->initializeDoctrine();
-
- if ( $this->cm->hasField($field) || $this->cm->hasAssociation($field) ) {
- return $this->$field;
- } else {
- throw new \BadMethodCallException("no field with name '".$field."' exists on '".$this->cm->getName()."'");
- }
- }
-
-
-
- private function completeOwningSide($field, $targetClass, $targetObject)
- {
-
-
- if ($this->cm->isAssociationInverseSide($field)) {
- $mappedByField = $this->cm->getAssociationMappedByTargetField($field);
- $targetMetadata = self::$objectManager->getClassMetadata($targetClass);
-
- $setter = ($targetMetadata->isCollectionValuedAssociation($mappedByField) ? "add" : "set").$mappedByField;
- $targetObject->$setter($this);
- }
- }
-
-
-
- private function add($field, $args)
- {
- $this->initializeDoctrine();
-
- if ($this->cm->hasAssociation($field) && $this->cm->isCollectionValuedAssociation($field)) {
- $targetClass = $this->cm->getAssociationTargetClass($field);
- if (!($args[0] instanceof $targetClass)) {
- throw new \InvalidArgumentException("Expected persistent object of type '".$targetClass."'");
- }
- if (!($this->$field instanceof Collection)) {
- $this->$field = new ArrayCollection($this->$field ?: array());
- }
- $this->$field->add($args[0]);
- $this->completeOwningSide($field, $targetClass, $args[0]);
- } else {
- throw new \BadMethodCallException("There is no method add".$field."() on ".$this->cm->getName());
- }
- }
-
-
-
- private function initializeDoctrine()
- {
- if ($this->cm !== null) {
- return;
- }
-
- if (!self::$objectManager) {
- throw new \RuntimeException("No runtime object manager set. Call PersistentObject#setObjectManager().");
- }
-
- $this->cm = self::$objectManager->getClassMetadata(get_class($this));
- }
-
-
-
- public function __call($method, $args)
- {
- $command = substr($method, 0, 3);
- $field = lcfirst(substr($method, 3));
- if ($command == "set") {
- $this->set($field, $args);
- } else if ($command == "get") {
- return $this->get($field);
- } else if ($command == "add") {
- $this->add($field, $args);
- } else {
- throw new \BadMethodCallException("There is no method ".$method." on ".$this->cm->getName());
- }
- }
- }
|