how to create own traits migration in yii2 - 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

Related

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

YII2 Codeception - create fixture. [Error] Class 'tests\fixtures\UserFixture' not found

I'm using YII2 2.0.10, basic. It seems that it don't use yii-codeception module anymore. So documentation from this page is not actual:
http://www.yiiframework.com/doc-2.0/guide-test-fixtures.html
My problem is that I can't load fixture to my test.
I'm using such way:
tests\unit\models\UserTest.php
<?php
namespace tests\models;
use app\models\User;
use tests\fixtures\UserFixture;
class UserTest extends \Codeception\Test\Unit
{
public function testFindUserById()
{
// load fixtures
$this->tester->haveFixtures([
'user' => [
'class' => UserFixture::className(),
// fixture data located in tests/_data/user.php
'dataFile' => codecept_data_dir() . 'user.php'
]
]);
// get first user from fixtures
$this->tester->grabFixture('user', 0);
...
tests\fixtures\UserFixture.php
namespace tests\fixtures;
use yii\test\ActiveFixture;
class UserFixture extends ActiveFixture
{
public $modelClass = 'dektrium\user\models\User';
}
And have some data returned from tests\fixtures\data\user.php
After vendor/codeception/base/codecept run I get
2) UserTest: Find user by id
Test tests/unit/models/UserTest.php:testFindUserById
[Error] Class 'tests\_fixtures\UserFixture' not found
#1 tests\models\UserTest->testFindUserById
What I'm doing wrong?

cakephp 3 get property changes entity for dependent model(table)

I want to get the result set with extra field through afterFind. In cakePHP no afterFind function. So used protected function _getAttrName() {
return $this->_properties['name'];
}
inside the Entity class. But i did not get the output with 'attr_name' property. with find()
$this->loadModel('Products');
$this->loadModel('Attributes');
// Get the attributes to use as facets
$attributes = $this->Products->Attributes->find(['all',
'order' => 'Attributes.id',
])
->contain(['AttributeTypes'])
->where([
'Attributes.filterable' => true,
//'Attribute.required' => true,
])
->hydrate(false)
->toArray();
Can i get the solution?
You need to declare in your entity class what are the virtual properties you want to expose when converting to array or to json. This is the relevant section of the docs:
http://book.cakephp.org/3.0/en/orm/entities.html#exposing-virtual-properties
namespace App\Model\Entity;
use Cake\ORM\Entity;
class Product extends Entity
{
protected $_virtual = ['attr_name'];
protected function _getAttrName() {
return $this->_properties['name'];
}
...
}

Yii2 Gii Table Prefix

I allways setup table prefix - for this post lets say my prefix is abc_.
So in common\config\main-local.php. I have:
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=database',
'username' => 'user',
'password' => 'pwd',
'charset' => 'utf8',
'tablePrefix' => 'abc_',
],
...
I have worked on Yii1 and used gii to generate models.
In this version it generated files like: table.php.
Now I work with Yii2 and learn the differences:
gii generate files like abc_table.php. Yes - I checked "Use Table Prefix".
This is not ok because prefix should be transparent.
Could please anyone tell me what I'm doing wrong?
You may change the model class name AbcTest to Test. For future model generations, check the Use Table Prefix field in the Gii tool. Gii generate correct model like this:
class Test extends \yii\db\ActiveRecord
{
/**
* #inheritdoc
*/
public static function tableName()
{
return '{{%test}}';
}
...
}
In tableName method, it returns '{{%test}}' if you check Use Table Prefix in the Gii tool. If you do not check the Use Table Prefix, this method return 'abc_test' and generated model class will be named as AbcTest.

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