I’ve created two models: Topic and Vote. Vote contains information about user votings (ratings of topic, like/doesn’t like), associated with ip or user id if logged in.
In Topic::relations() I made a simple relation:
$cnd = Yii::app()->user->getId() ?
'user_id = '.Yii::app()->user->getId() :
'user_ip = '.ipInt(); //gets ip as an integer / long
return array(
'vote' => array(self::HAS_ONE, 'Vote','topic_id')
);
and in category, when I’m looping over all topics in category, trying to access vote, I do ex:
<?php if (empty($item->voting)): ?>
<?php echo $item->vote->like_or_not == -1 ? 'like' : 'doesn\'t like'; ?>
<?php endif; ?>
But what’s strange, I do get an error like following (pointing to like containing empty() ):
Description
Invalid argument supplied for foreach()
Source File
C:\wamp\www\framework\db\ar\CActiveFinder.php(762)
00750: {
00751: // determine the primary key value
00752: if(is_string($this->_pkAlias)) // single key
00753: {
00754: if(isset($row[$this->_pkAlias]))
00755: $pk=$row[$this->_pkAlias];
00756: else // no matching related objects
00757: return null;
00758: }
00759: else // is_array, composite key
00760: {
00761: $pk=array();
*bug*00762: foreach($this->_pkAlias as $name=>$alias)
00763: {
00764: if(isset($row[$alias]))
00765: $pk[$name]=$row[$alias];
00766: else // no matching related objects
00767: return null;
00768: }
00769: $pk=serialize($pk);
00770: }
00771:
00772: // retrieve or populate the record according to the primary key value
00773: if(isset($this->records[$pk]))
00774: $record=$this->records[$pk];