从symfony json结果中的相关数据返回对象的属性

时间:2017-12-18 13:10:10

标签: php json symfony

我正在尝试返回与实体相关的评论列表。查询结果在运行和返回时,相关字段不会提供有意义的结果。

这是评论实体声明

string url = "https://mysharepoint.com/sites/testsite";

                    SecureString f_SecurePass = new SecureString();
                    foreach (char ch in pass)
                        f_SecurePass.AppendChar(ch);

                    clientcontext = new ClientContext(url);

                    var credentials = new SharePointOnlineCredentials(userid, f_SecurePass);                  

                    clientcontext.Credentials = credentials;
                    Web web = clientcontext.Web;

                    clientcontext.Load(web, website => website.Lists);
                    clientcontext.ExecuteQuery();

                WebCreationInformation wci = new WebCreationInformation();
                wci.Url = "/TestAPISite2";
                wci.Title = "TestAPISite2";
                wci.Language = 1033;

                var newsite =  clientcontext.Site.RootWeb.Webs.Add(wci);
                clientcontext.ExecuteQuery();

这是我的控制器代码片段,用于获取用户评论到特定图书的所有评论

/**
     * @var Books
     *
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Books")
     */
    private $imagefk;

    /**
     * @var User
     *
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Users")
     */
    private $userfk;

以下函数调用上面的函数

private function serializeComments(Comments $cmt) {
        return array(
            'message' => $cmt->getMessage(),
            'userid' => $cmt->getUserfk(),
            'bookid' => $cmt->getBookfk(),
        );
    }

以下是尝试的结果

public function getAllCommentsAction($books)
    {


    $messages = $em->getRepository("AppBundle")->findBy(
        array(
            "imagefk" => $books
        )
    );


    $data = array();

    foreach ($messages as $message)
    {
        array_push($data, $this->serializeComments($message));
    }
    $response = new Response(json_encode($data), 200);
    $response->headers->set('Content-Type', 'application/json');
    return $response;
    }

请问如何从此结果中返回对象属性而不是此

[{"message":"This is comment for a user one","userid":{"__initializer__":{},"__cloner__":{},"__isInitialized__":false},"bookid":{"path":"http:\/\/10.0.2.2:88\/xxx\/web\/uploads\/pdf\/5ub3uy8zv09cee2avi11.pdf"}}

1 个答案:

答案 0 :(得分:1)

尝试访问对象属性:

'userid' => $cmt->getUserfk()->getId(),

而不是

'userid' => $cmt->getUserfk(),

希望这个帮助