コード例 #1
0
ファイル: Collection.php プロジェクト: alexelev/bmf
 public function items()
 {
     if (!is_array($this->items)) {
         $query = $this->buildQuery();
         $rows = DB::getTable($query);
         $items = array();
         $firstAlias = substr($this->graph['model'], 0, -5);
         foreach ($rows as $row) {
             $this->parseRow($row, $items, $firstAlias, $this->graph);
         }
         $this->items = isset($items[$firstAlias]) ? array_values($items[$firstAlias]) : array();
     }
     return $this->items;
 }
コード例 #2
0
ファイル: Model.php プロジェクト: alexelev/bmf
 private function loadLink($link_name)
 {
     if (isset(static::$links[$link_name]) && $this->id) {
         $link = static::$links[$link_name];
         switch ($link['type']) {
             // Когда модель связана со списком объектов другой модели
             case LinkType::PRIMARY_KEY:
                 $collection = new Collection($link['model']);
                 if (isset($link['order'])) {
                     $collection->order($link['order']);
                 }
                 $where = "`{$link['field']}` = {$this->id}";
                 if (isset($link['where'])) {
                     $where .= " AND {$link['where']}";
                 }
                 $collection->where($where);
                 if (isset($link['limit'])) {
                     $collection->limit($link['limit']);
                 }
                 $models = $collection->items();
                 if (isset($link['limit']) && $link['limit'] == 1) {
                     if (!empty($models)) {
                         $this->models[$link_name] = $models[0];
                     } else {
                         $this->models[$link_name] = null;
                     }
                 } else {
                     $this->models[$link_name] = $models;
                 }
                 break;
                 // Когда модель связана с одним объектом другой модели
             // Когда модель связана с одним объектом другой модели
             case LinkType::FOREIGN_KEY:
                 $collection = new Collection($link['model']);
                 $where = "`id` = {$this->{$link['field']}}";
                 if (!empty($link['where'])) {
                     $where .= " AND {$link['where']}";
                 }
                 $collection->where($where);
                 $models = $collection->items();
                 if (!empty($models)) {
                     $this->models[$link_name] = $models[0];
                 } else {
                     $this->models[$link_name] = null;
                 }
                 break;
                 // Связь многие ко многим через промежуточную таблицу
             // Связь многие ко многим через промежуточную таблицу
             case LinkType::TABLE:
                 $ids = DB::getTable("SELECT `{$link['field2']}`\n                        FROM `{$link['table']}`\n                        WHERE `{$link['field1']}` = {$this->id}\n                    ");
                 foreach ($ids as $index => $row) {
                     $ids[$index] = $row[$link['field2']];
                 }
                 $collection = new Collection($link['model']);
                 if ($link['order']) {
                     $collection->order($link['order']);
                 }
                 $where = "`id` IN (" . implode(', ', $ids) . ")";
                 if ($link['where']) {
                     $where .= " AND {$link['where']}";
                 }
                 $collection->where($where);
                 if ($link['limit']) {
                     $collection->limit($link['limit']);
                 }
                 $models = $collection->items();
                 if ($link['limit'] == 1) {
                     if (!empty($models)) {
                         $this->models[$link_name] = $models[0];
                     } else {
                         $this->models[$link_name] = null;
                     }
                 } else {
                     $this->models[$link_name] = $models;
                 }
                 break;
         }
     }
 }