public static function add($object, $field, $value) { $targetEntity = static::$namespace . '\\' . ModelAnnotation::get($object, $field, 'targetEntity'); foreach ($value as $index => $item) { if (is_array($item)) { $instance = $targetEntity::getRepository()->findOneBy($item); } else { if (is_int($item)) { $instance = $targetEntity::get($item); } else { if (is_object($item)) { $instance = $item; } } } if (!is_object($instance) && is_array($item)) { $instance = new $targetEntity($item); } if (isset($instance)) { $object->{$field}->add($instance); } } }