Skip to content

Json nested object deserialize as manytoone entity #73

@andychan94

Description

@andychan94

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions