Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Condition in read() function CakePHP

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:

  1. Is there any way to tweak read function with condition more than $id ? Because it will just need to add a one line in all my controllers if it works ?
  2. Or I have to use long code of find() function to get is the only option ?

Any best solution will be appreciable.

like image 760
Rikesh Avatar asked Apr 16 '26 00:04

Rikesh


1 Answers

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.

like image 97
deizel Avatar answered Apr 20 '26 12:04

deizel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!