2017-03-11 50 views
0

在向數據庫添加新書時,我希望單獨執行操作以在新實體中修補請求並保存該實體。將請求修補到新實體中的操作將此實體返回到另一個方法。無法將實體作爲參數傳遞給CakePHP 3.2中同一控制器中的另一個操作。

的代碼是這樣的:

public function bookAdder($book = []) 
    { 
     if ($this->Books->save($book)) { 
       $this->Flash->success(__('The book has been saved.')); 
       return $this->redirect(['action' => 'index']); 
      } else { 
       $this->Flash->error(__('The book could not be saved. Please, try again.')); 
       return $this->redirect(['action' => 'add']); 
      } 
    } 


    public function add() 
    { 
     $user_id = $this->request->session()->read('Auth.User.id'); 
     $book = $this->Books->newEntity(); 
     if ($this->request->is('post')) { 
      $book = $this->Books->patchEntity($book, $this->request->data); 
      $book->set(array('user_id' => "$user_id")); 
      return $this->redirect(['action' => 'bookAdder', $book]); 
     } 
     $users = $this->Books->Users->find('list', ['limit' => 200]); 
     $this->set(compact('book', 'users')); 
     $this->set('_serialize', ['book']); 
    } 

同時節省了測試記錄,我得到這個錯誤:

Error: A route matching "/books/book-adder/{ "title": "cc", "writer": "cc", "edition": "cc", "course": "cc", "description": "cc", "price": 2, "user_id": "1"}" could not be found.

Error screenshot Here

我也試過public function bookAdder($book){ } 但仍然沒有成功。

有什麼辦法可以解決這個問題嗎?提前致謝!

回答

1

多功能!

Are you doing some practice for making multiple functions ? That's great if you make your function reusable and more readable.

但你在這裏做什麼是增加了一些沒出息的複雜性。當你重定向到bookAdder函數時,你會生成如下這樣的url:

book-adder/%7B%0A%20%20%20%20"name"%3A%20"abc"%2C%0A%20%20%20%20"user_id"%3A%20"1"%0A%7D 

而你正在傳遞entity作爲函數參數。很明顯,發生路由錯誤。

所以你真的不需要多個功能,至少對於你正在做的事情來說。

使簡單:

public function add() 
{ 
    $user_id = $this->request->session()->read('Auth.User.id'); 
    $book = $this->Books->newEntity(); 
    if ($this->request->is('post')) { 
     $book = $this->Books->patchEntity($book, $this->request->data); 
     $book->set(array('user_id' => "$user_id")); 
     if ($this->Books->save($book)) { 
      $this->Flash->success(__('The book has been saved.')); 
      return $this->redirect(['action' => 'index']); 
     } else { 
      $this->Flash->error(__('The book could not be saved. Please, try again.')); 
      return $this->redirect(['action' => 'add']); 
     } 
    } 
    $users = $this->Books->Users->find('list', ['limit' => 200]); 
    $this->set(compact('book', 'users')); 
    $this->set('_serialize', ['book']); 
} 

如果你真的需要從另一個函數程序流程實體應該是這樣的:

makeEntity功能:

public function makeEntity($book = null,$requestData = []) { 
    $book = $this->Books->patchEntity($book, $requestData); 
    return $book; 
} 

添加功能:

public function add() 
{ 
    $user_id = $this->request->session()->read('Auth.User.id'); 
    $book = $this->Books->newEntity(); 
    if ($this->request->is('post')) { 
     $book = $this->makeEntity($book, $this->request->data); 
     $book->set(array('user_id' => "$user_id")); 
     return $this->redirect(['action' => 'bookAdder', $book]); 
    } 
    $users = $this->Books->Users->find('list', ['limit' => 200]); 
    $this->set(compact('book', 'users')); 
    $this->set('_serialize', ['book']); 
} 

而這並沒有太大的區別。

+0

感謝您的回答:)但有可能嗎?我希望能以某種方式獲得add()函數中的實體的引用,然後將它傳遞給bookAdder()函數。 –

+0

這樣做是不可能的,這是非常糟糕的做法,因爲這樣做真的沒有意義..並且永遠不會以這種方式傳遞參數..這將始終產生錯誤..如果您確實需要這樣做,您可以從一個實體返回實體函數並通過調用該函數從另一個函數中獲取該函數..但從未將實體作爲參數傳遞! –

+0

如果您確實希望從其他功能獲得實體,請參閱更新的答案! –

相關問題