Example #1
0
 /**
  * Create a new gallery
  *
  * @param array $data
  * @param \Zend\Form\Form $form
  * @return bool
  */
 public function create(&$data, &$form)
 {
     $entity = new GalleryEntity();
     $em = $this->getEntityManager();
     if (isset($data['gallery']['images'])) {
         $data['gallery']['images'] = json_decode($data['gallery']['images'], true);
     }
     $form->bind($entity);
     $form->setData($data);
     $form->setBindOnValidate(Form::BIND_MANUAL);
     if (!$form->isValid()) {
         return false;
     }
     $entity->setName($data['gallery']['name']);
     $entity->setUrl($this->getGalleryUrl($entity));
     if (isset($data['gallery']['parentGallery']) && !empty($data['gallery']['parentGallery'])) {
         $parent = $this->getGalleryRepository()->find($data['gallery']['parentGallery']);
         if ($parent) {
             $entity->setParentGallery($parent);
         }
     }
     // WHY THE F**K DO I HAVE TO FLUSH HERE
     $em->persist($entity);
     $em->flush();
     foreach ($data['gallery']['images'] as $join) {
         $image = $this->getImageRepository()->find($join['joinId']);
         if ($image) {
             $galleryImage = new GalleryImage($join['title'], $join['position']);
             $image->addGalleries($galleryImage);
             $entity->addImages($galleryImage);
         }
     }
     try {
         $em->persist($entity);
         $em->flush();
         $this->message = $this->getTranslator()->translate($this->getVocabulary()["MESSAGE_GALLERY_CREATED"]);
         return true;
     } catch (\Exception $e) {
         $this->message = $this->getTranslator()->translate($this->getVocabulary()["MESSAGE_GALLERY_NOT_CREATED"]);
         return false;
     }
 }
Example #2
0
 public function testSetBindOnValidateWrongFlagRaisesException()
 {
     $this->setExpectedException('Zend\Form\Exception\InvalidArgumentException');
     $this->form->setBindOnValidate(Form::VALUES_AS_ARRAY);
 }