コード例 #1
0
ファイル: ModelRow.php プロジェクト: nkalush/orm
 public function __get($name = null)
 {
     if (property_exists($this->data, $name)) {
         return $this->data->{$name};
     } elseif (method_exists($this->model, 'get_' . $name)) {
         $name = 'get_' . $name;
         return $this->model->{$name}($this);
     } elseif (isset($this->_associations[$name])) {
         return $this->_associations[$name];
     } elseif (isset($this->model->associations[$name])) {
         $association = $this->model->associations[$name];
         if ($association['type'] == 'has_many') {
             $class_name = !empty($association['options']['class_name']) ? ucfirst($association['options']['class_name']) : ucfirst(\orm\Inflector::singularize($association['name']));
             $class = new $class_name();
             if ($association['options'] !== null && $association['options']['through']) {
                 $primary_key = $this->model->primary_key();
                 $foreign_key1 = !empty($association['options']['foreign_key']) ? $association['options']['foreign_key'] : $class->primary_key();
                 $foreign_key2 = $class->primary_key();
                 $table = $association['options']['through'];
                 $class->base_join($table, $table . '.' . $foreign_key1, "=", $class->table_name() . '.' . $foreign_key2);
                 $id = $this->{$primary_key};
                 $class->base_where($table . '.' . $primary_key, "=", $id);
                 $class->pivot_table = array("table_name" => $table, "attach_key" => $primary_key, "attach_id" => $id);
             } else {
                 $primary_key = $this->model->primary_key();
                 $id = $this->{$primary_key};
                 $class->base_where($primary_key, '=', $id);
             }
             $this->_associations[$name] = $class;
             return $class;
         } elseif ($association['type'] == 'belongs_to') {
             $class_name = !empty($association['options']['class_name']) ? ucfirst($association['options']['class_name']) : ucfirst($association['name']);
             $class = new $class_name();
             if (!empty($association['options']['foreign_key'])) {
                 $primary_key = $association['options']['foreign_key'];
             } else {
                 $primary_key = $class->primary_key();
             }
             $id = $this->{$primary_key};
             if ($id) {
                 $return = $class->find($id);
                 $this->_associations[$name] = $return;
                 return $return;
             } else {
                 return null;
             }
         }
     }
 }
コード例 #2
0
 public static function __callStatic($name, $args)
 {
     $class = get_called_class();
     if (strpos($name, 'get') !== 0) {
         throw new MemberAccessException("Call to undefined static method {$class}::{$name}().");
     }
     $type = Inflector::singularize(substr($name, strlen('get')));
     $prefix = strToLower($type) . '_';
     $return = [];
     $ref = new ReflectionClass($class);
     foreach ($ref->getConstants() as $name => $value) {
         $name = strToLower($name);
         if (strpos($name, $prefix) === 0) {
             $key = substr($name, strlen($prefix));
             $return[$key] = $value;
         }
     }
     return $return;
 }
コード例 #3
0
ファイル: Repository.php プロジェクト: clevis/orm
 /**
  * Mozno ovlivnit jake entity repository vyraby.
  * Pri $data === NULL vraci pole nazvu vsech trid ktere tato repository muze vyrobit,
  * jinak vraci konkretni nazev tridy pro tyto data.
  * Kdyz vyraby jen jednu tridu muze pokazde vratit string.
  *
  * Defaultne vraci nazev repository v jednotem cisle; pro prevod pouziva {@see Inflector::singularize()}.
  * V pripade potreby je mozne prepsat tuto metodu, nebo property $entityClassName:
  * <code>
  * // MiceRepository
  * protected $entityClassName = 'Mouse';
  * </code>
  *
  * Repository muze vyrabet ruzne entity, muze se rozhodovat na zaklade nejake polozky kterou ma ulozenou v ulozisti, napr. $type
  * <code>
  * // ProductsRepository
  * public function getEntityClassName(array $data = NULL)
  * {
  * 	$entities = array(
  * 		Product::BOOK => 'Book',
  * 		Product::MAGAZINE => 'Magazine',
  * 		Product::CD_MUSIC => 'CdMusic',
  * 		Product::DVD_MOVIE => 'DvdMovie',
  * 	);
  *
  * 	if ($data === NULL) return $entities;
  * 	else if (isset($entities[$data['type']])) return $entities[$data['type']];
  * }
  *
  * </code>
  *
  * @param array|NULL
  * @return string|array
  */
 public function getEntityClassName(array $data = NULL)
 {
     if ($this->entityClassName === NULL) {
         $helper = $this->getModel()->getContext()->getService('repositoryHelper', 'Orm\\RepositoryHelper');
         $this->entityClassName = Inflector::singularize($helper->normalizeRepository($this));
     }
     return $this->entityClassName;
 }