Yii2 grid sorting is not correct with ArrayDataProvider - yii2

I'm using array data provider for grid view widget since the data source is an API response. It's working fine, but when I try to sort the 'Name' column, it's sorting incorrectly with the lowercase first letters. Please check the table grid screenshot. The API response which I'm receiving is correct with the current sorting.
1. Table Grid
2. Model
$provider = new ArrayDataProvider([
'allModels' => #$response->data,
'pagination' => false,
'sort' => [
'attributes' => ['id', 'name', 'shortDescription'],
],
]);
3. View
<?= GridView::widget([
' dataProvider' => $dataProvider,
'columns' => [
'id',
'name',
'shortDescription'
],
'options' => ['class' => 'content'],
'tableOptions' => ['class' => 'table table-striped table-hover'],
'summary' => Yii::t('app', 'Showing').' {begin}-{end} of '.$total.' '.Yii::t('app', 'items')
]);
?>
Please guide me to fix this. Thanks!

Related

Yii2 update with multiple conditions

$this->localdb->createCommand()
->update(
$this->MYTable,
[
'name' => $el['new'],
'data' => $el['data'],
],
[
'userId', $this->user,
'product_id', $this->productId,
'name', $el['old'],
'created', $el['date'],
'category', $el['cat'],
]
);
I tried to use the following command to update a row using multiple where conditions, but it doesn't work for some reason. The documentation doesn't seem to cover this case and doesn't seem to be updated, because the update() method seems to match the updateAll method instead of the update method for some reason (maybe it was updated?). So I was wondering what was the correct way to do this.
You have a syntax error. Try following:
$this->localdb->createCommand()
->update(
$this->MYTable,
[
'name' => $el['new'],
'data' => $el['data'],
],
[
'userId' => $this->user,
'product_id' => $this->productId,
'name' => $el['old'],
'created' => $el['date'],
'category' => $el['cat'],
]
);
Try This updateAll query :
MYTable::updateAll([ // field to be updated in first array
'name' => $el['new'],
'data' => $el['data']
],
[ // conditions in second array
'userId' => $this->user,
'product_id' => $this->productId,
'name' => $el['old'],
'created' => $el['date'],
'category' => $el['cat']
]);

show Enable/disable value on grid list magento

In Magento 1.9, I have created Module and in admin grid i want to show value 0/1 as Enable/Disable title. But it is not working. Here is my script which i am using.
$this->addColumn('status',
array(
'header'=> Mage::helper('catalog')->__('Status'),
'align' =>'right',
'width' => '50px',
'index' => 'status',
'options' => array('1'=>'Enable','0'=>'Disable')
)
);
Please anybody can help me where i need to change in Grid.php file.
Thank you
$this->addColumn('is_active', [
'header'=> __('status'),
'align' => 'left',
'index' => 'is_active',
'params' => ['id'=>'getId'],
'type' => 'options',
'options' => array(
1 => 'enabled',
0 => 'disabled',
),
]);

Passing multiple parameters in a hyperlink in yii2 with clean urls, Html::a() doesnt generate clean url

I am trying to generate a hyper link by the method mentioned in
http://www.yiiframework.com/doc-2.0/guide-helper-html.html#hyperlinks like this
Html::a('<b>Register</b>',
['story/create', array('id' =>39,'usr'=>'11')],
['class' => 'profile-link'])
I want to get url like story/create/id/39/usr/11
But it is generating as
story/create?1%5Bid%5D=39&1%5Busr%5D=1
I have enabled the clean url functionality of yii2 like
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Disable r= routes
'enablePrettyUrl' => true,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
], also.
How this can be achieved?
With generate url use like that (see more http://www.yiiframework.com/doc-2.0/guide-helper-url.html):
Html::a('<b>Register</b>',
['story/create', 'id' =>39,'usr'=>'11'],
['class' => 'profile-link'])
In urlManager input new rule:
rules' => array(
....
'story/create/<id:\d+>/<usr:\d+>' => 'story/create',
),
Output url will be like that:
story/create/39/11
And in controller:
public function actionCreate($id, $usr)
And Yii2 provide this parameter.
create Url Dynamically
Html::a('<b>Register</b>',
['story/create', 'id' =>39,'usr'=>'11'],
['class' => 'profile-link'])
In urlManager config rules :
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>/<usr:\d+>' => '<controller>/<action>',
],
],
Output url will be like that:
story/create/39/11
Another useful method :
Write in urlManager rules in your
'rules'=>array('/controller/action/<limit>/<offset>'=>'/controller/action/'),
Can be accessed in url controller/action/100/20

