2013-08-16 102 views
1

我遇到了一个问题,当试图持续ArrayCollection时我无法解决。这是我的实体的简化:Symfony2:ArrayCollection不会持久

/** 
* Gallery 
* 
* @ORM\Table() 
* @ORM\Entity(repositoryClass="Estudio448\TarsitanoBundle\Entity\GalleryRepository") 
* @ORM\HasLifecycleCallbacks 
*/ 
class Gallery 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

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

    public function __construct() { 
     $this->images = new ArrayCollection(); 
    } 

    public function getImages() { 
     return $this->images; 
    } 
} 

,这是测试控制器:

/** 
* Gallery controller. 
* 
* @Route("/admin/gallery") 
*/ 
class GalleryController extends Controller 
{ 
    /** 
    * Edits an existing Gallery entity. 
    * 
    * @Route("/{id}", name="admin_gallery_update") 
    * @Method("POST") 
    * @Template("Estudio448TarsitanoBundle:Gallery:edit.html.twig") 
    */ 
    public function updateAction(Request $request, $id) 
    { 
     $em = $this->getDoctrine()->getManager(); 

     $entity = $em->getRepository('Estudio448TarsitanoBundle:Gallery')->find($id); 

     if (!$entity) { 
      throw $this->createNotFoundException('Unable to find Gallery entity.'); 
     } 

     $logger = $this->get('logger'); 

     $deleteForm = $this->createDeleteForm($id); 
     $editForm = $this->createForm(new GalleryType(), $entity); 
     $editForm->bind($request); 

     if ($editForm->isValid()) { 
      $entity->upload(); 
      $entity->getImages()->add("test!"); 
      $logger->warn(print_r($entity->getImages(), true)); // this prints as expected. 

      $em->persist($entity); 
      $em->flush(); 

      return $this->redirect($this->generateUrl('admin_gallery_edit', array('id' => $id))); 
     } 

     return array(
      'entity'  => $entity, 
      'edit_form' => $editForm->createView(), 
      'delete_form' => $deleteForm->createView(), 
     ); 
    } 

} 

日志读取预期:

app.WARNING: Doctrine\Common\Collections\ArrayCollection Object 
(
    [_elements:Doctrine\Common\Collections\ArrayCollection:private] => Array 
     (
      [0] => test! 
     ) 

) 
[] [] 

,但是当我检查数据库上我见:

+----+-----------------------------------------------------------------------------------------------------------------------------+ 
| id | images                              | 
+----+-----------------------------------------------------------------------------------------------------------------------------+ 
| 12 | O:43:"Doctrine\Common\Collections\ArrayCollection":1:{s:54:" Doctrine\Common\Collections\ArrayCollection _elements";a:0:{}} | 
+----+-----------------------------------------------------------------------------------------------------------------------------+ 

做一个你知道发生了什么事吗?

回答

1

type="array"用于数组而不是集合。所以使用纯数组并添加addImages方法。

public function addImages($image) { 
    $this->images[] = $image; 
    return $this; 
} 

阵列:类型,使用serialize()unserialize()

+0

感谢SQL CLOB映射到PHP数组!这解决了它。 – Chango