Yii2 application with config:
...
'formatter' => [
'locale' => 'ru_RU',
],
],
...
I call from the template Yii::$app->formatter->asCurrency($item->price) and got 12 675,00 ₽. It is ok, but now I would like to get rid of the decimals at all.
What is the right way to do this?
Try using NumberFormatter
Yii::$app->formatter->asCurrency(12675.00, null, [\NumberFormatter::MAX_SIGNIFICANT_DIGITS => 100])
asCurrency()
Related
I'm trying to have a time input in TYPO3 9 LTS working together with MySQL 5.7.24.
In the ext_tables.sql the field gets defined like this:
some_field time default NULL
In the TCA the field gets defined like this:
'some_field' => [
'exclude' => 1,
'label' => 'Some field',
'config' => [
'type' => 'input',
'dbType' => 'time',
'eval' => 'time',
],
],
When saving the record in the backend without a time input (which should be possible) I'm getting the error:
These fields of record 1 in table "some_table" have not been saved correctly: some_field! The values might have changed due to type casting of the database.
When looking at the database record the some_field field gets the value 00:00:00 (although the db default is NULL).
When selecting a time the record can be saved and opened without error.
Is this a bug in TYPO3 or how could I fix this behavior?
The bug can be solved by having the following eval:
'eval' => 'time,null',
That means you have given the wrong type for the value on your ext_tables.sql. Additionally, TYPO3 v9 has renderTypes.
Try something like that:
ext_tables.sql
begin int(11) DEFAULT '0' NOT NULL
TCA
'begin' => [
'exclude' => true,
'label' => 'LLL:EXT:your_ext/Resources/Private/Language/locallang_db.xlf:tx_yourext_domain_model_modelname.begin',
'config' => [
'type' => 'input',
'renderType' => 'inputDateTime',
'size' => 10,
'eval' => 'datetime',
'default' => time()
],
],
Additional information!
If you want to display the time in FrontEnd you could use something like that
<f:format.date>{dateObject.begin}</f:format.date>
If you want to modify how it looks, you can use the format attribute as well:
<f:format.date format="%d. %B %Y">{dateObject.begin}</f:format.date>
More about that here: TYPO3 Date format
Trying to use the Yii2 TimestampBehavior but when saving it will return a doesn't have default value error.
The values are not required in the rules. The DB column is a INT(11).
As I needed to save several rows I use batchInsert() for this.
When I add a the value with time() it will save everything to the DB.
public function behaviors()
{
return [
'timestamp' => [
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => 'created_at',
ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',
],
'value' => new Expression('NOW()'),
],
'blameable' => [
'class' => BlameableBehavior::className(),
'createdByAttribute' => 'created_by',
'updatedByAttribute' => 'updated_by',
],
];
}
Yii::$app->db->createCommand()->batchInsert( 'table_cms',
[
//all fields but not the timestamps
],
[
//rows here
])
->execute();
batchInsert() does not support ActiveRecord features, so TimestampBehavior has no effect here. TimestampBehavior will be used if you call save(). In your case you need to pass timestamp manually.
doesn't have default value probably means that you're using MySQL in strict mode - it will warn you if you will try to add record without specifying all fields without default values.
I have install extension from http://www.yiiframework.com/extension/yii2-latlon-finder/
but is showing me error message "MissingKeyMapError"
I have API key but not sure where to put this.
please help me for same
As i can see, this project is last updated 2 years ago, so it can be outdated with current API's.
As a solution, i can suggest you to use 2amigos - Google Maps extension. It's pretty simple with good documentation.
After installing it, just add this to main.php config file:
'components' => [
'assetManager' => [
'bundles' => [
'dosamigos\google\maps\MapAsset' => [
'options' => [
'key' => 'this_is_my_key',
'language' => 'id',
'version' => '3.1.18'
]
]
]
],
],
Just replace this_is_my_key with your key.
The bad thing in this solution is that you will have to write some additional code to handle user input of lat/long and display it on 2amigos map.
I am logging into Db using existing Yii logging API.
But I want to differentiate between Frontend logs and Backend logs inside DB.
Everything that appears is common for both, I face difficulty tracing frontend logs.
Below is the image of DB Logs where GREEN marked are for backend logs, RED marked are for Frontend Logs.
You can use prefix property for this. This is callable that returns a string to be prefixed to every exported message with signature function ($message).
As default getMessagePrefix() is used there which prefixes the message with user IP, user ID and session ID.
You can use it to add there frontend and backend.
Thanks to #Bizley!
Inside both backend/config/main and frontend/config/main, I configured below; This is how my entire log configuration for Frontend looks like(Similarly you can do it for Backend);
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\DbTarget',
'levels' => ['error'],
'prefix' => function ($message) {
return "[Frontend]";
},
],
[
'class' => 'yii\log\FileTarget',
'levels' => ['error','info'],
],
],
],
Below is the view on UI for logs. With the Help of Prefix I can now easily differentiate between channels.
I'm sending a post request in a test case, and I want to assert that a specific element, let's say with key 'x' exists in the response. In this case, I can't say seeJson(['x' => whatever]); because the value is unknown to me. and for sure, I can't do it with seeJson(['x']);.
Is there a way to solve this?
If it matters:
Laravel: v5.2.31
PHPUnit: 5.3.4
May it will be helpful for anyone else. You can write this test for your check response json structure
$this->post('/api/login/', [
'email' => 'customer3#example.com',
'password' => '123123123',
])->assertJsonStructure([
'status',
'result' => [
'id',
'email',
'full_name',
],
]);
Although it's not optimal at all, I chose to use this code to test the situation:
$this->post(URL, PARAMS)->see('x');
X is a hypothetical name, and the actual element key has a slim chance of popping up in the rest of the data. otherwise this nasty workaround wouldn't be practical.
UPDATE:
Here's the solution to do it properly:
public function testCaseName()
{
$this->post(route('route.name'), [
'param1' => 1,
'param2' => 10,
], [
'headers_if_any' => 'value'
]);
$res_array = (array)json_decode($this->response->content());
$this->assertArrayHasKey('x', $res_array);
}