Skip to content

Json nested object deserialize as manytoone entity #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
andychan94 opened this issue Apr 10, 2018 · 2 comments
Open

Json nested object deserialize as manytoone entity #73

andychan94 opened this issue Apr 10, 2018 · 2 comments

Comments

@andychan94
Copy link

andychan94 commented Apr 10, 2018

My api returns this json at my-api/house/3:

{
    "id": 3,
    "status": 1,
    "title": "sometitle",
    "agency": {
        "id": 1,
        "name": "AgencyName",
        ...
        "created_at": "-0001-11-30T00:00:00+01:00",
        "updated_at": "-0001-11-30T00:00:00+01:00"
    },
    "price": "123123",
    ...
    "created_at": "-0001-11-30T00:00:00+01:00",
    "updated_at": "-0001-11-30T00:00:00+01:00",
    "photos": [
        {
            "id": 1,
            "path": "img1.jpg",
            "created_at": "2018-04-05T15:29:47+02:00",
            "updated_at": "2018-04-05T15:29:47+02:00"
        },
        {
            "id": 2,
            "path": "img2.jpg",
            "created_at": "2018-04-05T15:35:28+02:00",
            "updated_at": "2018-04-05T15:35:28+02:00"
        }
    ]
}

There is no other Rest\Get in my api service.
I specified the @Datasource in my House entity

/**
 * House
 *
 * @ORM\Entity
 * @ORM\Table(name="house")
 * @DataSource\Select("http://my-api/house/{id}")
 */
class House
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

Each House has a OneToMany relationship with Photo and ManyToOne with Agency.

\Entity\House.php
/**
   * @ORM\OneToMany(targetEntity="Photo", mappedBy="house")
   */
   private $photos;
/**
   * @ORM\ManyToOne(targetEntity="Agency", inversedBy="houses")
   * @ORM\JoinColumn(name="agency", referencedColumnName="id")
   */
   private $agency;

    /**
     * Set agency
     *
     * @param Agency $agency
     *
     * @return House
     */
    public function setAgency(Agency $agency = null)
    {
        $this->agency = $agency;

        return $this;
    }

    /**
     * Get agency
     *
     * @return Agency
     */
    public function getAgency()
    {
        return $this->agency;
    }


    /**
     * Add photo
     *
     * @param Photo $photo
     *
     * @return House
     */
    public function addPhoto(\AppBundle\Entity\Photo $photo)
    {
        $this->photos[] = $photo;

        return $this;
    }

    /**
     * Remove photo
     *
     * @param Photo $photo
     */
    public function removePhoto(\AppBundle\Entity\Photo $photo)
    {
        $this->photos->removeElement($photo);
    }

    /**
     * Get photos
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getPhotos()
    {
        return $this->photos;
    }
\Entity\Photo.php
  /**
     * @ORM\ManyToOne(targetEntity="House", inversedBy="photos")
     */
    private $house;

    /**
     * Set house
     *
     * @param House $house
     *
     * @return Photo
     */
    public function setHouse(House $house = null)
    {
        $this->house = $house;

        return $this;
    }
    /**
     * Get house
     *
     * @return House
     */
    public function getHouse()
    {
        return $this->house;
    }

Now when I parse the json in my controller:

    /**
     * @Route("/", name="homepage")
     */
    public function readAction($id = 3) {
        $em     = $this->getDoctrine()->getManager();
        $house = $em->find('AppBundle\Entity\House', $id);

        return $this->render('test.html.twig', array(
            'house' => $house
        ));

    }

And fetch photos in the view:

 <ul>
    {% for photo in house.photos %}
        <li>{{ photo.path }}</li>
    {% endfor %}
</ul>

It throws an exception:

Execution failed for request: GET http://my-api/photo?house_id=3 HTTP/1.1: HTTPCode 404, body {"error":{"code":404,"message":"Not Found"}} ").

What am I doing wrong?
(I copy pasted my entitiy files from my API project to this project I think I need to change the relationships but dont know how)

@SerethiX
Copy link
Contributor

SerethiX commented Sep 4, 2018

That's not how this Driver works.

What it does: It creates seperate Requests for each Entity / relation you request. So you need to specify a DataSource also for Photos.

The Rest Driver then appends the search parameters as query string to the url like this:

https://example.com/photo?house=1

You then have to return the json array for all photos you want to display next to house 1

@TobiasHauck
Copy link
Member

Maybe we could add this as a feature, because often you are not able to change an API (because you are not owning it) so this driver becomes useless.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants