Symfony2:ArrayCollection没有持久化

时间:2013-08-16 19:37:58

标签: php symfony doctrine-orm symfony-2.3

我遇到了一个问题,在尝试持久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 个答案:

答案 0 :(得分:1)

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

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

array:使用serialize()unserialize()

将SQL CLOB映射到PHP数组的类型
相关问题