Example #1
0
 public static function convertToArray($object)
 {
     $result = array();
     $reflector = new \ReflectionClass($object);
     $properties = $reflector->getProperties(\ReflectionProperty::IS_PROTECTED);
     foreach ($properties as $property) {
         $property->setAccessible(true);
         $value = $property->getValue($object);
         $field = $property->getName();
         if ($value == null) {
             $result[$field] = null;
         } else {
             if (ModelAnnotation::match($property, array('ManyToMany', 'OneToMany'))) {
                 $result[$field] = $value->count();
             } elseif (ModelAnnotation::match($property, array('ManyToOne', 'OneToOne'))) {
                 $result[$field] = $value->id;
             } else {
                 $result[$field] = $value;
             }
         }
     }
     return $result;
 }
Example #2
0
 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);
         }
     }
 }