Use of condition in query builder in case of multiple association in cakephp 3.0? - mysql

I am facing trouble in multilevel association using query builder when i used conditions inside multiple contain in cakephp 3.0. Lets suppose i have to get a data of single store so i am trying to add the storeId in condition but its not working else rest of data is fetching correct from all store below are the query builder that i am using:-
// generate query
$bestSellingReportData = $this->ArticleMaster->find('all')->contain([
'Category' => [
'fields' => ['Cat_Id', 'Cat_Code']
],
'size_category' => [
'fields' => ['sizeCat_Id', 'sizeCat_Code']
],
'ItemMaster' => [
'Invoicedetaile' => [
'Invoice' => [
'Store' => [
'fields' => ['Store_Id', 'Store_Code']
],
'conditions' => ['Invoice.StoreId ='.$this->request->data['storeId']],
],
],
],
]);
$bestSellingReportData->select(['totalSoldItems' => $bestSellingReportData->func()->sum('Invoicedetaile.Qty')])
->matching('ItemMaster.Invoicedetaile', function ($q) {
return $q->where([
'AND' => [
'Invoicedetaile.ItemId = ItemMaster.Item_ID',
]
]);
})
->group(['ArticleMaster.Article_Code'])
->order(['totalSoldItems' => 'DESC'])
->autoFields(true);
I try to add the condition in both way using add condition and in where clause. But data is not filtering based on conditions i.e storeId

Work for me after doing R&D of many hours. Below is the query builder that work for me.
$bestSellingReportData = $this->ArticleMaster->find('all')->contain([
'Category' => [
'fields' => ['Cat_Id', 'Cat_Code']
],
'size_category' => [
'fields' => ['sizeCat_Id', 'sizeCat_Code']
],
'ItemMaster' => [
'Invoicedetaile' => [
'Invoice',
],
],
]);
$storeId = $this->request->data['storeId'];
$bestSellingReportData->select(['totalSoldItems' => $bestSellingReportData->func()->sum('Invoicedetaile.Qty')])
->matching('ItemMaster.Invoicedetaile.Invoice', function ($q) use ($storeId) {
return $q->where([
'AND' => [
'Invoicedetaile.ItemId = ItemMaster.Item_ID',
'Invoice.StoreId ='.$storeId,
]
]);
})
->group(['ArticleMaster.Article_Code'])
->order(['totalSoldItems' => 'DESC'])
->autoFields(true);

Related

MediaWiki, how to add filters to the SpecialPages:Categories page?

To better understand the code of MediaWiki, I'm trying to modify directly the source code without creating an extension.
In the page Special:Categories I'd like to show only the categories which are not created by a Template. How can I modify the query?
https://doc.wikimedia.org/mediawiki-core/1.28.0/php/classSpecialCategories.html
https://doc.wikimedia.org/mediawiki-core/1.28.0/php/classCategoryPager.html
I've found the solution
The source code for the Special Pages is inside the folder includes/specials. There are many scripts here about categories and by comparing some of those files I could find out the syntax used to manipulate queries in mediawiki which is defined through the method getQueryInfo() inside each file.
I'm posting the queries defined each in a different Special Page related to categories
public function getQueryInfo() {
return [
'tables' => [ 'category' ],
'fields' => [ 'title' => 'cat_title',
'namespace' => NS_CATEGORY,
'value' => 'cat_pages' ],
'conds' => [ 'cat_pages > 0' ],
];
}
public function getQueryInfo() {
return [
'tables' => [ 'categorylinks', 'page' ],
'fields' => [
'namespace' => 'page_namespace',
'title' => 'page_title',
'value' => 'COUNT(*)'
],
'conds' => [
'page_namespace' => $this->namespaceInfo->getContentNamespaces()
],
'options' => [
'HAVING' => 'COUNT(*) > 1',
'GROUP BY' => [ 'page_namespace', 'page_title' ]
],
'join_conds' => [
'page' => [
'LEFT JOIN',
'page_id = cl_from'
]
]
];
}

