Exemple #1
0
 /**
  *
  * @param \Music\Model\Artist $artist
  * @throws \Exception
  */
 public function saveArtist(Artist $artist)
 {
     $data = array('full_name' => $artist->getFullName(), 'year_found' => $artist->getYearFound(), 'origin' => $artist->getOrigin(), 'createdby' => $this->id);
     $artist_id = (int) $artist->getArtistId();
     if ($artist_id == 0) {
         $this->tableGateway->insert($data);
     } else {
         if ($this->getArtist($artist_id)) {
             //\Zend\Debug\Debug::dump($artist_id);
             $this->tableGateway->update($data, array('artist_id' => $artist_id));
         } else {
             throw new \Exception('Form id does not exist');
         }
     }
 }
 /**
  * This function returns a form for adding an artist
  * @return type
  */
 public function addAction()
 {
     //Array to populate all users in a select box
     $data2 = $this->getUserTable()->fetchAll();
     $jamesArray = array();
     foreach ($data2 as $p) {
         $jamesArray[$p->getUserId()] = $p->getUsername();
     }
     //Create an instance of the artist form
     $form = new ArtistForm();
     //Get the request
     $request = $this->getRequest();
     //Check if the request is posted
     if ($request->isPost()) {
         //Create an object of the artist
         $artist = new Artist();
         //Get the input filter and filter the form
         $form->setInputFilter($artist->getInputFilter());
         //Set the data and get the post
         $form->setData($request->getPost());
         //check if the form is valid
         if ($form->isValid()) {
             //Get the data using exchange array
             $artist->exchangeArray($form->getData());
             //Go to the artist table and save the data
             $this->getArtistTable()->saveArtist($artist);
             //add a flash message for success
             $this->flashMessenger()->addMessage(sprintf(" %s<center> Artist Created Successfully</center> %s", '<div class="alert alert-success">', '</div>'));
             //Redirect to the list of artist
             return $this->redirect()->toRoute('artist', array('controller' => 'artist', 'action' => 'index', 'msge' => $this->msge));
         }
     }
     //if the request was not posted display the add form
     return array('form' => $form);
 }