请看下面的例子:
mysql> SELECT rowid, myset, myset+0
-> FROM set_test;
+-------+-----------------------+---------+
| rowid | myset | myset+0 |
+-------+-----------------------+---------+
| 1 | Sports | 2 |
| 2 | Travel,Sports | 3 |
| 3 | Travel,Dancing | 5 |
| 4 | Travel,Sports | 3 |
| 5 | Travel,Sports,Dancing | 7 |
| 6 | Travel,Dancing | 5 |
| 7 | Sports | 2 |
| 8 | Travel,Dancing | 5 |
+-------+-----------------------+---------+
8 rows in set (0.00 sec)
使用Yii,使用CActiveRecord->myset
始终返回以逗号分隔的字符串。
我怎么能像上面的integer
那样回来?
答案 0 :(得分:2)
您可以覆盖活动记录类中的getter方法或创建一个新方法。
private _mysetInt = array('Sports' => 2, [...]);
function getMyset(){
$return = 0;
foreach(explode(',',$this->myset) AS $value) {
$return += $this->_mysetInt[$value];
};
return $return;
}
此外,获取这些整数值的方法应根据您的需要进行调整,上面的示例只是伪代码。