예제 #1
0
 /**
  * Construct
  *
  * @param string $entity entity class name
  */
 public function __construct($entity)
 {
     $ref = new ClassReflection($entity);
     // TODO: verify entity class
     $this->entity = $entity;
     if ($pos = strrpos($entity, '\\')) {
         $pos++;
     }
     $this->name = substr($entity, $pos);
     if ($ref->hasAnnotation('tableName')) {
         $this->tableName = $ref->getAnnotation('tableName');
     } else {
         $this->tableName = Tools::underscore(Tools::pluralize($this->name));
     }
     foreach ($ref->getProperties() as $property) {
         if ($property->hasAnnotation('column')) {
             $annotation = (array) $property->getAnnotation('column');
             $datatype = 'ActiveMapper\\DataTypes\\' . $annotation[0];
             unset($annotation[0]);
             $params = array_merge(array($property->name, $property->hasAnnotation('null')), $annotation);
             if (!\class_exists($datatype)) {
                 throw new \ActiveMapper\InvalidDataTypeException("Data type '{$datatype}' not exist");
             }
             $this->columns[$property->name] = callback(ClassReflection::from($datatype), 'newInstance')->invokeArgs($params);
         }
         if ($property->hasAnnotation('primary')) {
             if ($property->hasAnnotation('column')) {
                 if (empty($this->primaryKey)) {
                     $this->primaryKey = $property->name;
                 } else {
                     throw new \NotImplementedException("Multiple column primary key not implemented '{$entity}'");
                 }
             } else {
                 throw new \NotImplementedException("Primary key must be column {$entity}::\${$property->name}");
             }
         }
         if ($property->hasAnnotation('autoincrement')) {
             if (!$this->columns[$property->name] instanceof \ActiveMapper\DataTypes\Int) {
                 throw new \ActiveMapper\InvalidDataTypeException("Autoincrement avaiable only for Int data type column {$entity}::\${$property->name}");
             } elseif ($property->name == $this->primaryKey) {
                 $this->primaryKeyAutoincrement = TRUE;
             } else {
                 throw new \NotImplementedException("Auto increment for non primary key column not implemented");
             }
         }
     }
     if (!$this->primaryKey) {
         throw new \LogicException("Entity without primary key not supported '{$this->entity}'");
     }
     if (!$this->hasProxy() && ($ref->hasAnnotation('OneToOne') || $ref->hasAnnotation('OneToOne') || $ref->hasAnnotation('OneToOne') || $ref->hasAnnotation('OneToOne'))) {
         throw new \LogicException("Entity associations support only on proxy entity");
     }
 }
예제 #2
0
	/**
	 * Returns array of classes persistent parameters. They have public visibility and are non-static.
	 * This default implementation detects persistent parameters by annotation @persistent.
	 * @return array
	 */
	public static function getPersistentParams()
	{
		$rc = new Nette\Reflection\ClassReflection(get_called_class());
		$params = array();
		foreach ($rc->getProperties(\ReflectionProperty::IS_PUBLIC) as $rp) {
			if (!$rp->isStatic() && $rp->hasAnnotation('persistent')) {
				$params[] = $rp->getName();
			}
		}
		return $params;
	}