how to use Yii::$app->formatter->asInteger() and Yii::$app->formatter->asDecimal() without thousands separator or with custom thousands separator (I don't want to make changes everywhere. Only in one particular place).
EDITE:
I found that asInteger function accepts two optional parameter ( yii2 API Documentation):
(asInteger( $value, $options = [], $textOptions = [] )). and I tried using theme this way:
Yii::$app->formatter->asInteger( 100000000,[NumberFormatter::GROUPING_SEPARATOR_SYMBOL=>'*',NumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL=>'*']);
but not working correctly. it will output *۱۰۰۰۰۰۰۰۰ as result insted ۱۰۰*۰۰۰*۰۰۰
You can do this as simple as add this chunk of code in to your config file:
'components' => [
'formatter' => [
'dateFormat' => 'dd.MM.yyyy',
'decimalSeparator' => ',',
'thousandSeparator' => ' ',
'currencyCode' => 'EUR',
],
],
And adjust them like you wish. Also you can look up for more configs at: https://www.yiiframework.com/doc/guide/2.0/en/output-formatting
Related
I'm trying to create a simple rule url, and I can't get it to work.
I want the following rule:
mysite.com/[username]
to go to
mysite.com/kit/page?id=[username]
Is this possible?. Right now I only have one rule, but I need to keep that one as well
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'' => 'site/index',
],
],
If I understand correctly you are saying that you have an existing page mysite.com/kit/page?id=[username] which should be shown if you type in the URL mysite.com/[username] in the address bar, if yes then you can update the urlManager like below
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'/' => 'site/index',
'<id:\w+>' => 'kit/page'
]
]
Ideally, I would use mysite.com/kit/[username] to avoid conflicts with any other controller which somehow matches up with any username and use 'kit/<id:\w+>' => 'kit/page'.
Note : \w+ matches any word character (equal to [a-zA-Z0-9_]), so if your username can have any other character allowed you might have to update the pattern, for example to allow - you should change the rule to '<id:[\w\-]+>' => 'kit/page'
I have the following configuration for the formatter component in my project:
'formatter' => [
'dateFormat' => 'dd.MM.yyyy',
'datetimeFormat' => 'dd.MM.yyyy HH:mm',
'decimalSeparator' => '.',
'thousandSeparator' => ' ',
'currencyCode' => 'EUR',
],
Now, for my datetimeFormat I'd like to suffix it with an h letter/string at the end of it.
I have tried with:
'datetimeFormat' => 'dd.MM.yyyy HH:mm\\h',
But the escaped \\h returns a letter g there. I know that \\h works with date.
How do we do escapes with Yii 2?
Okay, found it while reading formatter documentation and ICU manual.
To achieve this, escape the letter with wrapping your letter/string with single quotes:
'datetimeFormat' => "dd.MM.yyyy HH:mm'h'",
Sample output: 30.12.1990 12:25h.
I am querying a database table conveniently named order and because of that I had to set 'quoteIdentifiers' => true in my app.php configuration. However, when I'm putting function names into the fields configuration of my find() call, CakePHP quotes them too.
$orders->find('all', array(
'fields' => array(
'DATE(Orders.date_added)',
'COUNT(*)',
),
'conditions' => array(
'Orders.order_status_id <>' => 0,
),
'group' => array(
'DATE(Orders.date_added)',
),
));
The query above ends up calling
SELECT <...>, `DATE(Orders.date_added)` FROM `order` <...>
which obviously throws an error.
I googled a lot, tried this:
$orders = $orders->find('all', array(
'conditions' => array(
'Orders.order_status_id <>' => 0,
),
'group' => array(
'DATE(Orders.date_added)',
),
))->select(function($exp) {
return $exp->count('*');
});
and that didn't work either, throwing me some array_combine error.
Is there any way for me to un-quote those function names, while keeping the rest of the query quoted automatically? Here's what I'm trying to accomplish:
SELECT <...>, DATE(Orders.date_added) FROM `order` <...>
Please help.
You should use function expressions, they will not be quoted, except for arguments that are explicitly being defined as identifiers:
$query = $orders->find();
$query
->select([
'date' => $query->func()->DATE([
'Orders.date_added' => 'identifier'
]),
'count' => $query->func()->count('*')
])
->where([
'Orders.order_status_id <>' => 0
])
->group([
$query->func()->DATE([
'Orders.date_added' => 'identifier'
])
]);
I'd generally suggest that you use expressions instead of passing raw SQL snippets wherever possible, it makes generating SQL more flexible, and more cross-schema compatible.
See also
Cookbook > Database Access & ORM > Query Builder > Using SQL Functions
How to properly create a custom configuration key in zend expressive
I tried to create a file custom-config.php in the config/autoload directory but the key is not read by the container
my custom-config.php looks like this
<?php
[
'customkey' => [
'value1' => '1',
'value2' => '2',
],
];
I think you a missing a return statement.
Try with
<?php
return [
'customkey' => [
'value1' => '1',
'value2' => '2',
],
];
Besides missing return statement, as marcosh pointed out, I think additional problem is the filename itself.
It should be something like custom-config.local.php or custom-config.global.php.
Configuration files are loaded in a specific order. First global.php, then *.global.php, local.php and finally *.local.php. This way local settings overwrite global settings.
Settings shared between servers go into *.global.php, sensitive data and local settings in *.local.php. Local config files are ignored by git.
The default loading behavior is set in config/config.php if you want to change this.
Your custom config could look like this:
<?php // config/autoload/custom-config.global.php
return [
'dependencies' => [
'invokables' => [
// ...
],
'factories' => [
// ...
],
],
// Prefered format
'vendor' => [
'package' => [
'key' => 'value',
]
],
// Custom package
'custom_package' => [
'value1' => '1',
'value2' => '2',
],
];
I installed the above extension via composer and follow the documentation for every step; in my :
view : use kartik\datecontrol\DateControl;// <?=$form->field($model, 'dated')->widget(DateControl::classname(), [
'type'=>DateControl::FORMAT_DATE,
'ajaxConversion'=>false,
'options' => ['pluginOptions' => ['autoclose' => true ],'class'=>'col-xs-12 form-control input-sm']])?>
Web.php - Module configuration :
use \kartik\datecontrol\Module;
'datecontrol' => [
'class' => 'kartik\datecontrol\Module',
// format settings for displaying each date attribute (ICU format example)
'displaySettings' => [
Module::FORMAT_DATE => 'php:dd-m-Y',
Module::FORMAT_TIME => 'php:H:i:s',
Module::FORMAT_DATETIME => 'php:Y-m-d H:i:s',
],
// format settings for saving each date attribute (PHP format example)
'saveSettings' => [
Module::FORMAT_DATE => 'php:Y-m-d',
Module::FORMAT_TIME => 'php:H:i:s',
Module::FORMAT_DATETIME => 'php:Y-m-d H:i:s',
],
// set your display timezone
// 'displayTimezone' => 'Asia/Kolkata',
// set your timezone for date saved to db
// 'saveTimezone' => 'UTC',
// automatically use kartik\widgets for each of the above formats
'autoWidget' => true,
// default settings for each widget from kartik\widgets used when autoWidget is true
'autoWidgetSettings' => [
Module::FORMAT_DATE => ['type'=>2, 'pluginOptions'=>['autoclose'=>true]], // example
Module::FORMAT_DATETIME => [], // setup if needed
Module::FORMAT_TIME => [], // setup if needed
],
// custom widget settings that will be used to render the date input instead of kartik\widgets,
// this will be used when autoWidget is set to false at module or widget level.
'widgetSettings' => [
Module::FORMAT_DATE => [
'class' => 'yii\jui\DatePicker', // example
'options' => [
'dateFormat' => 'php:d-M-Y',
'options' => ['class'=>'form-control'],
]
]
]
// other settings
]
When I run the view I got the following error message :
{"name":"Invalid Configuration","message":"The class
'\kartik\date\DatePicker' was not found and is required for
DateControl 'date' format.\n\nPlease ensure you have installed one of
'yii2-widgets' OR 'yii2-widget-datepicker' extensions. To install, you
can run this console command from your application root:\n\nphp
composer.phar require kartik-v/yii2-widgets: \"#dev\"\n\n--- OR
---\n\nphp composer.phar require kartik-v/yii2-widget-datepicker: \"#dev\"","code":0,"type":"yii\base\InvalidConfigException","file":"C:\wamp\www\pub\vendor\kartik-v\yii2-krajee-base\Config.php","line":118,"stack-trace":["#0
C:\wamp\www\pub\vendor\kartik-v\yii2-krajee-base\Config.php(195):
kartik\base\Config::checkDependency('\\kartik\\date\\Da...',
Array, 'for DateControl...')","#1
C:\wamp\www\pub\vendor\kartik-v\yii2-datecontrol\DateControl.php(215):
kartik\base\Config::validateInputWidget('\\kartik\\date\\Da...',
'for DateControl...')","#2
C:\wamp\www\pub\vendor\kartik-v\yii2-datecontrol\DateControl.php(154):
kartik\datecontrol\DateControl->initConfig()","#3
C:\wamp\www\pub\vendor\yiisoft\yii2\base\Object.php(107):
kartik\datecontrol\DateControl->init()","#4 [internal function]:
yii\base\Object->__construct(Array)","#5
C:\wamp\www\pub\vendor\yiisoft\yii2\di\Container.php(372):
ReflectionClass->newInstanceArgs(Array)","#6
C:\wamp\www\pub\vendor\yiisoft\yii2\di\Container.php(151):
yii\di\Container->build('kartik\\datecont...', Array, Array)","#7
C:\wamp\www\pub\vendor\yiisoft\yii2\BaseYii.php(344):
yii\di\Container->get('kartik\\datecont...', Array, Array)","#8
C:\wamp\www\pub\vendor\yiisoft\yii2\base\Widget.php(97):
yii\BaseYii::createObject(Array)","#9
C:\wamp\www\pub\vendor\yiisoft\yii2\widgets\ActiveField.php(665):
yii\base\Widget::widget(Array)","#10
C:\wamp\www\pub\views\activite\schedules.php(49):
yii\widgets\ActiveField->widget('kartik\\datecont...',
Array)","#11
C:\wamp\www\pub\vendor\yiisoft\yii2\base\View.php(325):
require('C:\\wamp\\www\\pub...')","#12
C:\wamp\www\pub\vendor\yiisoft\yii2\base\View.php(247):
yii\base\View->renderPhpFile('C:\\wamp\\www\\pub...',
Array)","#13
C:\wamp\www\pub\vendor\yiisoft\yii2\base\View.php(149):
yii\base\View->renderFile('C:\\wamp\\www\\pub...', Array,
Object(app\controllers\ActiviteController))","#14
C:\wamp\www\pub\vendor\yiisoft\yii2\base\Controller.php(371):
yii\base\View->render('schedules', Array,
Object(app\controllers\ActiviteController))","#15
C:\wamp\www\pub\controllers\ActiviteController.php(407):
yii\base\Controller->render('schedules', Array)","#16 [internal
function]:
app\controllers\ActiviteController->actionAddsch('4')","#17
C:\wamp\www\pub\vendor\yiisoft\yii2\base\InlineAction.php(55):
call_user_func_array(Array, Array)","#18
C:\wamp\www\pub\vendor\yiisoft\yii2\base\Controller.php(151):
yii\base\InlineAction->runWithParams(Array)","#19
C:\wamp\www\pub\vendor\yiisoft\yii2\base\Module.php(455):
yii\base\Controller->runAction('addsch', Array)","#20
C:\wamp\www\pub\vendor\yiisoft\yii2\web\Application.php(84):
yii\base\Module->runAction('activite/addsch', Array)","#21
C:\wamp\www\pub\vendor\yiisoft\yii2\base\Application.php(375):
yii\web\Application->handleRequest(Object(yii\web\Request))","#22
C:\wamp\www\pub\web\index.php(12):
yii\base\Application->run()","#23 {main}"]}
Try installing the following widgets:
yii2-widget-datepicker
yii2-widget-datetimepicker
or better, install the kartik widgets using composer:
php composer.phar require kartik-v/yii2-widgets "*"
that will install the following widgets:
yii2-krajee-base
yii2-widget-activeform
yii2-widget-affix
yii2-widget-alert
yii2-widget-colorinput
yii2-widget-datepicker
yii2-widget-datetimepicker
yii2-widget-depdrop
yii2-widget-fileinput
yii2-widget-growl
yii2-widget-rangeinput
yii2-widget-rating
yii2-widget-select2
yii2-widget-sidenav
yii2-widget-spinner
yii2-widget-switchinput
yii2-widget-timepicker
yii2-widget-touchspin
yii2-widget-typeahead