Advertisement
Guest User

model of news

a guest
Jul 21st, 2016
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.81 KB | None | 0 0
  1. <?php
  2. namespace Frontend\Models;
  3.  
  4. use Common\Model\BaseModel as Model;
  5.  
  6. class News extends Model {
  7.  
  8.     protected $table = 'news';
  9.  
  10.     protected $key;
  11.  
  12.     /**
  13.      * @return int
  14.      * @throws \Exception
  15.      */
  16.     public function getCount() {
  17.  
  18.         $sql = "SELECT * FROM " . parent::PREFIX . $this->table;
  19.  
  20.         $rows = $this->fetch($sql);
  21.         if ( !$rows ) {
  22.  
  23.             throw new \Exception("нет новостей");
  24.  
  25.         }
  26.         return count($rows);
  27.  
  28.     }
  29.  
  30.     /**
  31.      * @param $limit
  32.      * @param $offset
  33.      * @return mixed
  34.      * @throws \Exception
  35.      */
  36.     public function allNews( $limit, $offset ) {
  37.  
  38.         $sql = "SELECT * FROM " . parent::PREFIX . $this->table . " LIMIT {$limit}, {$offset}";
  39.  
  40.         $rows = $this->fetch($sql);
  41.         if ( !$rows ) {
  42.  
  43.             throw new \Exception("not find items");
  44.  
  45.         }
  46.         return $rows;
  47.  
  48.  
  49.     }
  50.  
  51.     /**
  52.      * @param $key
  53.      * @param array $param
  54.      * @return array|string
  55.      * @throws \Exception
  56.      */
  57.     public function getSingle($key, $param = []) {
  58.  
  59.         $data = [
  60.             "news_url" => $param['news_url']
  61.         ];
  62.  
  63.         $this->key = $key;
  64.         if( !$rows = parent::findBy($data, "SELECT * FROM " . parent::PREFIX . $this->table . " where {$this->key} = :news_url") ){
  65.  
  66.            throw new \Exception("Ошибка");
  67.            
  68.         }
  69.         return $rows;
  70.  
  71.     }
  72.  
  73.     /**
  74.      * @param $limit
  75.      * @return mixed
  76.      * @throws \Exception
  77.      */
  78.     public function getNews( $limit ) {
  79.  
  80.         $sql = "SELECT * FROM " . parent::PREFIX . $this->table . " LIMIT {$limit}";
  81.  
  82.         $rows = $this->fetch($sql);
  83.         if ( !$rows ) {
  84.  
  85.             throw new \Exception("not find items");
  86.  
  87.         }
  88.         return $rows;
  89.  
  90.  
  91.     }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement