12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- /**
- * Created by PhpStorm.
- * User: xiaofeng
- * Date: 2018/3/4
- * Time: 上午9:28
- */
- namespace common\models;
- use yii\db\ActiveRecord;
- //use yii\behaviors\TimestampBehavior;
- class CategoryFacilities extends Common
- {
- // public $setDel = 1;
- // public function behaviors()
- // {
- // return [
- // [
- // 'class' => TimestampBehavior::className(),
- // 'attributes' => [
- // # 创建之前
- // ActiveRecord::EVENT_BEFORE_INSERT => ['create_at', 'update_at'],
- // # 修改之前
- // ActiveRecord::EVENT_BEFORE_UPDATE => ['update_at']
- // ],
- // #设置默认值
- // 'value' => $_SERVER['REQUEST_TIME']
- // ]
- // ];
- // }
- public function rules()
- {
- return [
- ['name','required','message'=>'不能为空'],
- ['img','string','max'=>40],
- ['name','string','min'=>2,'max'=>20,'message'=>'请输入正确的城市名称'],
- ['name', 'unique', 'targetClass' => 'common\models\CategoryFacilities','on'=>['add'],'message'=>'名称已经存在'],
- ];
- }
- /*
- * 调用里面的验证,错误返回数组,正确返回对象
- * */
- public function Authenticator($input)
- {
- $this->load($input,'');
- if(!$this->validate()) return $this->errors;
- return $this;
- }
- public function FindById($id)
- {
- return self::findOne($id);
- }
- public function getList($page,$arr = [])
- {
- $query = self::find();
- $query->select($arr);
- $query = $this->WhereColumn($query);
- if(!empty($page['page']))
- {
- $query->offset = ($page['page'] - 1) * $page['limit'];
- $query->limit = $page['limit'];
- }
- return $query->orderBy(['sort'=>SORT_DESC,'create_at'=>SORT_DESC])->asArray()->all();
- }
- private function WhereColumn($query)
- {
- $query->andFilterWhere(['name'=>$this->name]);
- $query->andWhere(['del'=>$this->setDel]);
- return $query;
- }
- public function Total()
- {
- $query = self::find();
- $query = $this->WhereColumn($query);
- return $query->count();
- }
- }
|