Php 用条令存储序列化对象
我在存储php序列化对象时遇到了一些问题, 当我使用以下代码存储时:Php 用条令存储序列化对象,php,symfony,serialization,doctrine-orm,Php,Symfony,Serialization,Doctrine Orm,我在存储php序列化对象时遇到了一些问题, 当我使用以下代码存储时: $item = new \Company\MyBundle\Entity\Frontend\table(); $item->setData(serialize($myPhpObject)); $this->_em->persist($item); $this->_em->flush(); 然后在另一个例子中: $item = $this->_em->getRepository('My
$item = new \Company\MyBundle\Entity\Frontend\table();
$item->setData(serialize($myPhpObject));
$this->_em->persist($item);
$this->_em->flush();
然后在另一个例子中:
$item = $this->_em->getRepository('MyBundle:frontend\table')->findOneById(...);
echo $item->getData(); // display only a part of the serialized object
我不知怎么找到了解决办法,但我想还有更好的办法:
$item->setData(json_encode(serialize($myPhpObject)));
当我使用它时,所有序列化的字符串都被存储,我可以正确地取消序列化它。你知道怎么回事吗
-编辑-
这是从列数据设置的一个示例:
data:
type: string
length: null
fixed: false
nullable: true
column: data
我假设您使用注释作为配置。然后更改您的实体:
// src/Company/MyBundle/Entity/Frontend/table.php
// ...
/**
* @var resource
*
* (non-PHPdoc)
* @ORM\Column(name="data", type="object")
*/
private $data;
// ...
然后在控制器内部,并没有必要序列化/取消序列化对象,条令将处理这个问题
// src/Company/MyBundle/Controller/SomeController.php
// ...
// write
$item = new \Company\MyBundle\Entity\Frontend\table();
$item->setData($myPhpObject);
$this->_em->persist($item);
$this->_em->flush();
// ...
// read
$item = $this->getDoctrine()->getRepository('CompanyMyBundle:table')->find($id);
$myPhpObject = $item->getData();
var_dump($myPhpObject);
// ...
祝你好运 将
ArrayType
(又称实体注释中的array
)或ObjectType
(又称实体注释中的object
)用于条令类型可能会因为此问题而存在一些潜在的安全问题(导致拒绝服务)()
一、 我自己会使用JsonObjectType 请提供有关您正在序列化的对象的更多详细信息,因为有许多解决方案取决于您的对象。请注意,在评论和评论中有很好的手册,您可以找到许多案例。您尝试过使用Doctrine的内置
对象
字段类型吗?在getData()之后是否进行了反序列化?@pomaxa该对象是来自Swift邮件库的Swift\u Mime\u消息。@kix我必须看一看。我将问题转移到了Doctrine DBAL项目,它所属的项目:)