Zend2 - 如何获取 Zend\View\Model\JsonModel 对象中的数据?
Zend2 - How to get the data in Zend\View\Model\JsonModel Object?
如何获取Zend\View\Model\JsonModel Object
中的数据?
在article之后,我在下面有这段代码,
public function create($data)
{
$form = new AlbumForm();
$album = new Album();
$form->setInputFilter($album->getInputFilter());
$form->setData($data);
if ($form->isValid()) {
$album->exchangeArray($form->getData());
$id = $this->getAlbumTable()->saveAlbum($album);
}
print_r($this->get($id));
return new JsonModel(array(
'data' => $this->get($id),
));
}
有了这个print_r($this->get($id));
,我得到了,
Zend\View\Model\JsonModel Object
(
[captureTo:protected] =>
[jsonpCallback:protected] =>
[terminate:protected] => 1
[children:protected] => Array
(
)
[options:protected] => Array
(
)
[template:protected] =>
[variables:protected] => Array
(
[data] => Album\Model\Album Object
(
[id] => 15
[artist] => The Military Wives
[title] => In My Dreams
[inputFilter:protected] =>
)
)
[append:protected] =>
)
但是我怎样才能得到下面这个作为我的结果呢?
[data] => Album\Model\Album Object
(
[id] => 15
[artist] => The Military Wives
[title] => In My Dreams
[inputFilter:protected] =>
)
由于 JsonModel
将所有传递的变量序列化为嵌套数组和字符串以将该变量表示为 JSON,因此将 Object
实例传递给 JsonModel
将毫无意义这种情况。
尝试将 Album
对象的数组表示传递给 JsonModel
:
return new JsonModel(array("data" => $album->extract()));
或
return new JsonModel(array("data" => $album->toArray()));
并且不要忘记将 toArray()
或 èxtract()
方法添加到相册模型中:
class Album
{
public function extract()
{
return [
'id' => $this->id,
'artist' => $this->artist,
// ...
];
}
}
您可能还想实现 JsonSerializable 接口。
如何获取Zend\View\Model\JsonModel Object
中的数据?
在article之后,我在下面有这段代码,
public function create($data)
{
$form = new AlbumForm();
$album = new Album();
$form->setInputFilter($album->getInputFilter());
$form->setData($data);
if ($form->isValid()) {
$album->exchangeArray($form->getData());
$id = $this->getAlbumTable()->saveAlbum($album);
}
print_r($this->get($id));
return new JsonModel(array(
'data' => $this->get($id),
));
}
有了这个print_r($this->get($id));
,我得到了,
Zend\View\Model\JsonModel Object
(
[captureTo:protected] =>
[jsonpCallback:protected] =>
[terminate:protected] => 1
[children:protected] => Array
(
)
[options:protected] => Array
(
)
[template:protected] =>
[variables:protected] => Array
(
[data] => Album\Model\Album Object
(
[id] => 15
[artist] => The Military Wives
[title] => In My Dreams
[inputFilter:protected] =>
)
)
[append:protected] =>
)
但是我怎样才能得到下面这个作为我的结果呢?
[data] => Album\Model\Album Object
(
[id] => 15
[artist] => The Military Wives
[title] => In My Dreams
[inputFilter:protected] =>
)
由于 JsonModel
将所有传递的变量序列化为嵌套数组和字符串以将该变量表示为 JSON,因此将 Object
实例传递给 JsonModel
将毫无意义这种情况。
尝试将 Album
对象的数组表示传递给 JsonModel
:
return new JsonModel(array("data" => $album->extract()));
或
return new JsonModel(array("data" => $album->toArray()));
并且不要忘记将 toArray()
或 èxtract()
方法添加到相册模型中:
class Album
{
public function extract()
{
return [
'id' => $this->id,
'artist' => $this->artist,
// ...
];
}
}
您可能还想实现 JsonSerializable 接口。