protected function toMany($object, $collectionName, $target, $values)
    {
        $metadata   = $this->objectManager->getClassMetadata(ltrim($target, '\\'));
        $identifier = $metadata->getIdentifier();

        if (! is_array($values) && ! $values instanceof Traversable) {
            $values = (array)$values;
        }

        $collection = [];

        // If the collection contains identifiers, fetch the objects from database
        foreach ($values as $value) {
            if ($value instanceof $target) {
                // assumes modifications have already taken place in object
                $collection[] = $value;
                continue;
            } elseif (empty($value)) {
                // assumes no id and retrieves new $target
                $collection[] = $this->find($value, $target);
                continue;
            }

            $find = [];
            if (is_array($identifier)) {
                foreach ($identifier as $field) {
                    switch (gettype($value)) {
                        case 'object':
                            $getter = 'get' . Inflector::classify($field);

                            if (is_callable([$value, $getter])) {
                                $find[$field] = $value->$getter();
                            } elseif (property_exists($value, $field)) {
                                $find[$field] = $value->$field;
                            }
                            break;
                        case 'array':
                            if (array_key_exists($field, $value) && $value[$field] != null) {
                                $find[$field] = $value[$field];
                                unset($value[$field]); // removed identifier from persistable data
                            }
                            break;
                        default:
                            $find[$field] = $value;
                            break;
                    }
                }
            }

            if (! empty($find) && $found = $this->find($find, $target)) {
                $collection[] = (is_array($value)) ? $this->hydrate($value, $found) : $found;
            } else {
                $collection[] = (is_array($value)) ? $this->hydrate($value, new $target) : new $target;
            }
        }

        $collection = array_filter(
            $collection,
            function ($item) {
                return null !== $item;
            }
        );

        // Set the object so that the strategy can extract the Collection from it

        /** @var \DoctrineModule\Stdlib\Hydrator\Strategy\AbstractCollectionStrategy $collectionStrategy */
        $collectionStrategy = $this->getStrategy($collectionName);
        $collectionStrategy->setObject($object);

        // We could directly call hydrate method from the strategy, but if people want to override
        // hydrateValue function, they can do it and do their own stuff
        $this->hydrateValue($collectionName, $collection, $values);
    }
protected function toMany($object, $collectionName, $target, $values)
    {
        $metadata   = $this->objectManager->getClassMetadata(ltrim($target, '\\'));
        $identifier = $metadata->getIdentifier();

        if (! is_array($values) && ! $values instanceof Traversable) {
            $values = (array)$values;
        }

        $collection = [];

        // If the collection contains identifiers, fetch the objects from database
        foreach ($values as $value) {
            if ($value instanceof $target) {
                // assumes modifications have already taken place in object
                $collection[] = $value;
                continue;
            } elseif (empty($value)) {
                // assumes no id and retrieves new $target
                $collection[] = $this->find($value, $target);
                continue;
            }

            $find = [];
            if (is_array($identifier)) {
                foreach ($identifier as $field) {
                    switch (gettype($value)) {
                        case 'object':
                            $getter = 'get' . Inflector::classify($field);

                            if (is_callable([$value, $getter])) {
                                $find[$field] = $value->$getter();
                            } elseif (property_exists($value, $field)) {
                                $find[$field] = $value->$field;
                            }
                            break;
                        case 'array':
                            if (array_key_exists($field, $value) && $value[$field] != null) {
                                $find[$field] = $value[$field];
                                unset($value[$field]); // removed identifier from persistable data
                            }
                            break;
                        default:
                            $find[$field] = $value;
                            break;
                    }
                }
            }

            if (! empty($find) && $found = $this->find($find, $target)) {
                $collection[] = (is_array($value)) ? $this->hydrate($value, $found) : $found;
            } else {
                $collection[] = (is_array($value)) ? $this->hydrate($value, new $target) : new $target;
            }
        }

        $collection = array_filter(
            $collection,
            function ($item) {
                return null !== $item;
            }
        );

        // Set the object so that the strategy can extract the Collection from it

        /** @var \DoctrineModule\Stdlib\Hydrator\Strategy\AbstractCollectionStrategy $collectionStrategy */
        $collectionStrategy = $this->getStrategy($collectionName);
        $collectionStrategy->setObject($object);

        // We could directly call hydrate method from the strategy, but if people want to override
        // hydrateValue function, they can do it and do their own stuff
        $this->hydrateValue($collectionName, $collection, $values);
    }
protected function toMany ( $ object , $ collectionName , $ target , $ values ) { $ metadata = $ this -> objectManager -> getClassMetadata ( ltrim ( $ target , '\\' ) ) ; $ identifier = $ metadata -> getIdentifier ( ) ; if ( ! is_array ( $ values ) && ! $ values instanceof Traversable ) { $ values = ( array ) $ values ; } $ collection = [ ] ; // If the collection contains identifiers, fetch the objects from database foreach ( $ values as $ value ) { if ( $ value instanceof $ target ) { // assumes modifications have already taken place in object $ collection [ ] = $ value ; continue ; } elseif ( empty ( $ value ) ) { // assumes no id and retrieves new $target $ collection [ ] = $ this -> find ( $ value , $ target ) ; continue ; } $ find = [ ] ; if ( is_array ( $ identifier ) ) { foreach ( $ identifier as $ field ) { switch ( gettype ( $ value ) ) { case 'object' : $ getter = 'get' . Inflector :: classify ( $ field ) ; if ( is_callable ( [ $ value , $ getter ] ) ) { $ find [ $ field ] = $ value -> $ getter ( ) ; } elseif ( property_exists ( $ value , $ field ) ) { $ find [ $ field ] = $ value -> $ field ; } break ; case 'array' : if ( array_key_exists ( $ field , $ value ) && $ value [ $ field ] != null ) { $ find [ $ field ] = $ value [ $ field ] ; unset ( $ value [ $ field ] ) ; // removed identifier from persistable data } break ; default : $ find [ $ field ] = $ value ; break ; } } } if ( ! empty ( $ find ) && $ found = $ this -> find ( $ find , $ target ) ) { $ collection [ ] = ( is_array ( $ value ) ) ? $ this -> hydrate ( $ value , $ found ) : $ found ; } else { $ collection [ ] = ( is_array ( $ value ) ) ? $ this -> hydrate ( $ value , new $ target ) : new $ target ; } } $ collection = array_filter ( $ collection , function ( $ item ) { return null !== $ item ; } ) ; // Set the object so that the strategy can extract the Collection from it /** @var \DoctrineModule\Stdlib\Hydrator\Strategy\AbstractCollectionStrategy $collectionStrategy */ $ collectionStrategy = $ this -> getStrategy ( $ collectionName ) ; $ collectionStrategy -> setObject ( $ object ) ; // We could directly call hydrate method from the strategy, but if people want to override // hydrateValue function, they can do it and do their own stuff $ this -> hydrateValue ( $ collectionName , $ collection , $ values ) ; }
Handle ToMany associations. In proper Doctrine design, Collections should not be swapped, so
collections are always handled by reference. Internally, every collection is handled using specials
strategies that inherit from AbstractCollectionStrategy class, and that add or remove elements but without
changing the collection of the object

@param  object $object
@param  mixed  $collectionName
@param  string $target
@param  mixed  $values

@throws \InvalidArgumentException

@return void
Handle ToMany associations . In proper Doctrine design Collections should not be swapped so collections are always handled by reference . Internally every collection is handled using specials strategies that inherit from AbstractCollectionStrategy class and that add or remove elements but without changing the collection of the object