Symfony2或Doctrine2不会反序列化值

时间:2013-10-08 20:56:48

标签: symfony doctrine-orm symfony-2.3

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

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;
    }

}

2 个答案:

答案 0 :(得分:0)

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

  • Doctrine DBAL使用type="array" check here for more info
  • 负责序列化/反序列化
  • 我插入值时犯了一个错误,因为我正在做一个不需要的serialize,因为正如我在Doctrine之前所说的那样。见下文:

不正确:

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

正确:

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

这就是全部,希望对其他人有帮助

答案 1 :(得分:-1)

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

控制您的数据库,该值应该是以s:

开头的序列化字符串

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