【发布时间】:2016-12-21 16:34:15
【问题描述】:
需要你的帮助。
我有 json 对象,看起来像:
{
"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"
}
]
}
}
我有实体
<?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;
}
}
问题是:反序列化此 json 并将 main 和 unmain 中的所有对象持久保存到数据库的最佳方法是什么。 需要最佳实践,谢谢!
更新:好吧,实际上,我认为我用错误的方式描述了我的建议。我知道如何从这个 json 获取 std 类的实例。例如我们有:
[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
)
问题是这个 std 类如何转换为 Cinema 实体。
我认为应该使用$serializer->deserialize();
是的,我可以再次将这些对象编码为 json,然后像这样
$serializer->deserialize($data, 'Acme\Cinema[]', 'json');
或者我们可以通过循环设置每个属性
但它看起来不像解决方案..
【问题讨论】:
-
画一堵代码墙并要求最佳实践并不能掩盖您没有明显努力解决问题的事实
-
@RiggsFolly Okey,我现在讨论这个,但是如何从主要和非主要部分开始解码。因为我需要内部的对象将它们直接保存到 Doctrine Manager。那你&
-
json_decode JSON 字符串,它为您提供一个具有属性 2 的对象,其中
$obj->result->main[]和$obj->result->umain[]每个都是一个数组。因此,编写一个 foreach 外观来处理这些数组并使用您想要从这些数组中获取的任何数据加载您的Cinemas对象
标签: php serialization doctrine-orm deserialization symfony