php 如何将Doctrine对象转换为json
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1934443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to convert Doctrine object into json
提问by cc96ai
I am using Doctrine 1.2, how could I get the query object into json / array format?
我正在使用 Doctrine 1.2,如何将查询对象转换为 json/array 格式?
$user = Doctrine_Query::create()
->select('u.id, u.username, u.firstname, u.lastname')
->from('User u')
->orderby('u.id')
->execute();
回答by Pascal MARTIN
A solution might be to use the toArray()method on the $userobject, to have a simple array containing only the data youre interested in, and, then, use json_encodeto convert that PHP array to a JSON string.
一个解决方案可能是toArray()在$user对象上使用该方法,创建一个仅包含您感兴趣的数据的简单数组,然后使用json_encode该方法将该 PHP 数组转换为 JSON 字符串。
Something like this, I suppose :
像这样,我想:
$user = Doctrine_Query::create()
->select('u.id, u.username, u.firstname, u.lastname')
->from('User u')
->orderby('u.id')
->execute();
$userArray = $user->toArray();
$json = json_encode($userArray);
(Not tested, but it should not be too far from working...)
(未测试,但它应该离工作不远......)
回答by Tadas Sasnauskas
Trouble with $record->exportTo('json')is that it will export allrecord fields. And in most cases it's not a desirable behaviour (for e.g. when this piece of json should be passed to browser). One way to limit the scope of export is to specify fields in DQL select:
麻烦$ record-> exportTo(“JSON”)是,它会导出所有记录的字段。而且在大多数情况下,这不是一种理想的行为(例如,当这段 json 应该传递给浏览器时)。限制导出范围的一种方法是在 DQL select 中指定字段:
$user = Doctrine_Query::create()
->select('u.id, u.name')
->from('User u')
->addWhere('u.id = ?', $id)
->fetchOne();
$user_json = $user->exportTo('json');
$user_json then will have something like this:
$user_json 然后会有这样的东西:
{
"id": 123,
"name": "John Smith",
"password": null,
"deleted": null
}
So it does not expose "password" field value but doesexpose underlying database structure. Again, might not be what we want. What I do is specify fields in DQL select + fetch as array then json encode:
因此它不会公开“密码”字段值,但会公开底层数据库结构。同样,可能不是我们想要的。我所做的是在 DQL 中指定字段 select + fetch as array 然后 json 编码:
$user = Doctrine_Query::create()
->select('u.id, u.name')
->from('User u')
->addWhere('u.id = ?', $id)
->fetchOne(array(), Doctrine::HYDRATE_ARRAY);
$user_json = json_encode($user);
In this case json will look like something like:
在这种情况下,json 将类似于:
{
"id": 123,
"name": "John Smith"
}
回答by cc96ai
$users2 = Doctrine_Query::create()
->select('u.id, u.username, u.firstname, u.lastname')
->from('User u')
->orderby('u.id');
$tmp2 = $users2->fetchArray();
I don't know why the toArray() will give the other field in the table, e.g. it will have the "password" field, it seems fetchArray() can give me the correct fields in query.
我不知道为什么toArray() 会给表中的另一个字段,例如它会有“密码”字段,似乎fetchArray() 可以给我查询中的正确字段。
toArray()
toArray()
Array
(
[0] => Array
(
[id] => 1
[username] => user1
[password] => password
[firstname] => John
[lastname] => Smith
)
fetchArray()
fetchArray()
Array
(
[0] => Array
(
[id] => 1
[username] => user1
[firstname] => John
[lastname] => Smith
)
回答by inalgnu
For JSON :
对于 JSON :
$user->exportTo('json');
;-)
;-)
回答by ScorpioT1000
Now Doctrine ORM Transformationsis out, it allows to convert entities to scalar arrays and back
现在Doctrine ORM Transformations出来了,它允许将实体转换为标量数组并返回
class User implements ITransformable {
use Transformable;
// ...
echo json_encode($user->toArray());

