alxkolm

chat models

Sep 30th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.96 KB | None | 0 0
  1. <?php
  2.  
  3. use yii\db\ActiveRecord;
  4.  
  5. class User extends ActiveRecord {
  6.  
  7.     // Не привожу правила валидации
  8.  
  9.     /**
  10.      * Реляция для выборки всех сообщений пользователя
  11.      * @return \yii\db\ActiveQuery
  12.      */
  13.     public function getMessages()
  14.     {
  15.         return $this->hasMany(Message::className(), ['user_id' => 'id']);
  16.     }
  17.  
  18.     /**
  19.      * Реляция для выборки всех сообщений пользователя в указаном чате
  20.      * @param $chat_id
  21.      * @return \yii\db\ActiveQuery
  22.      */
  23.     public function getMessagesByChat($chat_id)
  24.     {
  25.         return $this->getMessages()->where(['chat_id' => $chat_id]);
  26.     }
  27. }
  28.  
  29. class Chat extends ActiveRecord {
  30.  
  31.     // Не привожу правила валидации
  32.  
  33.     /**
  34.      * Реляция для выборки все сообщений чата
  35.      * @return \yii\db\ActiveQuery
  36.      */
  37.     public function getMessages()
  38.     {
  39.         return $this->hasMany(Message::className(), ['chat_id' => 'id']);
  40.     }
  41.  
  42.     /**
  43.      * Реляция для выборки всех сообщений чата, конкртеного пользователя
  44.      * @param $user_id
  45.      * @return \yii\db\ActiveQuery
  46.      */
  47.     public function getMessagesByUser($user_id)
  48.     {
  49.         return $this->getMessages()->where(['user_id' => $user_id]);
  50.     }
  51. }
  52.  
  53. class Message extends ActiveRecord {
  54.  
  55.     public function rules()
  56.     {
  57.         return [
  58.             ['user_id', 'exist', 'targetClass' => User::className(), 'targetAttribute' => 'id'],
  59.             ['chat_id', 'exist', 'targetClass' => Chat::className(), 'targetAttribute' => 'id']
  60.         ];
  61.     }
  62.  
  63.     public function getChat()
  64.     {
  65.         return $this->hasOne(Chat::className(), ['id' => 'chat_id']);
  66.     }
  67.  
  68.     public function getUser()
  69.     {
  70.         return $this->hasOne(User::className(), ['id' => 'user_id']);
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment