<?php
/**
* This is the model class for table "{{user_friends}}".
*/
class UserFriend extends CActiveRecord {
/**
* @return string the associated database table name
*/
public function tableName() {
return '{{user_friends}}';
}
/**
* @return array validation rules for model attributes.
*/
public function rules() {
return array(
array('user_id, friend_id', 'required'),
array('user_id, friend_id', 'length', 'max' => 11),
array('approved', 'length', 'max' => 1),
array('message', 'safe'),
);
}
/**
* @return array relational rules.
*/
public function relations() {
return array(
'user' => array(self::BELONGS_TO, 'User', 'user_id'),
'friend' => array(self::BELONGS_TO, 'User', 'friend_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 Friend the static model class
*/
public static function model($className=__CLASS__) {
return parent::model($className);
}
/**
* Returns friendship relations
* @param int $user User id
*/
public function allUserFriends($user) {
$this->dbCriteria->mergeWith(array(
'condition' => 'user_id = :user OR friend_id = :user',
'params' => array(':user' => $user),
));
return $this;
}
/**
* Returns friendship relations
*/
public function byUsers($user1, $user2) {
$this->dbCriteria->mergeWith(array(
'condition' => '(user_id = :user1 AND friend_id = :user2) OR (user_id = :user2 AND friend_id = :user1)',
'params' => array(':user1' => $user1, ':user2' => $user2),
));
return $this;
}
/**
* Returns second user
*/
public function getFriend(User $user) {
$friend = $this->user_id == $user->id
? 'friend'
: 'user';
return $this->{$friend};
}
}