Specifying unsigned attribute when using the Schema Builder Trait in MIgrations? - yii2

I have to specify a column as unsigned in yii2 migrations .
Example migration code from manual
public function up()
{
$this->createTable('news', [
'id' => $this->primaryKey(),
'title' => $this->string()->notNull()
]);
}
From the research I have done there doesn't seem to be a method to add the unsigned capability in schema builder trait.
But is there some other way I can add unsigned attribute to the column while still making use of the schemaBuilderTrait style methods ?
For instance the $this->string() above returns an instance of yii\db\ColumnSchemaBuilder, but that doesn't even have a property to set unsigned/signed..

Unfortunately, some things are impossible to write with new migrations syntax.
In this case you can use string concatenation like that:
'title' => $this->string()->notNull() . ' UNSIGNED',
Alternatively you can use old syntax (backwards compatibility is observed):
use yii\db\Schema;
...
'title' => Schema::TYPE_STRING . ' NOT NULL UNSIGNED',
P.S. You can post issue on official framework repo for this problem.
Update: It's already implemented, use ->unsigned() method. Note that you need to update framework. Thanks leitasat for information.

Just in case: they did it.
Now you can add ->unsigned() to your definition.

Related

Doctrine 2.5 - Generate schema provider class from existing database

Question
Are there any existing tools able to generate an implementation of Doctrine\DBAL\Migrations\Provider\SchemaProvider reflecting the state of an existing database?
I googled for a while and looked at similar questions in SO but i couldn't find an answer. They are all related to Symfony + Doctrine ORM.
Why do i need this?
I want to use doctrine migrations to manage/track changes on an existing database. I cannot jump into using doctrine ORM because I would need to refactor the database and it would break other ( non php ) applications that depend on it.
I know i can use migrations without ORM, but i need to provide a concrete implementation of Doctrine\DBAL\Migrations\Provider\SchemaProvider (documentation), in my case this would mean to rewrite the entire database.
If i had the SchemaProvider generated for the first time to reflect the state of the database. Eg
<?php
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Migrations\Provider\StubSchemaProvider;
final class MySchemaProvider implements SchemaProvider {
public function createSchema()
{
$schema = new Schema();
$table = $schema->createTable('foo');
$table->addColumn('id', 'integer', array(
'autoincrement' => true,
));
$table->setPrimaryKey(array('id'));
//and so on for the rest of the databse...
return $schema;
}
}
I would be able to edit the generated class and create migrations with:
$ ./doctrine migrations:diff

Adding a 'varbinary' MySQL field type with Phinx Migrate

I'm trying to create a migration in Phinx which will create a varbinary type field in a MySQL DB to store an ip_address.
This is what I have:
$table = $this->table('my_table');
$table->addColumn('ip_address', 'varbinary', ['after' => 'id', 'limit' => 16])
->save();
However this simply returns:
[InvalidArgumentException]
An invalid column type "varbinary" was specified for column
"ip_address".
I tried using 'binary' but this just ended up as a BLOB. :/
Its currently not possible, I did however have the same problem and have just created a pull request for adding in this functionality: https://github.com/robmorgan/phinx/pull/811
It would let you add a varbinary field with code like you currently have.
$table->addColumn('ip', 'varbinary', ['length' => 16]);

Unable to update a document elastica

I am trying to update a document with script as mentioned below:
$script = new \Elastica\Script( 'ctx._source.fuzzy = value', array( 'value' => 'y' ), 'groovy' );
$script->setId( 1 );
$this->getType()->updateDocument( $script );
I am not able to recognize what is wrong as there is no error message. Am I missing any step in this process?
As you don't get an error, I assume the operation worked as intended. As the updated is by default not immediately available on all shards and I assume you directly check afterwards if it worked, I recommend you to pass the 'refresh' option as following:
$type->updateDocument($script, array('refresh' => true));
A working example can be found here: https://github.com/ruflin/Elastica/blob/master/test/lib/Elastica/Test/TypeTest.php#L609
In the case that this is does not solve the issue, please post also the part where you add the document and query for it to check if it exists for further analysis.

Phalcon Update Model does not work

