2017-03-11 17 views
0

DBに新しい本を追加する際に、新しいエンティティで要求をパッチし、そのエンティティを保存するためのアクションを別にします。要求を新しいエンティティにパッチするアクションは、このエンティティを別のメソッドに返します。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 

そして、エンティティを関数パラメータとして渡しています。明らかにルーティングエラーが発生します。

ここでは、少なくともあなたがやっていることに対して、ここでは複数の機能を必要としません。

それを簡単に:

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

これはできません。実際には意味がないので、実際には意味がありません。パラメータをそのまま渡してはいけません。常にエラーを生成します。本当に必要なら、エンティティを1つ関数を呼び出すことによって別の関数から取得します。ただし、パラメータとしてエンティティを渡すことはありません! –

+0

本当に別の関数からEntityを取得したい場合は、更新された答えを見てください! –

関連する問題