Saba
Saba

Reputation: 159

Deserialise json object to doctrine entity

need your help.

I have json object, that looks like:

{
  "succes": true,
  "count": 6,
  "result": {
    "main": [
      {
        "id": 21,
        "name": "\u0421\u043f\u0443\u0442\u043d\u0438\u043a",
        "url": "\/cinema\",
        "image": "\/upload\/kinoteatrs\/21\/1340272755sputnik-.jpg",
        "vote": "8,2",
        "count_vote": "1479 \u0433\u043e\u043b\u043e\u0441\u043e\u0432",
        "phone": "(044) 243-46-32; (044) 243-46-33 (\u0430\u0432\u0442\u043e\u043e\u0442\u0432.)",
        "address": "\u0433. \u041a\u0438\u0435\u0432, \u0443\u043b. \u0418\u0441\u043a\u0440\u043e\u0432\u0441\u043a\u0430\u044f, 18"
      },
      {
        "id": 126,
        "name": "\u041c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0435\u043a\u0441 \u0432 \u0422\u0420\u0426 \"Sky Mall\"",
        "url": "\/cinema\",
        "image": "\/upload\/kinoteatrs\/126\/1429607129megapleks.png",
        "vote": "7,4",
        "count_vote": "829 \u0433\u043e\u043b\u043e\u0441\u043e\u0432",
        "phone": "(096)129 48 54, (044) 290 83 75",
        "address": "\u0433. \u041a\u0438\u0435\u0432, \u043f\u0440-\u0442 \u0413\u0435\u043d\u0435\u0440\u0430\u043b\u0430 \u0412\u0430\u0442\u0443\u0442\u0438\u043d\u0430, 2-\u0422"
      },
      {
        "id": 108,
        "name": "\u041c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0435\u043a\u0441 \u0432 \u0422\u0420\u0426 \"\u041a\u043e\u043c\u043e\u0434\"",
        "url": "\/cinema\",
        "image": "\/upload\/kinoteatrs\/108\/1429607154multipleks-v-komode.png",
        "vote": "7,1",
        "count_vote": "409 \u0433\u043e\u043b\u043e\u0441\u043e\u0432",
        "phone": "(044)-593-35-77, 593-35-80",
        "address": "\u0433. \u041a\u0438\u0435\u0432, \u0443\u043b. \u041b\u0443\u043d\u0430\u0447\u0430\u0440\u0441\u043a\u043e\u0433\u043e 4, 4-\u044d\u0442\u0430\u0436"
      }
    ],
    "unmain": [
      {
        "id": 14,
        "name": "\u041b\u0435\u0439\u043f\u0446\u0438\u0433",
        "url": "\/cinema\",
        "image": "\/upload\/kinoteatrs\/14\/1332685506lejpcig.png",
        "vote": "8,0",
        "count_vote": "3402 \u0433\u043e\u043b\u043e\u0441\u0430",
        "phone": "407-19-93, 403-86-59, 403-86-60",
        "address": "\u0433. \u041a\u0438\u0435\u0432, \u043f\u0440\u043e\u0441\u043f. \u041b\u0435\u0441\u044f \u041a\u0443\u0440\u0431\u0430\u0441\u0430, 8"
      },
      {
        "id": 13,
        "name": "\u0424\u043b\u043e\u0440\u0435\u043d\u0446\u0438\u044f",
        "url": "\/cinema\",
        "image": "\/upload\/kinoteatrs\/13\/1332686274florencija.png",
        "vote": "8,0",
        "count_vote": "2134 \u0433\u043e\u043b\u043e\u0441\u0430",
        "phone": "(044) 515 87 81",
        "address": "\u0433.\u041a\u0438\u0435\u0432, \u043f\u0440\u043e\u0441\u043f. \u041c\u0430\u044f\u043a\u043e\u0432\u0441\u043a\u043e\u0433\u043e, 31"
      },
      {
        "id": 134,
        "name": "\u0411\u0430\u0442\u0442\u0435\u0440\u0444\u043b\u044f\u0439 \u041a\u043e\u0441\u043c\u043e\u043f\u043e\u043b\u0438\u0442",
        "url": "\/cinema\",
        "image": "\/upload\/kinoteatrs\/134\/1332684447bolshevik.jpg",
        "vote": "7,6",
        "count_vote": "1512 \u0433\u043e\u043b\u043e\u0441\u043e\u0432",
        "phone": "200-90-18, 200-90-20; 200-90-10, 200-90-12 (\u0430\u0432\u0442\u043e\u043e\u0442\u0432.)",
        "address": "\u0433. \u041a\u0438\u0435\u0432, \u0443\u043b. \u0413\u0435\u0442\u044c\u043c\u0430\u043d\u0430, 6"
      }
    ]
  }
}

And I have entity

