1

I'm having a problem that I can't solve when trying to persist an ArrayCollection. This is a simplification of my entity:

/**
 * 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;
    }
}

and this is the test controller:

/**
 * 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(),
        );
    }

}

The log reads as expected:

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

)
 [] []

but when I check on the database I see:

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

does anybody know what's going on?

1 Answer 1

1

The type="array" is for arrays, not collections. So use pure arrays and add a addImages method.

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

array: Type that maps a SQL CLOB to a PHP array using serialize() and unserialize()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.