CRUD: Adding WHERE condition in the index action - cakephp

I was studying the CRUD documentation but somehow I miss the point. I want to implement in the index() action different WHERE conditions depending on some parameters I obtain from the URL.
Reading https://crud.readthedocs.io/en/latest/events.html doesn't really help me. I tried this very simple code in the index() function:
public function index()
{
$this->Crud->on('beforeFilter', function(\Cake\Event\EventInterface $event) {
$event->getSubject()->query->where([ "partner_type" => "AGENT" ]);
});
return $this->Crud->execute();
}
This means, each call of the index action should add the WHERE condition partner_type = 'AGENT' and return only those rows but all rows are returned.
What do I misunderstand?

The correct event is beforePaginate - see the answer at Github:
public function index()
{
$this->Crud->on('beforePaginate', function(\Cake\Event\EventInterface $event) {
$event->getSubject()->query->where([ "partner_type" => "AGENT" ]);
});
return $this->Crud->execute();
}

Related

Auth.afterIdentify is not firing

I need to change something in the user session after it was started. This is temporary, so using an event such as Auth.afterIdentify is what I'm looking for.
What I tried
I have largely referred to this answer on how to even approach this.
I have tried using an anonymous function callback as well as a controller callback method
Following the Auth.afterIdentify documentation I have added implementedEvents
Made sure implementedEvents has a + parent::implementedEvents(), otherwise the controller kept showing me the home page
What I have
Here's my current src/Controller/AppController.php:
<?php
namespace App\Controller;
use Cake\Controller\Controller;
use Cake\Event\Event;
class AppController extends Controller implements \Cake\Event\EventListenerInterface
{
public function initialize()
{
parent::initialize();
// …
$this->loadComponent('Authentication.Authentication');
// Trying with an anonymous function
\Cake\Event\EventManager::instance()->on('Auth.afterIdentify', function ($event) {
Log::write( // noticed when posting this question, should have thrown an error
'info',
'Testing: ' . $event->getSubject()->id
);
debug($event);exit;
});
// Trying a controller callback
\Cake\Event\EventManager::instance()->on('Auth.afterIdentify', [$this, 'afterIdentify']);
}
public function beforeFilter(\Cake\Event\Event $event)
{
parent::beforeFilter($event);
$this->set('myAuth', $this->Authentication->getResult());
$this->set('myUser', $this->Authentication->getIdentity());
}
public function afterIdentify(CakeEvent $cakeEvent, $data, $auth) {
debug([
'$cakeEvent' => $cakeEvent,
'$data' => $data,
'$auth' => $auth,
]);exit;
}
public function implementedEvents()
{
return [
'Auth.afterIdentify' => 'afterIdentify',
] + parent::implementedEvents();
}
}
What doesn't work
It seems neither of the above event listeners is being called. No CakePHP logs are being updated (not even with errors), although they normally work.
What I expected to happen
Calling Log::write without declaring where it comes from should have thrown (and logged) an error
The debug() information was not displayed
Removing the public function afterIdentify method should have caused an error; it didn't – meaning the controller isn't even looking for it
You are mixing up the old auth component and the new authentication plugin, the Auth.afterIdentify event belongs to the former.
The authentication plugin's authentication component has a Authentication.afterIdentify event, but this only applies to authenticators that are stateful and do not implement automatic persisting. So out of the box this only applies to the Form authenticator, and the event is being triggered once on the request where the user was authenticated via the form, on subsequent requests where they are authenticated via for example the Session authenticator, the event is not being triggered.
public function initialize()
{
parent::initialize();
// ...
$this->loadComponent('Authentication.Authentication');
$this->Authentication->getEventManager()->on(
'Authentication.afterIdentify',
function (
\Cake\Event\EventInterface $event,
\Authentication\Authenticator\AuthenticatorInterface $provider,
\Authentication\IdentityInterface $identity,
\Authentication\AuthenticationServiceInterface $service
) {
// ...
$identity['foo'] = 'bar';
$this->Authentication->setIdentity($identity);
}
);
}

Cakephp 2.3 beforeFilter and implementedEvents aren't able to co-exist

