CakePHP 3 patchEntity SQL date fails - mysql

I have an array like this:
['valid_from' => '2016-02-01']
In my model I have the following validation rule
$validator->date('valid_from')->allowEmpty('valid_from');
When I try to patch the entity with the array I get this:
'valid_from' => object(Cake\I18n\FrozenDate) {
'time' => '2168-12-02T00:00:00+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
}
The column in MySQL is of the type date. (I don't want to use datetime as I don't need the time for calculations)
What am I doing wrong?

Think your issue is that your locale is misconfigured try one of these methods.
The default locale can be set in your config/bootstrap.php folder by using the following line:
ini_set('intl.default_locale', 'fr_FR');
Changing the Locale at Runtime
use Cake\I18n\I18n;
I18n::locale('de_DE');

Related

TYPO3 9 LTS type casting error on mysql time field for TCA type input / dbType time saving empty field

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

How to migrate column type DOUBLE via cakephp phinx migrations?

I'm trying to migrate table from db1 to db2 with using Phinx migrations, but i have a problem with one table where i have column type DOUBLE. I know that supported types are there Phinx column type, but it is possible to specify FLOAT type to get DOUBLE in diff_migration ? I use cakephp version 3.5.6.
My example migration_diff
<?php
use Migrations\AbstractMigration;
class Diff003 extends AbstractMigration
{
public function up()
{
$this->table('test_double')
->addColumn('double1', 'float', [ // here type DOUBLE is changing to FLOAT
'default' => null,
'limit' => null,
'null' => true,
])
->addColumn('double2', 'float', [
'default' => null,
'limit' => null,
'null' => true,
])
->create();
}
public function down()
{
$this->dropTable('test_double');
}
}
The DOUBLE type has been implemented recently, and will probably be available in the next Phinx release (it's been added as of version 0.10.7), see https://github.com/cakephp/phinx/pull/1493.
Until then you can for example use the custom column type feature:
->addColumn('double1', \Phinx\Util\Literal::from('DOUBLE'), [
// ...
])
or manually add the columns via raw SQL, something like:
$this->execute('ALTER TABLE test_double ADD COLUMN double1 DOUBLE NULL');
or if you're adventurous, use the Phinx master branch until the stable release is available:
composer require robmorgan/phinx:dev-master
->addColumn('double1', 'double', [
// ...
])

Select default schema for MS SQL

I need to configure Yii2 to work with Microsoft SQL server.
The db configuration file (db.php) is something like this
return [
'class' => 'yii\db\Connection',
'dsn' => 'sqlsrv:Server=192.168.77.111;Database=xyz',
'username' => 'xy',
'password' => 'xyz',
This works only if i add before the table name in the tableName() function inside all models the correct schema name.
For example:
public static function tableName()
{
return '{{%xyzschema.users}}';
}
How can i set the db configuration so xyzschema is always added when connecting to the table?
I tried with tablePrefix and schemaMap with defaultSchema but it doesn't work
The error returned is
Invalid object name 'users'.
or
Invalid object name 'xyzschema.users'.
If i add tablePrefix to db.php
Update: The defaultSchema property inside connection's schemaMap/Schema config array gets ingored
For this case i solved changing the schema to the standard "dbo" for each table.

Use DB expression or PHP datetime as timestamp?

I have a timestamp behavior (yii2) that looks like this (taken from user guide example)...
public function behaviors()
{
return [
'timestamp' => [
'class' => TimestampBehavior::className(),
'value' => new Expression('NOW()'),
],
];
}
But is it not better to use a PHP DateTime expression like...
'value' => (new /DateTime('NOW'))->format('Y-m-d H:i:s')
Seems to me like it would be better to work with one clock rather than possibly two (in case the database server is separate and time not in sync, which is not unlikely.) Especially if I'm using DateTime in my code to check other conditions.
Which approach is better and why?
Expression class created a DBMS function inside the SQL query, it works no matter which date type (or format, it is possible to force the date format in some DBMS) you have in your column. If you change the value of a behavior you should take care the format and type of your column does match, otherwise some time you would get an error.

Cakephp 3.0 I would like to include a Year input field with a drop down, but it is inputing as an array

I have a Year field in a form and I am using FormHelper.
echo $this->Form->input('year', [
'type' => 'year',
'minYear' => date('Y')-10,
'maxYear' => date('Y')
]);
The table file validator looks like:
->add('year', 'valid', ['rule' => 'numeric'])
->allowEmpty('year')
I have a very similar input in another app that seems to work fine. I set the MySql column to int(5) to match what I had working elsewhere.
Checking debugkit it shows the "year" input as an array while the other inputs are strings. If I remove the validation rule it throws an illegal array to string conversion, so I assume this is where the error is.
Any help is greatly appreciated.
I have just tested with your above code and it is working fine for me. Try to delete the cache and check it once more.
Creates a select element populated with the years from minYear to maxYear. Additionally, HTML attributes may be supplied in $options. If $options['empty'] is false, the select will not include an empty option:
empty - If true, the empty select option is shown. If a string, that
string is displayed as the empty element.
orderYear - Ordering of
year values in select options. Possible values ‘asc’, ‘desc’. Default
‘desc’ value The selected value of the input.
maxYear The max year to
appear in the select element.
minYear The min year to appear in the
select element.
Try this one:
<?php
echo $this->Form->year('exp_date', [
'minYear' => date('Y')-10,
'maxYear' => date('Y'),
'id' => 'cc-year',
'class' => 'form-control',
'empty' => false,
'orderYear' => 'asc'
]);
?>
Official Documentation: CookBook - Creating Year Inputs