相关疑难解决方法(0)

如何在Symfony 2.0 AJAX应用程序中将Doctrine实体编码为JSON?

我正在开发游戏应用程序并使用Symfony 2.0.我对后端有很多AJAX请求.更多的响应是将实体转换为JSON.例如:

class DefaultController extends Controller
{           
    public function launchAction()
    {   
        $user = $this->getDoctrine()
                     ->getRepository('UserBundle:User')                
                     ->find($id);

        // encode user to json format
        $userDataAsJson = $this->encodeUserDataToJson($user);
        return array(
            'userDataAsJson' => $userDataAsJson
        );            
    }

    private function encodeUserDataToJson(User $user)
    {
        $userData = array(
            'id' => $user->getId(),
            'profile' => array(
                'nickname' => $user->getProfile()->getNickname()
            )
        );

        $jsonEncoder = new JsonEncoder();        
        return $jsonEncoder->encode($userData, $format = 'json');
    }
}
Run Code Online (Sandbox Code Playgroud)

我的所有控制器都做同样的事情:获取一个实体并将其一些字段编码为JSON.我知道我可以使用规范化器并对所有权限进行编码.但是,如果一个实体已经循环链接到其他实体呢?或实体图非常大?你有什么建议吗?

我想一下实体的一些编码模式......或者NormalizableInterface用来避免循环..,

ajax doctrine symfony doctrine-orm

87
推荐指数
10
解决办法
11万
查看次数

Symfony 2 Doctrine导出到JSON

我正在使用Symfony 2和Doctrine 2为iOS应用程序创建Web服务(JSON).

要获取我的实体我做:

$articles = $this->getDoctrine()->getRepository('UdoPaddujourBundle:MenuArticle')->findAll();
Run Code Online (Sandbox Code Playgroud)

我必须告诉你:

$article = array();
$article = $articles->toArray();
Run Code Online (Sandbox Code Playgroud)

给我以下错误:

Fatal error: Call to a member function toArray() on a non-object
Run Code Online (Sandbox Code Playgroud)

同样的事情发生了

$article = $articles->exportTo('json');
Run Code Online (Sandbox Code Playgroud)

我怎样才能创建一个json响应?

亲切的问候,Cearnau Dan

编辑:var_dump($ articles)=

array(18) {
   [0]=>
     object(Udo\PaddujourBundle\Entity\MenuArticle)#50 (4) {
    ["id":"Udo\PaddujourBundle\Entity\MenuArticle":private]=>
    int(1)
    ["name":"Udo\PaddujourBundle\Entity\MenuArticle":private]=>
    string(17) "My Article Name 1"
    ["description":"Udo\PaddujourBundle\Entity\MenuArticle":private]=>
    string(26) "My Article Description 1"
    ["price":"Udo\PaddujourBundle\Entity\MenuArticle":private]=>
    float(20)
    }
   [1]=> ...
Run Code Online (Sandbox Code Playgroud)

- 稍后编辑

我怎样才能遍历所有"属性名称"?这就是我所拥有的:

$myarray=array(); 
$myArray["name"]=array(); 
$myArray["description"]=array(); 
foreach($articles in $article) 
{ 
  array_push($myArray["name"], $article->getName());
  array_push($myArray["description"], $article->getDescription()); 
}
Run Code Online (Sandbox Code Playgroud)

php json doctrine web-services symfony

6
推荐指数
2
解决办法
2万
查看次数

如何在symfony2中将cookie附加到JSON响应?

我有JSON端点,用于将产品添加到购物车.它会检查购物车是否已存在或没有.如果没有,则创建购物车,购物车ID存储在cookie中.所以我如何将cookie附加到symfony2的JsonResponse?

在非ajax版本中,如果我从我的动作渲染模板,我可以使用:

$response = new Response();
$response->headers->setCookie(new Cookie(‘cookie_name’, ‘cookie_value’));

$this->render('<template_path>', '<array_options>', $response);
Run Code Online (Sandbox Code Playgroud)

请帮我介绍如何为JsonResponse做这件事.

php cookies symfony

4
推荐指数
1
解决办法
3196
查看次数

标签 统计

symfony ×3

doctrine ×2

php ×2

ajax ×1

cookies ×1

doctrine-orm ×1

json ×1

web-services ×1