How to migrate column type DOUBLE via cakephp phinx migrations? - mysql

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', [
// ...
])

Related

TYPO3 Extbase: Domain model property with value NULL is not converted to zero when updating record in controller action

I'm stuck with the following problem:
#1470230767 TYPO3\CMS\Extbase\Persistence\Generic\Storage\Exception\SqlErrorException
Column 'linked_module' cannot be null
I've got a domain model with this property:
/**
* #var Module|null
*/
protected ?Module $linkedModule = null;
Module is just another domain model and this value is not mandatory. The database definition looks like this:
linked_module int(11) unsigned DEFAULT '0' NOT NULL,
and last but not least the relevant TCA:
config => [
'autoSizeMax' => 1,
'default' => 0,
'eval' => 'int',
'foreign_table' => 'tx_psbriskassessment_domain_model_module',
'items' => [
['---', 0],
],
'maxitems' => 1,
'minitems' => 0,
'renderType' => 'selectSingle',
'size' => 1,
'type' => 'select',
]
In an action of my controller I want to reset the value of this property:
$module->setLinkedModule(null);
$this->moduleRepository->update($module);
This raises the exception (see beginning of this post).
I'm using TYPO3 v11.5.4 and assumed that the DataMapper would convert NULL to 0 because of the eval=int. But that does not happen.
I dived into the core and reached the function persistObject() in EXT:extbase/Classes/Persistence/Generic/Backend.php (line 295). If I read the code correctly, a NULL-value is always passed as NULL to the database - no conversion possible at this point.
I'm sure, I'm just missing a simple point. Disabling strict mode on the database server is no option for me. There has to be a clean solution.
null is different from 0. The relation could be to a record with UID 0 (which is unusual but not forbidden or prevented by design).
So, automatic conversion of a not-set relation ($linkedModule = null) to an integer 0 would falsify the information.
The simplest and cleanest way should be to define the database matching your model: without default value 0, but instead nullable.
linked_module int(11) unsigned,

CakePHP 3: Best Practice for Temporary SQL Tables

Dear CakePHP 3 developers,
I'd like to use SQL's Temporary Tables in a CakePHP 3.4.13 project for a single run through a script. Going through Cake's documentation, there seems no direct way to tell CakePHP my desire. How would I best go about it, then?
I've prepared a Table in src/Model/Table/TempItemsTable.php:
namespace App\Model\Table;
use Cake\ORM\Table;
class TempItemsTable extends Table
{
public $fields = [
'id' => ['type' => 'integer'],
'con' => ['type' => 'string', 'length' => 255, 'null' => false],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id']]
]
];
public function initialize(array $config)
{
// $this->setTable(null);
}
}
The idea to use $fields to tell CakePHP the desired table schema comes from a possibly unrelated documentation for Test Fixtures.
But how do I tell CakePHP not to look for an actual table in the database?
The uncommented line $this->setTable(null); was my poor attempt at that, which is supposedly similiar to the right way in earlier versions of CakePHP, but according to version 3.x documentation, setTable() doesn't accept null, while table() does, but it's deprecated as of 3.4 and also didn't change anything.
Finally, of course, I get this exception as soon as I try to access this "table" in a controller via $temp = TableRegistry::get('TempItems');:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mydatabase.temp_items' doesn't exist
Help, I'm stuck. :(
There's no need to tell it to not look for the table, actually that's the opposite of what you want to do, given that you eventually want to access it.
The table class should basically be configured as usual, and you should create the temporary database table before the application causes it to be accessed. You can either write the raw table creation SQL manually, or generate it from a \Cake\Database\Schema\TableSchema instance, which supports temporary tables.
You can either explicitly create the schema object:
$schema = new \Cake\Database\Schema\TableSchema('temp_items');
$schema
->addColumn('id', ['type' => 'integer'])
->addColumn('con', ['type' => 'string', 'length' => 255, 'null' => false])
->addConstraint('primary', ['type' => 'primary', 'columns' => ['id']])
->setTemporary(true);
$TableObject->setSchema($schema);
or let the table object generate it, using your fields definition array:
$TableObject->setSchema($TableObject->fields);
$schema = $TableObject->getSchema()->setTemporary(true);
You can then generate the table creation SQL from the schema object and run it against the database:
$connection = $TableObject->getConnection();
$queries = $schema->createSql($connection);
$connection->transactional(
function (\Cake\Database\Connection $connection) use ($queries) {
foreach ($queries as $query) {
$stmt = $connection->execute($query);
$stmt->closeCursor();
}
}
);
$queries would be an array of SQL commands required to create the table, something along the lines of:
[
'CREATE TEMPORARY TABLE `temp_items` (
`id` INTEGER AUTO_INCREMENT,
`con` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`)
)'
]
Note that if you do not assign the schema to the table object, you could run into caching problems, as the cached schema wouldn't match anymore when you change the table definition and do not clear the cache.
See also
Cookbook > Database Access & ORM > Schema System
Cookbook > Database Access & ORM > Database Basics

CakePHP 3 patchEntity SQL date fails

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');

how to create own traits migration in yii2

This is a default code for migration
<?php
use yii\db\Schema;
use yii\db\Migration;
class m150101_185401_create_news_table extends Migration
{
public function up()
{
$this->createTable('news', [
'id' => Schema::TYPE_PK,
'title' => Schema::TYPE_STRING . ' NOT NULL',
'content' => Schema::TYPE_TEXT,
]);
}
public function down()
{
$this->dropTable('news');
}
}
here TYPE_TEXT is a predefined trait ,
so how i can create my own trait for example
int(11) Not NULL unsigned to unsignedInt is there any way to create own traits.
For this you can define your personalSchemaBuilderTraits in a proper namespace and the recall this in your code
look at this yii2 doc http://www.yiiframework.com/doc-2.0/yii-db-schemabuildertrait.html
https://github.com/yiisoft/yii2/blob/master/framework/db/Migration.php
In Migration.php you can easly view in the firt line the call for use ....
In yii\db\SchemaBuilderTrait you can see the functions for settinge the value of the several column data type. This is the preferred method for create column from version 2.0.6

Add a MySQL field of type "bit" to a FuelPHP model

My schema has a column that is of type "bit(1)". I haven't found a way that this can be expressed in Fuel. They don't seem to support the "bit" type and can't properly build insert queries.
Is there a way (possibly undocumented) to get Fuel to support this?
hmm... the orm supposedly accepts the bit field.
i have created a model from database. Look my migration script and model, maybe it can help you.
Model
class Model_Test extends \Orm\Model
{
protected static $_properties = array(
'id',
'whatever',
);
protected static $_table_name = 'tests';
}
Migration script
builded from an existing table using the command: oil refine fromdb:model test
namespace Fuel\Migrations;
class Create_tests
{
public function up()
{
\DBUtil::create_table('tests', array(
'id' => array('constraint' => 11, 'type' => 'int', 'auto_increment' => true, 'unsigned' => true),
'whatever' => array('type' => 'bit'),
), array('id'));
}
public function down()
{
\DBUtil::drop_table('tests');
}
}
In the controller, you must be need cast the value as INT
$f = Input::post('whatever_post_field');
$o = Model_Test::forge(array('whatever' => (int)$f));
$o->save();