Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use yii\db\ActiveRecord;
- class User extends ActiveRecord {
- // Не привожу правила валидации
- /**
- * Реляция для выборки всех сообщений пользователя
- * @return \yii\db\ActiveQuery
- */
- public function getMessages()
- {
- return $this->hasMany(Message::className(), ['user_id' => 'id']);
- }
- /**
- * Реляция для выборки всех сообщений пользователя в указаном чате
- * @param $chat_id
- * @return \yii\db\ActiveQuery
- */
- public function getMessagesByChat($chat_id)
- {
- return $this->getMessages()->where(['chat_id' => $chat_id]);
- }
- }
- class Chat extends ActiveRecord {
- // Не привожу правила валидации
- /**
- * Реляция для выборки все сообщений чата
- * @return \yii\db\ActiveQuery
- */
- public function getMessages()
- {
- return $this->hasMany(Message::className(), ['chat_id' => 'id']);
- }
- /**
- * Реляция для выборки всех сообщений чата, конкртеного пользователя
- * @param $user_id
- * @return \yii\db\ActiveQuery
- */
- public function getMessagesByUser($user_id)
- {
- return $this->getMessages()->where(['user_id' => $user_id]);
- }
- }
- class Message extends ActiveRecord {
- public function rules()
- {
- return [
- ['user_id', 'exist', 'targetClass' => User::className(), 'targetAttribute' => 'id'],
- ['chat_id', 'exist', 'targetClass' => Chat::className(), 'targetAttribute' => 'id']
- ];
- }
- public function getChat()
- {
- return $this->hasOne(Chat::className(), ['id' => 'chat_id']);
- }
- public function getUser()
- {
- return $this->hasOne(User::className(), ['id' => 'user_id']);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment