开发者

In CActiveRecord of Yii, how we return a property has SET datatype as integer

Please look at below example:

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)

With Yii, using CActiveRecord->myset always returns a string which is 开发者_如何转开发separated by comma. How could I return as a integer like above?


You could overwrite the getter method in your active record class or create a new one.

private _mysetInt = array('Sports' => 2, [...]);

function getMyset(){
  $return = 0;
  foreach(explode(',',$this->myset) AS $value) {
    $return += $this->_mysetInt[$value];
  };
  return $return;
}

Also the way of getting these integer values should be adjusted to your needs, the above example is just pseudo-code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