Using cakephp 2.3.5.
I use beforeFilter in several controllers to allow certain actions without the need to log in. I've used it successfully for quite some time. However, I've noticed that I can't get beforeFilter to fire if the controller also has the implementedEvents() method.
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('matchWwiProducts');
}
public function implementedEvents() {
return array(
'Controller.Product.delete' => 'deleteSku',
'Controller.Product.price' => 'notifySubscribers',
'Controller.Product.stock' => 'notifySubscribers'
);
}
For the code as displayed above I will be forced to do a login if I call the method www.example.com/products/matchWwiProducts.
When I comment out the implementedEvents() everything works as intended. I've searched around and can't find any references to implementedEvents() creating issues with beforeFilter.
The action matchWwiProducts() is as follows. It works perfectly when I log in. However, I don't want to force a log in for this action to take place.
public function matchWwiProducts() {
// this is an audit function that matches Products sourced by wwi
//
$this->autoRender = false; // no view to be rendered
// retrieve products sourced from wwi from Table::Product
$this->Product->contain();
$wwiProducts = $this->Product->getWwiSkus();
$wwiProductCount = count($wwiProducts);
// retrieve products sourced from wwi from Table:Wwiproduct
$this->loadModel('WwiProduct');
$this->Wwiproduct->contain();
$wwiSource = $this->Wwiproduct->getSkuList();
$wwiSourceCount = count($wwiSource);
// identify SKUs in $wwiProducts that are not in $wwiSource
$invalidSkus = array_diff($wwiProducts, $wwiSource);
// identify SKUs in $wwiSource that are not in $wwiProducts
$missingSkus = array_diff($wwiSource, $wwiProducts);
$missingSkuDetails = array();
foreach ($missingSkus as $missingSku) {
$skuStockStatus = $this->Wwiproduct->getStockStatus($missingSku);
$missingSkuDetails[$missingSku] = $skuStockStatus;
}
$email = new CakeEmail();
$email->config('sourcewwi');
$email->template('sourcewwiaudit', 'sourcewwi');
if (count($invalidSkus) > 0 || count($missingSkus) > 0) {
$email->subject('WWI Source Audit: Invalid or Missing SKUs');
$email->viewVars(array('invalidSkus' => $invalidSkus,
'missingSkuDetails' => $missingSkuDetails,
'wwiProductCount' => $wwiProductCount,
'wwiSourceCount' => $wwiSourceCount));
} else {
$email->subject('WWI Source Audit: No Exceptions');
$email->viewVars(array('wwiProductCount' => $wwiProductCount,
'wwiSourceCount' => $wwiSourceCount));
}
$email->send();
}
It doesn't fire because you're overloading the implementendEvents() method without making sure you keep the existing events there.
public function implementedEvents() {
return array_merge(parent::implementedEvents(), array(
'Controller.Product.delete' => 'deleteSku',
'Controller.Product.price' => 'notifySubscribers',
'Controller.Product.stock' => 'notifySubscribers'
));
}
Overloading in php.
Check most of the base classes, Controller, Table, Behavior, Component, they all fire or listen to events. So be careful when extending certain methods there. Most simple way might to do a search for "EventListenerInterface" in all classes. A class that implements this interface is likely to implement event callbacks.

Cakephp validation bug in controller?

I am trying to invalidate a field by a condition in controller instead of Model.
$this->Model->invalidate('check_out_reason', __('Please specify check out reason.', true));
The above won't work to invalidate the field. Instead, I need the below:
$this->Model->invalidate('Model.check_out_reason', __('Please specify check out reason.', true));
However, if I wish get the error message show up in the "field" itself ($this->model->validationErrors), it needs to be "check_out_reason" instead of "Model.check_out_reason". That means, I can't get the error message to show up in the field itself if I wish to invalidate the input in controller.
May I know is this a bug in CakePHP?
i created a test controller called "Invoices", just for testing, and i developed the following function
public function index(){
if (!empty($this->request->data)) {
$this->Invoice->invalidate('nombre', __('Please specify check out reason.'));
if ($this->Invoice->validates()) {
// it validated logic
if($this->Invoice->save($this->request->data)){
# everthing ok
} else {
# not saved
}
} else {
// didn't validate logic
$errors = $this->Invoice->validationErrors;
}
}
}
i think it worked for me
Change the field "nombre" for your field called "check_out_reason" to adapt the function to your code
I found a workaround for manual invalidates from controller. Reading a lot on this issue I found out that the save() function doesn't take in consideration the invalidations set through invalidate() function called in controller, but (this is very important) if it is called directly from the model function beforeValidate() it's working perfectly.
So I recommend to go in AppModel.php file and create next public methods:
public $invalidatesFromController = array();
public function beforeValidate($options = array()) {
foreach($this->invalidatesFromController as $item){
$this->invalidate($item['fieldName'], $item['errorMessage'], true);
}
return parent::beforeValidate($options);
}
public function invalidateField($fieldName, $errorMessage){
$this->invalidatesFromController[] = array(
'fieldName' => $fieldName,
'errorMessage' => $errorMessage
);
}
After that, make sure that your model's beforeValidate() function calls the parent's one:
public function beforeValidate($options = array()) {
return parent::beforeValidate($options);
}
In your controller for invalidating a field use next line:
$this->MyModel->invalidateField('fieldName', "error message");
Hope it helps! For me it's working!

