cakephp 3 controller action -> how to make it smart -


i implementet controller action in maybe unelegant way. how made better? table classes after bin/cake bake. think part entity created simplyfied much.

what i'm doing: books --belongsto--> publishers <--habtm--> publishernumbers when adding book database, publishernumber extracted isbn number. number linked publisher in habtm relation. need suggest user publishers when typing isbn in form.

the code works now, in year, god know did here. first part straightforward.

public function add() {     $book = $this->books->newentity();     $associations = ['associated' =>             [                    'tags',                 'publishers',                 'publishers.publishernumbers'             ]         ];     if ($this->request->is('post')) {         $data= $this->request->data;           $publisher = $this->books->publishers->get(             $this->request->data['publisher_id'],                  ['contain' => ['publishernumbers']]         );         unset($data['publisher_id']);          $book->publisher = $publisher;      //extract group- , publishernumber isbn     $this->loadcomponent('isbn.isbn');     $split = $this->isbn->splitisbn($this->request->data['isbn']);     $publishernumber = $split[1].$split[2]; 

this part mess begins. think done way more elegant.

    //check if publisher contains $publishernumber     //and if not, add entity     $new = true;     foreach ($book->publisher->publishernumbers $n){         if ($n->number == $publishernumber){             $new = false;         }     }     if ($new){         $existingnumber = $this->books->publishers->publishernumbers             ->findbynumber($publishernumber)             ->first();         if (!$existingnumber){              //publishernumber not exist in database             $pubnumber = $this->books->publishers->publishernumbers                 ->newentity();             $pubnumber = $this->books->publishers->publishernumbers                 ->patchentity($pubnumber, ['number' => $publishernumber]);             $book->publisher->publishernumbers[] = $pubnumber;          } else {              //publishernumber exists in database              //but not associated publisher             $book->publisher->publishernumbers[] = $existingnumber;         }         $book->publisher->dirty('publishernumbers', true);     }       $book = $this->books->patchentity($book, $data, $associations); 

saving

    if ($this->books->save($book, $associations)){         cache::delete('exlibrisbooksindex');         $this->flash->success(__('the book has been saved.'));         return $this->redirect(['action' => 'index']);     } else {         $this->flash->error(__('error.'));     } } $publishers = $this->books->publishers->find('list')     ->order('name')     ->toarray(); $this->set(compact('book', 'publishers')); $this->set('_serialize', ['book']); } 


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -