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