public static function testArray($returnType = 'array', $refresh = false, $autoset = true) { $cachekey = 'test-array-array'; if ($refresh) { BCacheHelper::delete($cachekey); } $data = self::get($cachekey, function () { $users = User::model()->findAll(array('condition' => 'isdelete=0', 'limit' => 10)); $users = BArrayHelper::getModelAttributesArray($users, 'userid', BCacheHelper::getPreChar()); return $users; }, $autoset); return $data; }
/** * 组合返回JSON数据格式 * @author wjh * @version 2014-5-10 * @param mixed $data string text or array * @param string $err error message * @param null $type 返回类型,json | jsonp * @param null $jsonpfunc 如果 $type= jsonp ,用来指定回调函数 * @throws Exception * @return array */ public static function encodeJsonReturn($data, $err = null, $type = 'json', $jsonpfunc = 'jsonpCallback') { if (is_null($data)) { $data = array(); } elseif (BArrayHelper::isCActiveRecord($data)) { $data = $data->attributes; } elseif (BArrayHelper::isCActiveRecordArray($data)) { $data = BArrayHelper::getModelAttributesArray($data); } else { //$data = array(); } if (is_array($data)) { $data = SnapshotHelper::encodeArray($data); } if (!empty($type) && $type == 'jsonp') { $str = sprintf('%s({"err":[%s],"data":%s})', $jsonpfunc, $err, $data); } else { $str = sprintf('{"err":[%s],"data":%s}', $err, $data); } return $str; }