Ejemplo n.º 1
0
 /**
  * 转换普通数组为 model
  * @author wjh 20141222
  * @param $model
  * @param $attributes
  * @return mixed
  */
 public static function getModel($model, $attributes)
 {
     if (BArrayHelper::isCActiveRecord($model)) {
         $obj = clone $model;
     } else {
         $obj = new $model();
     }
     $obj->attributes = $attributes;
     return $obj;
 }
Ejemplo n.º 2
0
 /**
  * 组合返回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;
 }