I have this table of users, which contains all users (ID,name,email,status, …)
I also have a table of relations, lets say friend requests.
requested_id,requestor_id,friend_status
In my Model, I have a fake enum of constants:
const FRIEND = 0
const USERREQUESTED =1
const OTHERREQUESTED =2
const USERREJECTED = 3
const OTHERREJECTED = 4
Now all fine and dandy, so I created my very own custom scope, which finds all relations which contains a user (found by ID), where both users have the "active" status (IE not banned)
What I now want, is to propagate this ID to all models returned by findAll();
The issue is; the table does not return nicely formatted enums, but magic numbers.
Let’s say “friendship.status = 0” means PENDING, 1 means accepted, 2 means rejected.
Now I want to convert this to one of the constants shown above.
so I tried this:
/**
* _currentId the ID of the current user in the relationship
* Used for the request types
* @var integer
*/
public $_currentId;
...
public function getRequestType() {
$this->_currentId = $this->_currentId == null ? Yii:app()->user->id : $this->_currentId;
if ($this->friend_status == 2 && $this->requestor_id == $this->_currentId) {
return self::USERREJECTED;
} else {
// All other cool cases.
}
}
...
//THIS IS THE FUNCTION THAT THE SCOPE REFERS TO
public function getAllActiveRelatedToUser($id) {
$this->_currentId = $id;
$this->getDbCriteria()->mergeWith([ /* Lots of stuff here, this works perfectly fine */ ]);
return $this;
)
I’ve tried afterFind, but basically I want to insert functionality on model instance creation, cause there might be a lot of instances and it’s just more computing to loop through it all twice.