0

我在发送到数据库时序列化了一些值,现在我需要对它们进行反序列化以迭代它们。在我的实体中,我有这个:

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 ProductBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use ProductBundle\DBAL\Types\StatusType;
use ProductBundle\DBAL\Types\FieldType;
use Fresh\Bundle\DoctrineEnumBundle\Validator\Constraints as DoctrineAssert;

/**
 * @ORM\Entity
 * @ORM\Table(name="product_detail")
 * @Gedmo\SoftDeleteable(fieldName="deletedAt")
 */
class ProductDetail {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="ProductDetail")
     * @ORM\JoinColumn(name="parent", referencedColumnName="id")
     */
    protected $parent;

    /**
     * @ORM\Column(type="string", length=255)
     */
    protected $description;

    /**
     * @ORM\Column(type="string", length=255)
     */
    protected $label;

    /**
     * @var string $field_type
     * @DoctrineAssert\Enum(entity="ProductBundle\DBAL\Types\FieldType")
     * @ORM\Column(name="field_type", type="FieldType", nullable=false)
     */
    protected $field_type;

    /**
     * @ORM\Column(name="values_text", type="array")
     */
    protected $values_text;

    /**
     * @ORM\Column(type="string", length=255)
     */
    protected $measure_unit;

    /**
     * @var string $status
     * @DoctrineAssert\Enum(entity="ProductBundle\DBAL\Types\StatusType")
     * @ORM\Column(name="status", type="StatusType", nullable=false)
     */
    protected $status;

    /**
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(name="created", type="datetime")
     */
    protected $created;

    /**
     * @Gedmo\Timestampable(on="update")
     * @ORM\Column(name="modified", type="datetime")
     */
    protected $modified;

    /**
     * @ORM\Column(name="deletedAt", type="datetime", nullable=true)
     */
    protected $deletedAt;

    /**
     * @ORM\ManyToMany(targetEntity="CategoryBundle\Entity\Category", inversedBy="pd_category", cascade={"persist"})
     * @ORM\JoinTable(name="product_detail_has_category",
     *      joinColumns={@ORM\JoinColumn(name="detail", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="category", referencedColumnName="id")}
     *      )
     */
    protected $category;

    /**
     * @ORM\ManyToMany(targetEntity="ProductBundle\Entity\DetailGroup", inversedBy="productDetail", cascade={"persist"})
     * @ORM\JoinTable(name="detail_group_has_product_detail",
     *      joinColumns={@ORM\JoinColumn(name="detail", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="kgroup", referencedColumnName="id")}
     *      )
     */
    protected $detail_group;

    /**
     * @ORM\Column(name="to_product", type="boolean")
     */
    protected $to_product;

    public function __construct() {
        $this->detail_group = new \Doctrine\Common\Collections\ArrayCollection();
        $this->category = new \Doctrine\Common\Collections\ArrayCollection();
    }

    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(\CategoryBundle\Entity\Category $category) {
        $this->category[] = $category;
    }

    public function getCategory() {
        return $this->category;
    }

    public function setDetailGroup(\ProductBundle\Entity\DetailGroup $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;
    }

}
4

2 回答 2

0

好的,在阅读并检查我的代码后,我意识到问题出在哪里:

不正确:

$entity->setValuesText(serialize($form->get('values_text')->getData()));

正确的:

$entity->setValuesText($form->get('values_text')->getData());

仅此而已,希望对其他人有所帮助

于 2013-10-09T16:05:47.067 回答
-1

Doctrine 2 DBAL ArrayType 不检查数组并序列化每种类型的参数。

控制您的数据库,该值应该是一个以开头的序列化字符串s:

参考:https ://github.com/doctrine/dbal/blob/master/lib/Doctrine/DBAL/Types/ArrayType.php

于 2013-10-08T22:03:54.243 回答