What's the difference between ActiveDataProvider and SQLDataProvider?

I'm a little bit confused. I'm wondering what is the difference between Yii2 ActiveDataProvider and SQLDataProvider? I was looking in the documentation for it, but couldn't get it too.
Could someone explain me when should I use one or another? And what is the difference between it?
Thank you for your time.
SqlDataProvider works with a raw SQL statement which is used to fetch the needed data and ActiveDataProvider can take either a yii\db\Query or yii\db\ActiveQuery object.
SqlDataProvider example:
$provider = new SqlDataProvider([
'sql' => 'SELECT * FROM post WHERE status=:status', //HERE
'params' => [':status' => 1],
'totalCount' => $count,
'pagination' => [
'pageSize' => 10,
],
'sort' => [
'attributes' => [
'title',
'view_count',
'created_at',
],
],
]);
ActiveDataProvider example:
$query = Post::find()->where(['status' => 1]); // ActiveQuery here
$provider = new ActiveDataProvider([
'query' => $query, // and here
'pagination' => [
'pageSize' => 10,
],
'sort' => [
'defaultOrder' => [
'created_at' => SORT_DESC,
'title' => SORT_ASC,
]
],
]);

Yii2 error strtoupper() expects parameter 1 to be string, array given

I made a query like this:
return Orders::find()->where([
'customer_id' => $CustomerID,
['in', 'order_status', $statuses]
])->all();
It returns the following error:
"message": "strtoupper() expects parameter 1 to be string, array given",
This return a collection of Orders Rows
return Orders::find()
->where(['customer_id' => $CustomerID,['in','order_status',$statuses]])->all();
The strtouppoer work on a string so If you want a column value you should get a rows and a column eg: try using
$myOrders = Orders::find()
->where(['customer_id' => $CustomerID,['in','order_status',$statuses]])->all();
return $myOrder[0]->my_column_name;
I had code like this so i got error, in Yii2, mongodb
"strtoupper() expects parameter 1 to be string"
[
'$match' => [
[
'$or' => [
['patient.appointment_info.lab.lab_number' => $lab_number],
['test.name' => ['$regex' => ".*$searchText.*"]],
['patient.name' => ['$regex' => ".*$searchText.*"]],
['user.full_name' => ['$regex' => ".*$searchText.*"]],
]
]
]
],
it is fixed like
[
'$match' => [
'$or' => [
['patient.appointment_info.lab.lab_number' => $lab_number],
['test.name' => ['$regex' => ".*$searchText.*"]],
['patient.name' => ['$regex' => ".*$searchText.*"]],
['user.full_name' => ['$regex' => ".*$searchText.*"]],
]
]
],
I was using one extra curly brace.

ZF3 zend-mvc generic route for many controllers in one module not working

