limit() public method

Returns a new collection with a limited number of elements
public limit ( integer $limit ) : Collection
$limit integer The number of elements to return
return Collection
Example #1
0
 protected static function actionDefault()
 {
     /*
      * Если выводиться вся страница то списко товаров передается шаблону products-list таким образом
      * Controller -> index -> products-list
      * Если запрос аяксовый то переменные передаются напрямую шаблону products-list
      * Controller -> products-list
      * Но при создании этих переменных мы не думаем о том какой шаблон будет выводиться.
      */
     // Заголовок списка товаров
     self::templateVar('product_list_title', 'Последние товары');
     // товары в списке (6 последних добавленных в соотвествии с фильтром цен)
     $collection = new Collection('Product', App::currentLang()->getId());
     $collection->link('cover');
     $collection->order("`add` DESC");
     $where = "`active` = 1";
     if (isset($_POST['submitPriceRange'])) {
         $where .= " AND `price` >= {$_POST['min']} AND `price` <= {$_POST['max']}";
     }
     $collection->where($where);
     $collection->limit(6);
     self::templateVar('product_list_items', $collection->items());
     if (App::isAjax()) {
         // Если это ajax выводим чанк списка товаров
         return self::displayPage('chunks/product-list');
     }
     // Привязка категорий для табов под списком товаров
     // Получаем две категории из базы
     $collection = new Collection('Category', App::currentLang()->getId());
     $collection->where("`active` = 1");
     $collection->limit(2);
     $categories = $collection->items();
     foreach ($categories as $category) {
         $collection = new Collection('Product', App::currentLang()->getId());
         $collection->where("`id_category` = " . $category->getId() . " AND `active` = 1");
         $collection->limit(4);
         $category->setLink('products', $collection->items());
     }
     foreach ($categories as $index => $category) {
         if (empty($category->products)) {
             unset($categories[$index]);
         }
     }
     // Привязываем к шаблону
     static::templateVar('tab_categories', $categories);
     return self::displayPage('index');
 }
Example #2
0
 protected static function actionSearch()
 {
     // Заголовок над списком твоаров
     self::templateVar('product_list_title', 'Результаты поиска');
     // Товары
     $products = new Collection('Product', App::currentLang()->getId());
     $products->link('cover');
     $products->where("`active` = 1 AND `name` LIKE '%{$_GET['q']}%'" . (isset($_POST['submitPriceRange']) ? " AND `price` >= {$_POST['min']} AND `price` <= {$_POST['max']}" : ''));
     $products->order("`add` DESC");
     $products->limit(12);
     self::templateVar('product_list_items', $products->items());
     // Если это ajax выводим только чанк product-list
     if (App::isAjax()) {
         return self::displayPage('chunks/product-list');
     }
     return self::displayPage('catalog');
 }
Example #3
0
 public static function __callStatic($name, $arguments)
 {
     $collection = new Collection($arguments[0]);
     switch ($name) {
         case '_columns':
             return $collection->columns($arguments[1]);
         case '_where':
             return $collection->where($arguments[1]);
         case '_join':
             return $collection->join($arguments[1]);
         case '_order':
             return $collection->order($arguments[1], $arguments[2]);
         case '_group':
             return $collection->group($arguments[1]);
         case '_limit':
             return $collection->limit($arguments[1]);
         case '_offset':
             return $collection->offset($arguments[1]);
         case '_all':
             return $collection->all();
         default:
             throw new \Bacon\Exceptions\MethodNotFound();
     }
 }
Example #4
0
 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;
         }
     }
 }