我在发送到数据库时序列化了一些值,现在我需要反序列化它们以迭代它们。在我的实体中,我有这个:
public function getValuesText() {
return $this->values_text;
}
然后在模板中我显示为:
{{ element.getValuesText }}
但我得到这个原始结果:
a:3:{i:1;s:7:"Value 1";i:2;s:7:"Value 2";i:3;s:7:"Value 3";}
而且我不知道如何迭代它以获取键,值,什么是失败?
更新:包括映射信息
这是:
<?php
namespace ProductBundleEntity;
use DoctrineORMMapping as ORM;
use GedmoMappingAnnotation as Gedmo;
use ProductBundleDBALTypesStatusType;
use ProductBundleDBALTypesFieldType;
use FreshBundleDoctrineEnumBundleValidatorConstraints as DoctrineAssert;
/**
* @ORMEntity
* @ORMTable(name="product_detail")
* @GedmoSoftDeleteable(fieldName="deletedAt")
*/
class ProductDetail {
/**
* @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORMManyToOne(targetEntity="ProductDetail")
* @ORMJoinColumn(name="parent", referencedColumnName="id")
*/
protected $parent;
/**
* @ORMColumn(type="string", length=255)
*/
protected $description;
/**
* @ORMColumn(type="string", length=255)
*/
protected $label;
/**
* @var string $field_type
* @DoctrineAssertEnum(entity="ProductBundleDBALTypesFieldType")
* @ORMColumn(name="field_type", type="FieldType", nullable=false)
*/
protected $field_type;
/**
* @ORMColumn(name="values_text", type="array")
*/
protected $values_text;
/**
* @ORMColumn(type="string", length=255)
*/
protected $measure_unit;
/**
* @var string $status
* @DoctrineAssertEnum(entity="ProductBundleDBALTypesStatusType")
* @ORMColumn(name="status", type="StatusType", nullable=false)
*/
protected $status;
/**
* @GedmoTimestampable(on="create")
* @ORMColumn(name="created", type="datetime")
*/
protected $created;
/**
* @GedmoTimestampable(on="update")
* @ORMColumn(name="modified", type="datetime")
*/
protected $modified;
/**
* @ORMColumn(name="deletedAt", type="datetime", nullable=true)
*/
protected $deletedAt;
/**
* @ORMManyToMany(targetEntity="CategoryBundleEntityCategory", inversedBy="pd_category", cascade={"persist"})
* @ORMJoinTable(name="product_detail_has_category",
* joinColumns={@ORMJoinColumn(name="detail", referencedColumnName="id")},
* inverseJoinColumns={@ORMJoinColumn(name="category", referencedColumnName="id")}
* )
*/
protected $category;
/**
* @ORMManyToMany(targetEntity="ProductBundleEntityDetailGroup", inversedBy="productDetail", cascade={"persist"})
* @ORMJoinTable(name="detail_group_has_product_detail",
* joinColumns={@ORMJoinColumn(name="detail", referencedColumnName="id")},
* inverseJoinColumns={@ORMJoinColumn(name="kgroup", referencedColumnName="id")}
* )
*/
protected $detail_group;
/**
* @ORMColumn(name="to_product", type="boolean")
*/
protected $to_product;
public function __construct() {
$this->detail_group = new DoctrineCommonCollectionsArrayCollection();
$this->category = new DoctrineCommonCollectionsArrayCollection();
}
public function getId() {
return $this->id;
}
public function setParent(ProductDetail $parent = null) {
$this->parent = $parent;
}
public function getParent() {
return $this->parent;
}
public function setDescription($description) {
$this->description = $description;
}
public function getDescription() {
return $this->description;
}
public function setLabel($label) {
$this->label = $label;
}
public function getLabel() {
return $this->label;
}
public function setFieldType($field_type) {
$this->field_type = $field_type;
}
public function getFieldType() {
return $this->field_type;
}
public function setValuesText($values_text) {
$this->values_text = $values_text;
}
public function getValuesText() {
return $this->values_text;
}
public function setMeasureUnit($measure_unit) {
$this->measure_unit = $measure_unit;
}
public function getMeasureUnit() {
return $this->measure_unit;
}
public function setStatus($status) {
$this->status = $status;
}
public function getStatus() {
return $this->status;
}
public function setCreated($param) {
$this->created = $param;
return true;
}
public function getCreated() {
return $this->created;
}
public function setModified($param) {
$this->modified = $param;
return true;
}
public function getModified() {
return $this->modified;
}
public function setCategory(CategoryBundleEntityCategory $category) {
$this->category[] = $category;
}
public function getCategory() {
return $this->category;
}
public function setDetailGroup(ProductBundleEntityDetailGroup $detailGroup) {
$this->detail_group[] = $detailGroup;
}
public function getDetailGroup() {
return $this->detail_group;
}
public function getDeletedAt() {
return $this->deletedAt;
}
public function setDeletedAt($deletedAt) {
$this->deletedAt = $deletedAt;
}
public function setToProduct($to_product) {
$this->to_product = $to_product;
}
public function getToProduct() {
return $this->to_product;
}
}
好的,在阅读并检查我的代码后,我意识到问题出在哪里:
- 原则 DBAL 负责序列化/反序列化,使用
type="array"
查看此处以获取更多信息 - 当我插入值时,我犯了一个错误,因为我正在做一个不需要的
serialize
因为正如我之前所说 教义 照顾这个。见下文:
不對:
$entity->setValuesText(serialize($form->get('values_text')->getData()));
正确:
$entity->setValuesText($form->get('values_text')->getData());
仅此而已,希望对别人有帮助
原则 2 DBAL ArrayType 不检查数组并序列化每种类型的参数。
控制数据库,该值应是以 s:
开头的序列化字符串
参考: https://github.com/doctrine/dbal/blob/master/lib/Doctrine/DBAL/Types/ArrayType.php