Running find('all') or find('first') on identical set of conditions in Cake 2

In a controller/index action in my CakePHP2 app, I have
$this->Model-find('all',array( ... lots conditions and things... ));
Then, in controller/view I have:
$this->Model-find('first',array( ... lots conditions and things... ));
Both of these work really well, but I'm obviously repeating a large chunk of code (namely, the find conditions, field list etc) with the only two differences being that in the first one I'm doing find('all') and in the second one I'm doing find('first') and providing the id as one of the conditions.
Is there a way that I can move the conditions into the model and then run different types of find on those conditions?
You can create a method in Youur Model:
public function getConditions($id = null) {
return array( ... lots conditions and things... );
}
somewhere in this method just test if (!empty($id)) to return correct result. (You haven't provide the 'conditions and things' so I can't write the exact code here...
and use it in both controler actions:
$this->Model->find('first', $this->Model->getConditions()));
$this->Model->find('all', $this->Model->getConditions()));
Yes you can move your query into model class as a function/method. see sample bellow for post model:
class Post extends AppModel {
public $name = 'Post';
public function getAll() {
return $this->find('all', array('limit' => 20, 'order' => 'Post.created DESC'));
}
}
and call it on your controller by $this->Model->getAll();
$this->Post->getAll();
You can also add parameter to your function.
Add the conditions as an attribute of your controller
class FoosController extends AppController{
public $conditions = array( ... lots conditions and things... )
public function index(){
$this-Foo->find('all', $this->conditions);
}
public function view(){
$this-Foo->find('first', $this->conditions);
}
}

CakePHP: How to list only the Posts which have been posted by the user himself/herself (one user)

On this page, a user would see a list of links which he himself/herself has posted, not including the rests which were posted by others. I'm in the middle of writing the function in one of my models, e.g. news
I am trying to use the idea of a dashboard: http://nuts-and-bolts-of-cakephp.com/2008/12/16/how-to-build-a-dashboard-for-your-application-in-cakephp/
I created my dashboards controller as:
function index () {
$this->set('news', ClassRegistry::init('News')->showmy());
}
// In my news::model I have a function called showmy()
function showmy() {
$userid = $this->Session->read('Auth.User.id');
$mynews = $this->News->find('all', array('conditions' => array('News.user_id' => '$userid')));
$this->set('mynews', $mynews);
}
//The error I get is as follow
Undefined property: News::$Session [APP\models\news.php, line 7]
Fatal error: Call to a member function read() on a non-object in C:\...\app\models\news.php on line 7
I know something is terribly wrong with the showmy function, can someone shed some light how do we write the function that only retrieves the posts by one user? or if the problems are minor, correct the function above?
Try this
You can't use Session from model so passit from controller
function index () {
$userid = $this->Session->read('Auth.User.id');
$this->set('news', ClassRegistry::init('News')->showmy($userid ));
}
function showmy($userid) {
return $this->find('all', array('conditions' => array('News.user_id' => $userid)));
}
My approach seems to be more documentation-like:
Let say you have PostsController with Post Model. When you do index on posts it's fine, you query for all posts. But for one user, I think you mean single index action in PostsController like:
class Post extends AppModel {
public $belongsTo = 'User'; // This way you're telling cakephp that one user can have many posts (he's posts' author)
}
class PostsController extends AppController {
public function viewByUser() {
$id = $this->Auth->user('id');
$posts = $this->Post->findAllById($id);
$this->set('posts', $posts);
}
}
And then, in your view, you build a table like for index action
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#magic-find-types

Resources