1

I am attempting to return a list of comments related to a an entity. The query results on when it runs and returns, the related field does not provide a meaningful result.

Here is the comment entity declarations

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

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

This is my controller snippets of codes that fetches all the comment a user commented to a particular book

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

the below function calls the function above

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;
    }

Here is the result of attempt

[{"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"}}

Please how can I return the objects properties from this result instead of this

"userid":{"__initializer__":{},"__cloner__":{},"__isInitialized__":false},"bookid":{"path":"http:\/\/10.0.2.2:88\/xxx\/web\/uploads\/pdf\/5ub3uy8zv09cee2avi11.pdf"

1 Answer 1

1

Try accessing the object properties:

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

instead of

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

Hope this help

Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.