How to cache database translations on yii2

How to cache database translations on yii2
I tried the following but not worked
'i18n' => [
'class' => Zelenin\yii\modules\I18n\components\I18N::className(),
'languages' => ['en', 'ar', 'fr'],
'sourceMessageTable' => 'source_message',
'messageTable' => 'message',
'cache' => 'cache'
],
The problem is in Zelenin i18n module. If you look at Module.php file, you can see:
$this->translations['*'] = [
'class' => DbMessageSource::className(),
'sourceMessageTable' => $this->sourceMessageTable,
'messageTable' => $this->messageTable,
'on missingTranslation' => $this->missingTranslationHandler
];
in init() method. This Code set DbMessageSource options and there are no any options about caching. Module have no any caching options too.
If you change this code to:
$this->translations['*'] = [
'class' => DbMessageSource::className(),
'sourceMessageTable' => $this->sourceMessageTable,
'messageTable' => $this->messageTable,
'enableCaching' => true,
'cachingDuration' => 3600,
'on missingTranslation' => $this->missingTranslationHandler
];
Cache will work. Some SELECT messages will gone from debug list.
The Yii documentation for i18n db messages says that the property cache only has meaning when the property cacheDuration is not zero. I suggest you set this value, so;
'i18n' => [
'class' => Zelenin\yii\modules\I18n\components\I18N::className(),
'languages' => ['en', 'ar', 'fr'],
'sourceMessageTable' => 'source_message',
'messageTable' => 'message',
'cache' => 'cache',
'cacheDuration' => 3600
],

Zend Framework 2 - Multiple sub domains cause problems

I am writing an application in Zend Framework 2 which is going to run from a few different subdomain, and I want to have a different module for each sub domain, to keep things tidy.
My problem is when I add more than 1 sub domain to the routing, it loses one of the sub domains.
eg: This setup works
testbed.localhost (module/Application)
a.testbed.localhost (module/A)
If I add an extra one it will the route all requests for a to the Application Index Controller
eg
testbed.localhost (module/Application), a.testbed.localhost (module/A), b.testbed.localhost (module/B)
This is the module.config.php for module/A
'router' => array(
'routes' => array(
'ads' => array(
'type' => 'Hostname',
'options' => array(
'route' => 'a.testbed.localhost',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'__NAMESPACE__' => 'A\Controller',
'controller' => 'A\Controller\A',
'action' => 'index',
),
),
And this is the route in module.config.php in module/B
'router' => array(
'routes' => array(
'ads' => array(
'type' => 'Hostname',
'options' => array(
'route' => 'b.testbed.localhost',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'__NAMESPACE__' => 'B\Controller',
'controller' => 'B\Controller\B',
'action' => 'index',
),
),
Now the namespaces are correct in both the module.config.php files, what I have noticed is the sub domain a.testbed.localhost will work if I remove the reference to it from config/application.config.php
<?php
return array(
'modules' => array(
'Application',
'A',
'B', <--- A doesn't work if B is here
),
And if I swap A & B around in the modules array above, then B will get forwarded to the Application Module and A will work. So it seems to have problems with more than 1 sub domain. Has anyone got any ideas / come across the same thing?
This happens because your route names are the same. I would try a-ads and b-ads for route names and that should resolve your situation.
In the end the configuration is getting merged together. So it's like an array, when the last array is merged it overwrites anything before it.