Просмотр файла vkclone-0.0.1/protected/models/UserMessage.php

Размер файла: 1.65Kb
<?php
/**
 * This is the model class for table "{{user_messages}}".
 */
class UserMessage extends CActiveRecord {

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return '{{user_messages}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		return array(
			array('user_id, receiver_id, text', 'required'),
			array('user_id, receiver_id', 'length', 'max' => 10),
			array('new', 'boolean'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		return array(
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
			'receiver' => array(self::BELONGS_TO, 'User', 'receiver_id'),
		);
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return TalkMessage the static model class
	 */
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}

	public function unreadMessages($id) {
		$this->dbCriteria->compare("new", "1");
		$this->dbCriteria->compare("receiver_id", $id);
		return $this;
	}

	public function getTalk($user1, $user2) {
		$this->getDbCriteria()->mergeWith(array(
			'condition' => '(user_id = :user1 AND receiver_id = :user2) OR (user_id = :user2 AND receiver_id = :user1)',
			'params' => array(':user1' => $user1, ':user2' => $user2),
		));
		return $this;
	}

	public function isNew() {
		return (boolean)$this->new;
	}

	/**
	 * Unmarks as new
	 */
	public function unmarkNew() {
		$this->new = "0";
		return $this->save();
	}
}