The problem is explained short with the following code. $category->update() results with true (or $category->save()) but really nothing happens.
$category = Category::findFirst('id=' . (int)$id);
if ($this->request->isPost()) {
$category->setCategoryId($this->request->getPost('category_id', 'int'));
$category->setLanguageId($this->request->getPost('language_id', 'int'));
$category->setName($this->request->getPost('name', 'striptags'));
$category->setDescription($this->request->getPost('description', 'striptags'));
$category->setSort($this->request->getPost('sort'));
$category->setValid($this->request->getPost('valid'));
if (!$category->update()) {
$this->flash->error($category->getMessages());
} else {
$this->flash->success(
$this->translator->_('Category was updated successfully')
);
}
}
The model-classes are generated with getter- and setter-methods and protected member-variables with the phalcon-devtools.
What am I doing wrong?
I know this problem was also discussed here but i unfortunately have not enough points to write a comment :)
Phalcon Version 1.2.3, MySQL 5.5 + Apache on Debian with PHP5.4.4
It might be something as simple as a column that doesn't accept nulls and a null is being slipped in. If you don't explicitly tell Phalcon not to do so, even if you don't have any validation, Phalcon will enforce the not null constraint on the field implicitly.
Disable not null validations by adding this to the top of your bootstrap file and try again:
\Phalcon\Mvc\Model::setup([
'notNullValidations' => false
]);
Validation may be to blame? Check validators
Have you checked the php error log? I found that Phalcon is sometimes not as verbose as I would have liked it to be...

CakePHP added column doesn't appear in query

I have added a new_column to a MyISAM table on a MySQLproduction server.
I have deleted the appropriate model cache file from the /app/tmp/models folder.
Upon reloading the page and inspection, the newly generated cache file for the model INCLUDES the new column.
The new column doesn't appear in SQL queries or results when read with fields => null.
debug($this->read(null, $id));
//Query generates fields: SELECT Model.column_a, Model.column_b, Model.id WHERE ...
//Results do not include the new column
debug($this->read(array('new_column'), $id));
//Query generates fields: SELECT Model.new_column, Model.id WHERE ...
//Results include the new column and id
Possible caveats and additional info:
The table has 39 columns (why would this be a problem?)
The model is an EXTENDED class from a plugin
The new column definition is TEXT DEFAULT null
Previous updates to the table gave expected results
Local development replica of the app doesn't have this issue
The test controller action enables Configure::write(debug => 2)
CakePHP 2.3.6, PHP 5.4.19, MySQL 5.1.70
How to make the Model recognize the new table definition? Are there any other cache systems I should investigate?
Edit #1:
debug($this->ModelName->getDataSource()->describe('table_name'));
// outputs all columns
// ...
'new_column' => array(
'type' => 'text',
'null' => true,
'default' => null,
'length' => null,
'collate' => 'utf8_general_ci',
'charset' => 'utf8'
)
Edit #2:
I have tried disabling the cache completely. Although the model picks up the new column, it is not really a solution.
Configure::write('Cache.disable', true);
// new_column appears in both query and results
Edit #3:
Further debugging shows that there is a discrepancy between these two results:
$this->ModelName->getDataSource()->fields($this->Model, 'ModelName');
//returns fields without new_colum
array_keys($this->ModelName->schema());
//returns fields with new_column
And this temporarily solves the problem:
$ds = $this->ModelName->getDataSource();
$ds->cacheMethods = false;
...
Method caching of the DboSource is the source of this issue. Slowly getting to the bottom of things.. :D
You have to clear all the cache files (but not directories) in app/tmp/cache whenever you update an application that is in production (debug is set to 0).
The actual cache causing problems in this issue was the app/tmp/cache/persistent/_cake_core_method_cache. Don't let the persistent part fool you, as it did me.
See this great answer for more details.
It is probably something related to the Database cache engine: in this case you can solve your issue just changing the query (for example, removing a field using the 'fields' statement). You can even try deleting the CakePHP's model cache: it's located in app/tmp/cache/models.
Happy Coding!
In fact what you can add a bootstrap file if you use a Plugin.
In my global bootstrap file I put:
CakePlugin::load('Myplugin', array('routes' => true, 'bootstrap' => true));
and in Plugin/Myplugin/Config/bootstrap.php I have
<?php
Cache::config('_cake_core_', array(
'engine' => 'File',
'prefix' => 'cake_core_myplugin_',
'path' => CACHE . 'persistent' . DS,
'serialize' => true,
'duration' => '+7 days',
));
?>
I added two columns in one of my models and I faced the same issue has you. Then I deleted all the models and persistent cache files and I change the duration inside my bootstrap file to 1 min.
Finally, my model was ok and reloaded. Later I go back to the initial duration.
It is durty, but it also looks like CakePHP messed up a little bit with cache.
Hope that it can help