Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [CakePHP] Duplikowanie rekordu
Forum PHP.pl > Forum > PHP > Frameworki
Bajki
Witam!
Chcąc dodać do swojego skryptu możliwość tagowania (akurat w moim skrypcie odpowiednikiem tagów i postów są zespoły (tagi) przypisywane do wydarzeń (postów) ) skorzystałem z tego artykułu :

http://www.jamesfairhurst.co.uk/posts/view...lication_part_5

I aktualnie tak wyglądają metody :

metoda odp. za edycje wydarzenia
  1. function edit($id = null)
  2. {
  3. $this->Event->id = $id;
  4. if (empty($this->data)) {
  5. $this->data = $this->Event->read();
  6. } else {
  7.  
  8. if($this->FileHandler->upload('Event', 'poster', md5($id) , 'posters/'))
  9. {
  10. $imageData = $this->FileHandler->getLastUploadInfo();
  11. $this->params['data']['Event']['poster_extension'] = $imageData['extension'];
  12. }
  13.  
  14. $this->data['Band'] = $this->_parse_genres($this->data['Event']['bands']);
  15.  
  16. if ($this->Event->save($this->data)) {
  17. $this->Session->setFlash('Wydarzenie zostało zaktualizowane.', 'default', array('class' => 'success'));
  18. $this->redirect(array('action' => 'view', $id));
  19. }
  20. }
  21. }


Funkcja _parse_genres()
  1. function _parse_genres($bands = null)
  2. {
  3. // variable to save genre data array
  4. $data = array();
  5.  
  6. // explode the genres sepearated by a comma
  7. $explode = explode(';', $bands);
  8.  
  9. // if the explode array is not empty
  10. if(!empty($explode)) {
  11. // loop through exploded genres
  12. foreach($explode as $band) {
  13. // remove leading/trailing spaces
  14. $band = trim($band);
  15.  
  16. // if the genre is not empty
  17. if(!empty($band)) {
  18. // find the genre in the db
  19. $db_band = $this->Event->Band->find('first', array(
  20. 'conditions' => array(
  21. 'Band.name' => $band,
  22. )
  23. ));
  24.  
  25. // if a genre was found
  26. if(!empty($db_band)) {
  27. // save the genre id
  28. $data[] = $db_band['Band']['id'];
  29. } else {
  30. // create a new genre
  31. $save = array('Band'=>array(
  32. 'name' => $band,
  33. ));
  34.  
  35. // create model
  36. // has to be done when adding multiple items in a row
  37. $this->Event->Band->create();
  38.  
  39. // save the new genre
  40. $saveOK = $this->Event->Band->save($save);
  41.  
  42. // if save was successful
  43. if($saveOK) {
  44. // last insert id
  45. $data[] = $this->Event->Band->getLastInsertID();
  46. }
  47. }
  48. }
  49. }
  50. }
  51.  
  52. return array('Band' => $data);
  53. }


Wszystko działa jak trzeba jednak pojawiła się pewna komplikacja - kiedy zapisuje zmiany ( edit() ), edytowane wydarzenie zostaje zapisane jednak w bazie pojawia się nowe wydarzenie będące kopią tego edytowanego (tylko id jest inne). Czy ktoś ma może pomysł co jest nie tak z tymi metodami ? Niestety mnie nie udało się znaleźć niczego, co wyglądało by na błąd

Pozdrawiam i z góry dzięki!
fleshgrinder
czy w formularzu edycyjnym masz ukryte pole Event.id?
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.