Add an object to a collection
string $field:
array $args:
\BadMethodCallException
\InvalidArgumentException
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());
}
}