<?php
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 *
 * @ORM\Entity
 * @ORM\Table(name="cinemas")
 */
class Cinemas
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=100)
     */
    private $name;
    /**
     * @ORM\Column(type="string", length=100)
     */
    private $phone;
    /**
     * @ORM\Column(type="string", length=100)
     */
    private $address;
    /**
     * @ORM\Column(type="string", length=100)
     */
    private $url;
    /**
     * @ORM\Column(type="string", length=100)
     */
    private $image;
    /**
     * @ORM\Column(type="string", length=100)
     */
    private $vote;
    /**
     * @ORM\Column(type="string", length=100)
     */
    private $countVote;


    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return mixed
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param mixed $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * @return mixed
     */
    public function getPhone()
    {
        return $this->phone;
    }

    /**
     * @param mixed $phone
     */
    public function setPhone($phone)
    {
        $this->phone = $phone;
    }

    /**
     * @return mixed
     */
    public function getAddress()
    {
        return $this->address;
    }

    /**
     * @param mixed $address
     */
    public function setAddress($address)
    {
        $this->address = $address;
    }

    /**
     * @return mixed
     */
    public function getUrl()
    {
        return $this->url;
    }

    /**
     * @param mixed $url
     */
    public function setUrl($url)
    {
        $this->url = $url;
    }

    /**
     * @return mixed
     */
    public function getImage()
    {
        return $this->image;
    }

    /**
     * @param mixed $image
     */
    public function setImage($image)
    {
        $this->image = $image;
    }

    /**
     * @return mixed
     */
    public function getVote()
    {
        return $this->vote;
    }

    /**
     * @param mixed $vote
     */
    public function setVote($vote)
    {
        $this->vote = $vote;
    }

    /**
     * @return mixed
     */
    public function getCountVote()
    {
        return $this->countVote;
    }

    /**
     * @param mixed $countVote
     */
    public function setCountVote($countVote)
    {
        $this->countVote = $countVote;
    }


}

The questing is: What is the best way to deserialise this json and persist all objects inside main and unmain to database. Need best practise, thank you!

Update: Well, actually, I think I describe my propose in a wrong way.. I know how to obtain instance of std class from this json. For example we have:

[0] => stdClass Object
            (
                [id] => 21
                [name] => text
                [url] => /cinema/sputnik-
                [image] => /upload/kinoteatrs/21/1340272755sputnik-.jpg
                [vote] => 8,2
                [count_vote] => 1479 votes
                [phone] => (014) 223-46-32; (054) 245-46-33
                [address] => address
            )

The questing is how this std class convert to Cinema entity.

I think it should be make with $serializer->deserialize();

Yes, I can encode this objects to json again and then do like

$serializer->deserialize($data, 'Acme\Cinema[]', 'json');

or we can set every property via loop

But it not looks like solution..

Upvotes: 0

Views: 3961

Answers (2)

Saba
Saba

Reputation: 159

Well I find the solution. Assuming we have

$cinemas => stdClass Object
        (
            [id] => 21
            [name] => text
            [url] => /cinema/sputnik-
            [image] => /upload/kinoteatrs/21/1340272755sputnik-.jpg
            [vote] => 8,2
            [count_vote] => 1479 votes
            [phone] => (014) 223-46-32; (054) 245-46-33
            [address] => address
        )

Convert it to Entity with:

        $normalizers = array(new ObjectNormalizer());

        $serializer = new Serializer($normalizers);
        $serializer->denormalize($cinemas, 'AppBundle\Entity\Cinemas');

We obtain object like this:

        [0] => AppBundle\Entity\Cinemas Object
            (
                [id:AppBundle\Entity\Cinemas:private] => 
                [name:AppBundle\Entity\Cinemas:private] => text
                [phone:AppBundle\Entity\Cinemas:private] => (014) 223-46-32; (054) 245-46-33
                [address:AppBundle\Entity\Cinemas:private] => text
                [url:AppBundle\Entity\Cinemas:private] => /cinema/sputnik-
                [image:AppBundle\Entity\Cinemas:private] => /upload/kinoteatrs/21/1340272755sputnik-.jpg
                [vote:AppBundle\Entity\Cinemas:private] => 8,2
                [countVote:AppBundle\Entity\Cinemas:private] => 1479 votes
            )

And then we can persist this with Doctrine manager. Hope this saved time for someone.

Upvotes: 3

vincenth
vincenth

Reputation: 1792

Add a static method to your entity :

static public function fromJson($data){
    $s = new self();
    $s->setId($data['id'];
    //do the same for every other fields

    return $s
}

Then loop over your json and use your freshly created method :

$d = json_decode($json);
foreach($d['result']['main'] as $item){
    $c = Cinema::fromJson($item);
}

You will need to do some tweaking to do what you intend to do.

Upvotes: 2

Related Questions