0 レビュー
1 回答
php-ユーザーがCakephpのURLIDを変更できないようにする
一部のユーザーがURLのIDを変更したり、別の本を編集したりすることを避けたいと思います。 例:これは元のURLです:
https://www.myurl/books/edit/1
ユーザーは番号1を変更できます:
https://www.myurl/books/edit/41
ユーザーが自分の国の本をのみ編集できるようにしたい
これは私のBooksControllerからのオリジナルの編集です
public function edit($id = null)
{
$country_id= $this->Auth->User()['country_id'];
$book= $this->Books->get($id, [
'contain' => []
]);
if ($this->request->is(['patch', 'book', 'put'])) {
$book= $this->Books->patchEntity($book, $this->request->data);
if ($this->Books->save($book)) {
$this->Flash->success(__('Success.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('Error'));
}
}
$this->set('_serialize', ['book']);
}
コードのこの部分を変更しようとしました:
$country_id= $this->Auth->User()['country_id'];
$book= $this->Books->get($id, [
'contain' => []
]);
そのために:
$country_id= $this -> Auth -> User()['country_id'];
$book = $this->Books->get($id, [
'contain' => ['City'],
'conditions' => ['City.country_id' => $country_id]
]);
つまり、同じ国の本を表示できるのはユーザーだけです。 しかし、エラーがあります:「テーブル「本」にレコードが見つかりません」
元の編集機能を入れれば完璧に動作しますが、ユーザーはIDを変更できます。 上記の変更を行うと、ユーザーはブックIDを編集できなくなります
わからない
0
レビュー
答え :
解決策:
get操作で結果が見つからない場合a Cake \ Datasource \ Exception\RecordNotFoundExceptionが発生します。君 この例外を自分でキャッチするか、CakePHPに変換を許可することができます 404エラーになります。
ここでDocumentationを確認
または、uouは
の結果を確認できます。find()
メソッドでfirst()
$ book = $ this-> Books-> find([ 'contain' => ['City']、 '条件'=>['City.country_id' => $ country_id、'Books.id' => $ id] ])->{-コード-2};
http://book.cakephp.org/3.0/en/orm/retrieveing-data-and-resultsets.html#getting-the-first-result
わからない
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。