2013-10-08 43 views
0

我發送到數據庫時序列化一些值,現在我需要將它們反序列化以迭代它們。在我的實體我有這樣的:Symfony2或Doctrine2不是非序列化值

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

而且我不知道如何迭代它以獲取關鍵字,值,什麼是失敗?

UPDATE:包括映射信息

這裏是:

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

} 
+0

包括您的映射信息(註釋/ YML/XML)對於您試圖在問題中提取的實體。 – nifr

+0

@nifr完成,添加到主後 – Reynier

+0

你如何從存儲庫中獲取實體?通常該屬性在此過程中應該是非序列化的。您是否清除緩存並將數據庫架構更新爲最新版本? :) – nifr

回答

0

好,後閱讀和檢查我的代碼,我意識到其中的問題是:

  • Doctrine DBAL負責使用序列化/反序列化type="array"check here for more info
  • 我犯了一個錯誤,當我插入值sin我正在做一個不需要的serialize,因爲正如我之前說的教義照顧這一點。請看下圖:

錯誤:

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

正確:

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

這一切,希望對別人有幫助的

-1

學說2 DBAL數組類型不檢查陣列和序列化每類型的參數。

控制你的數據庫,該值應該是一個連續字符串的開頭s:

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

+0

我該如何處理這個問題?是的,在我的數據庫中,所有序列化的值都是:' – Reynier

+0

使用腳本獲取所有這些序列化的值,將它們反序列化兩次,第一次從':'到'a:',第二次從'a:'到數組,然後使用Doctrine再次存儲 –