NewKnowledgelabel.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/3/4
  6. * Time: 上午9:25
  7. */
  8. namespace common\models;
  9. use yii\helpers\ArrayHelper;
  10. class NewKnowledgelabel extends Common
  11. {
  12. public function rules()
  13. {
  14. return [
  15. ['name','required','message'=>'不能为空'],
  16. ['name','trim'],
  17. ['name', 'unique', 'targetClass' => 'common\models\NewKnowledgelabel','on'=>['add'],'filter'=>function($query){
  18. return $query->andWhere(['del'=>1]);
  19. }],
  20. [['pid','sort'],'number'],
  21. ['sort','default','value'=>0],
  22. ['path','string','max'=>100],
  23. ['icon','string','max'=>50],
  24. ['pid','default','value'=>0],
  25. [['is_show','del'],'default','value'=>1],
  26. ['path','default','value'=>'0,'],
  27. [['is_show','del'],'in', 'range' => [1, 2]],
  28. ];
  29. }
  30. public function attributeLabels()
  31. {
  32. return [
  33. 'name'=>'栏目名称',
  34. ];
  35. }
  36. public function Authenticator($input)
  37. {
  38. $this->load($input,'');
  39. if(!$this->validate()) return $this->errors;
  40. return $this;
  41. }
  42. public function FindById($id)
  43. {
  44. return self::findOne($id);
  45. }
  46. public function WhereColumn()
  47. {
  48. $query = static::find();
  49. $query->andFilterWhere(['del'=>1]);
  50. $query->andFilterWhere(['is_show'=>$this->is_show]);
  51. $query->andFilterWhere(['pid'=>$this->pid]);
  52. $query->andFilterWhere(['id'=>$this->id]);
  53. $query->andFilterWhere(['like','name',$this->name]);
  54. return $query;
  55. }
  56. //多条数据
  57. public function getList($page,$select = null,$sort = null)
  58. {
  59. $query = $this->WhereColumn();
  60. $query->select($select);
  61. if(!empty($page['page']))
  62. {
  63. $query->offset = ($page['page'] - 1) * $page['limit'];
  64. $query->limit = $page['limit'];
  65. }
  66. switch ($sort)
  67. {
  68. case 1:
  69. $query->orderBy(['sort'=>SORT_DESC]);
  70. break;
  71. case 2:
  72. $query->orderBy(['sort'=>SORT_ASC]);
  73. break;
  74. case 3:
  75. $query->orderBy('RAND()');
  76. break;
  77. default:
  78. $query->orderBy(['create_at'=>SORT_DESC]);
  79. break;
  80. }
  81. return $query->all();
  82. }
  83. //传递ID 如果是父类返回所有子类,如果是子类 返回自己;
  84. public function FatherOrSon($id)
  85. {
  86. $query = static::findOne($id);
  87. if(!empty($query))
  88. {
  89. if($query['pid'] == 0)
  90. {
  91. $this->pid = $query['id'];
  92. $q = $this->getList([],['id','name'],1);
  93. return $q;
  94. }
  95. return $query;
  96. }
  97. }
  98. }