1

我遇到了一个在尝试坚持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:{}} |
+----+-----------------------------------------------------------------------------------------------------------------------------+

有人知道发生了什么吗?

4

1 回答 1

1

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

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

array:serialize()使用and将 SQL CLOB 映射到 PHP 数组的类型unserialize()

于 2013-08-16T19:45:42.707 回答