Reputation: 26431
In CakePHP function edit
I use read()
function as:
$this->data = $this->Article->read(null, $id);
It brings all fields of $id
. Now, what I am trying to tweak to give one more condition in read()
to get articles only if user logged in is related to it.
eg:
$this->Article->user_id = $user_id;
$this->Article->id = $id;
$this->Article->read();
And obviously it want work as read()
brings data only w.r.t. $id
(primary key).
My question:
$id
? Because it will just need to add a one line in all my controllers if it works ?Any best solution will be appreciable.
Upvotes: 3
Views: 5826
Reputation: 11212
If you really must do thise, you could use OOP techniques to override the way the core method works.
Just copy the Model::read()
method to your AppModel
class and make the changes necessary.
Upvotes: 3
Reputation: 33163
You have to use find()
if you want to make the search conditionally. Another option is to read the data only if the conditions hold:
$this->Article->id = $id;
if( $this->Article->field( 'user_id' ) == $user_id ) {
$this->Article->read();
}
(read()
populates $this->data
automatically.)
Upvotes: 0