Email.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/4/14
  6. * Time: 下午5:38
  7. */
  8. namespace common\models;
  9. class Email extends Common
  10. {
  11. public function rules()
  12. {
  13. return [
  14. ['email','required','message'=>'不能为空'],
  15. ['email','email'],
  16. // ['email', 'unique', 'targetClass' => 'common\models\Email','on'=>['add'],'message'=>'该邮箱已存在','filter'=>function($query){
  17. // return $query->andWhere(['del'=>1]);
  18. // }],
  19. ['city', 'unique', 'targetClass' => 'common\models\Email','on'=>['add'],'message'=>'该区域的邮箱已经存在','filter'=>function($query){
  20. return $query->andWhere(['del'=>1]);;
  21. }],
  22. ['city','number'],
  23. ['name','string','max'=>10],
  24. [['email','name','city'],'trim'],
  25. //
  26. ['city', 'unique', 'targetClass' => 'common\models\Email','on'=>['edit'],'message'=>'该区域已存在','filter'=>function($query){
  27. $query->andWhere(['del'=>1]);
  28. return $query->andWhere(['!=','id',$this->id]);
  29. }],
  30. ];
  31. }
  32. public function attributeLabels()
  33. {
  34. return [
  35. 'city'=>'区域',
  36. 'email'=>'邮箱',
  37. 'name'=>'姓名',
  38. ];
  39. }
  40. public function FindById($id)
  41. {
  42. return self::findOne($id);
  43. }
  44. /*
  45. * 传入区域ID 获取相对应的邮箱
  46. * */
  47. public function FindByEmail($city)
  48. {
  49. $query = self::find();
  50. $query->select(['pfg_email.email','pfg_category_city.city_name']);
  51. $query->andWhere(['pfg_email.city'=>$city]);
  52. $query->andWhere(['pfg_email.del'=>1]);
  53. $query = $this->LeftJoinCategoryCity($query);
  54. return $query->asArray()->one();
  55. }
  56. public function Authenticator($input)
  57. {
  58. $this->load($input,'');
  59. if(!$this->validate()) return $this->errors;
  60. return $this;
  61. }
  62. public function getList($page)
  63. {
  64. $query = self::find();
  65. $query->select(['pfg_email.*','pfg_category_city.city_name']);
  66. $query->andFilterWhere(['pfg_email.del'=>1]);
  67. $query = $this->LeftJoinCategoryCity($query);
  68. if(!empty($page['page']))
  69. {
  70. $query->offset = ($page['page'] - 1) * $page['limit'];
  71. $query->limit = $page['limit'];
  72. }
  73. return $query->orderBy(['pfg_email.create_at'=>SORT_ASC])->asArray()->all();
  74. }
  75. public function Total()
  76. {
  77. $query = self::find();
  78. $query = $this->LeftJoinCategoryCity($query);
  79. return $query->count();
  80. }
  81. private function LeftJoinCategoryCity($query)
  82. {
  83. return $query->leftJoin('pfg_category_city','pfg_email.city=pfg_category_city.id');
  84. }
  85. }