Model类增加toJson方法输出JSON字符串

This commit is contained in:
thinkphp
2016-05-03 12:28:15 +08:00
parent c7e363ef3b
commit 289507c0ce

View File

@@ -169,6 +169,17 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return !empty($item) ? $item : []; return !empty($item) ? $item : [];
} }
/**
* 转换当前模型对象为JSON字符串
* @access public
* @param integer $options json参数
* @return string
*/
public function toJson($options = JSON_UNESCAPED_UNICODE)
{
return json_encode($this->toArray(), $options);
}
/** /**
* 获取当前模型对象的主键 * 获取当前模型对象的主键
* @access public * @access public
@@ -980,7 +991,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
public function __toString() public function __toString()
{ {
return json_encode($this->toArray()); return $this->toJson();
} }
// JsonSerializable // JsonSerializable