I'm in ZF3, using the zend-mvc-skeleton and trying to configure a generic route that will match as many URLs as possible as I want to be able to create new controllers (including action methods of course), and have them immediately available.
The common approach described in the documentation is to write a route that matches the controller and action (same with ZF2).
Here is my module.config.php
namespace Application;
use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;
return [
'router' => [
'routes' => [
'home' => [
'type' => Literal::class,
'options' => [
'route' => '/',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
],
'default' => [
'type' => Segment::class,
'options' => [
'route' => '/application[/:controller[/:action]]',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
'constraints' => [
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
],
],
],
],
],
'controllers' => [/* ... */],
'view_manager' => [/* ... */],
],
It works like a charm for http://localhost/ and http://localhost/application calling the indexAction() function of the IndexController class inside the /module/Application/src/IndexController.php file.
However, it's not working when I try to get the fooAction() function in the same Controller (i.e. IndexController). It's not resolving correctly http://localhost/application/foo. and I get the following error:
A 404 error occurred
Page not found.
The requested controller could not be mapped to an existing controller class.
Controller:
foo (resolves to invalid controller class or alias: foo)
No Exception available
Same error if I try http://localhost/bar/foo to get the fooAction() in the barController.
Do you have any idea of what's wrong with this? Any help will be appreciated. Many thanks.
The route http://localhost/application/foo won't resolve to fooAction() in the index controller, since /foo in the URL will match the controller not the action. With that route setup you would need to visit http://localhost/application/index/foo.
To get it working you'll also need to make sure you have aliased your controller in the config, e.g. assuming you have:
'controllers' => [
'invokables' => [
'Application\Controller\Index' => \Application\Controller\IndexController::class
]
],
Then alias the controller so it matches the route parameter:
'controllers' => [
'invokables' => [
'Application\Controller\Index' => \Application\Controller\IndexController::class
],
'aliases' => [
'index' => 'Application\Controller\Index'
]
],
You'll need to add aliases that match the route parameter for each controller that isn't registered using the string you want for the route, e.g. a controller Namespace\Controller\BarController should be aliased to bar, etc.
I came here with similar problem. I have created two controllers in
"Application" module, and two in new module "Account" with the same name.
Application/Controller/IndexController
Application/Controller/OverviewController
Account/Controller/IndexController
Account/Controller/OverviewController
here are my modules.config.php
module/Account/config/module.config.php
return [
'router' => [
'routes' => [
'Account-account' => [
'type' => Segment::class,
'options' => [
'route' => '/account[/][:controller[/][:action][/]]',
'defaults' => [
'__NAMESPACE__' => 'Account\Controller',
'controller' => Account\Controller\IndexController::class,
'action' => 'index',
'locale' => 'en_us'
],
],
'may_terminate' => true,
'child_routes' => [
'wildcard' => [
'type' => 'Wildcard'
],
],
],
],
],
'controllers' => [
'factories' => [
Controller\IndexController::class => AccountControllerFactory::class,
Controller\OverviewController::class => AccountControllerFactory::class,
],
'aliases' => [
'index' => IndexController::class,
'overview' => OverviewController::class
]
],
and my
module/Application/config/module.config.php
return [
'router' => [
'routes' => [
'home' => [
'type' => Literal::class,
'options' => [
'route' => '/',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
],
'Application-application' => [
'type' => Segment::class,
'options' => [
'route' => '/application[/][:controller[/][:action][/]]',
'defaults' => [
'__NAMESPACE__' => 'Application\Controller',
'controller' => Application\Controller\IndexController::class,
'action' => 'index',
'locale' => 'en_US'
],
],
'may_terminate' => true,
'child_routes' => [
'wildcard' => [
'type' => 'Wildcard'
],
],
],
],
],
'controllers' => [
'factories' => [
Controller\IndexController::class => IndexControllerFactory::class,
Controller\OverviewController::class => IndexControllerFactory::class,
],
'aliases' => [
'index' => IndexController::class,
'overview' => OverviewController::class,
]
],
With this configuration if aliases sections are commented there is a error message which says that there is invalid controller or alias (index/overview).
If there are aliases
route: "application/overview/index" goes into Account module.

Yii2 Apply a function to all requests

Is there any way to apply a function to all requests and queries in Yii2?
I want to replace specific characters for all of them.
I am using Yii2 advanced app
Thanks.
This is the config file:
$config = [
'language' => 'en',
'components' => [
'request' => [
'cookieValidationKey' => 'something',
],
'authManager' => [
'class' => 'yii\rbac\DbManager',
'defaultRoles' => ['guest'],
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
],
],
];
return $config;
Without extending custom code on each request can be exexuted like so (add this to your application config):
return [
'on beforeRequest' => function () {
if (!Yii::$app->get('user', false)) {
return;
}
$user = User::getCurrent();
if ($user) {
Yii::$app->setTimeZone($user->time_zone);
}
},
'on afterRequest' => function () {
...
},
];
Depending on when you need to execute code (before or after the request) use 'on beforeRequest' or 'on afterRequest' accordingly.
yii2 have a request component. You can extend yii\web\request and define your custom implementation.
[
...
'components' =>
'request' => [
'class' => '\common\MyRequest',
'addGeoLocationForExample' => true,
]
...