private function PersistentObject::add

Add an object to a collection

Parameters

string $field:

array $args:

Throws

\BadMethodCallException

\InvalidArgumentException

1 call to PersistentObject::add()
PersistentObject::__call in drupal/core/vendor/doctrine/common/lib/Doctrine/Common/Persistence/PersistentObject.php
Magic method that implements

File

drupal/core/vendor/doctrine/common/lib/Doctrine/Common/Persistence/PersistentObject.php, line 183

Class

PersistentObject
PersistentObject base class that implements getter/setter methods for all mapped fields and associations by overriding __call.

Namespace

Doctrine\Common\Persistence

Code

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());
  }
}