yii2 migration generates createTable instead of addColumn - yii2

I'm having trouble auto-creation migrations with bizley/yii2-migration-creator extensions on table updates. Initially, it works as expected with new tables:
<?php
use yii\db\Migration;
class m200122_110631_update_table_yii_urban_tourdate extends Migration
{
public function up()
{
$this->createTable('{{%urban_tourdate}}', [
'id' => $this->primaryKey(),
'name' => $this->string()->notNull(),
'time' => $this->dateTime(),
'duration' => $this->integer(),
'tour_id' => $this->integer(),
'tourguide_id' => $this->integer(),
'tourcourse_id' => $this->integer(),
'start_station_id' => $this->integer(),
'stop_station_id' => $this->integer(),
'status' => $this->integer(3)->notNull(),
'created_by' => $this->integer(),
'updated_by' => $this->integer(),
'created_at' => $this->integer(),
'updated_at' => $this->integer(),
]);
}
public function down()
{
$this->dropTable('{{%urban_tourdate}}');
}
}
Then after adding a column directly in database and creating another migration, I get a createTable statement as above (with the added column), which results in error (table already exists) when applying migration.
My expectation would have been to get only addColumn statement like this:
public function up()
{
$this->addColumn('urban_tourdate', 'position', $this->integer());
}
What am I doing wrong? Thanks!

You have to call
yii migration/update urban_tourdate
the second time (not create).

Related

Associate tables from different database cakephp 3.0

I have two database with name default and default_history. And tables with name users and wafer_detail_history under default database and order_history under default_history database. want to associate Users table with OrderHistory table.
OrderHistoryTable :-
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('order_history');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->hasMany('WaferDetailHistory', [
'foreignKey' => 'order_id'
]);
$this->belongsTo('Users', [
'foreignKey' => 'created_by',
'joinType' => 'INNER'
]);
}
i used this.
$connection = ConnectionManager::get('default_history');
$this->OrderHistory = TableRegistry::get('OrderHistory');
$this->OrderHistory->setConnection($connection);
$id = 37;
$order_history = $this->OrderHistory->get($id, ['contain' => ['Users']]);
but not able to succeed. getting this error:
Base table or view not found: 1146 Table 'default_history.users'
doesn't exist
I had the same problem few days ago,
You must had 'strategy' => 'select' in your BelongTo to join with the other database
$this->belongsTo('Users', [
'strategy' => 'select'
'foreignKey' => 'created_by',
'joinType' => 'INNER'
]);
Try this on the OrderHistoryTable.php File:
$this->setTable('default_history.order_history');

CakePHP 3 is saying a table doesn't exist in another database when using an ORM query

I have an application in CakePHP 3.5.13 which uses four different databases.
In my config/app.php I have configured two of these as follows:
'Datasources' => [
'default' => [ 'database' => 'sdb5_hub_subdb', ... ],
'db_orgs' => [ 'database' => 'dev_hub_subdb_orgs', ... ]
];
So this means there are 2 databases with the names sdb5_hub_subdb and dev_hub_subdb_orgs.
I'm attempting to do a query which does a join on a table in each database.
I have my Table entities configured as follows:
// src/Model/Table/TblListsSubstancesCommentsTable.php
public function initialize(array $config)
{
$this->belongsTo('Substances', [
'foreignKey' => 'substance_id',
'joinType' => 'INNER'
]);
}
public static function defaultConnectionName()
{
return 'db_orgs';
}
// src/Model/Table/SubstancesTable.php
public function initialize(array $config)
{
$this->belongsTo('TblListsSubstancesComments', [
'foreignKey' => 'substance_id'
]);
}
When I attempt to do the following with the ORM:
$query = $Substances->find()->select(['id' => 'Substances.id'])->distinct();
if ($this->request->getData('org_information')) {
$org_information = $this->request->getData('org_information');
$query = $query->matching('TblListsSubstancesComments', function ($q) use ($org_information) {
return $q->where(['TblListsSubstancesComments.comment LIKE' => '%'.$org_information.'%' ]);
});
}
It's producing an SQL error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'sdb5_hub_subdb.tbl_lists_substances_comments' doesn't exist
I don't understand this because the table definitely exists. Furthermore my TblListsSubstancesCommentsTable.php has defaultConnectionName() in it to specify that it should use the db_orgs connection. So I assume it must know to go to the second database to load that table? It's like it's looking in the default database and not finding it, but I don't know why, because the Table entity is telling it where it needs to look.

Validation rule unique requires at least 1 parameters. laravel 5.4

I've got problem with my laravel 5.4 I can't save the data
public function rules()
{
return [
'permission_id' => 'required|unique',
'name' => 'required',
'label' => 'required',
];
}
Error
Validation rule unique requires at least 1 parameters.
the unique rule need at least the name of ur database table.
ur rules function should be like this:
public function rules()
{
return [
'permission_id' => 'required|unique:db_table_name',
'name' => 'required',
'label' => 'required',
];
}
for more informations check laravel doc unique rule

Authentication Component

I am using CakePHP for the first time. So, please, be patient with me:
I have a table called admin_users, when i try to add the Auth component in my AppController.php.
While executing, I always receive an error telling me that: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db_test.users' doesn't exist.
My table is called admin_users, so the error could be because the table should be always called "users" to work properly?
Here is my implementation in AppController.php:
public function initialize()
{
parent::initialize();
$this->loadComponent('Flash');
$this->loadComponent('Auth',[
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
]
]
],
'loginAction' => [
'controller' => 'AdminUsers',
'action' => 'login'
]
]);
$this->Auth->allow(['display']);
}
and in AdminUsersController, the login function:
public function login()
{
if ($this->request->is('post')){
$adminUser = $this->Auth->identify();
if ($adminUser){
$this->Auth->setAdminUser($adminUser);
return $this->redirect($this->Auth->redirectUrl());
}
$this->Flash->error('Your username or password is incorrect.');
}
}
Thank you.
Try this
In AppController.php:
function beforeFilter() {
$this->Auth->userModel = 'admin_users';
}

How to connect 3 tables in yii2 and display in Gridview then make sorting work correctly

I have used the gii tool to create crud application. I have 3 tables the tbl_targetcities, lib_cities, and lib_provinces. I was able to connect lib_cities to tbl_targetciteis but not the lib_provinces. And also the sorting of city / Municipality does not work. It seems that it sorts according ti the ID.
tbl_target_cities
lib_cities
lib_provinces
sample View
So far here is my relation in the model.
public function getCityName()
{
return $this->hasOne(LibCities::className(),['city_code'=>'city_code']);
}
in my view file...
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute'=>'city_code',
'value'=>'cityName.city_name'
],
[
'attribute'=>'prov code',
'value'=>'cityName.city_name'
],
'kc_classification',
'cluster',
'grouping',
'priority',
'launch_year',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
How to display the prov_name from lib_provinces???
EDIT to answer user2839376 question in the comment box
IN THE SEARCH MODEL CLASS
$query = TblSpBub::find();
$query->joinWith('brgyCode')->joinWith(['cityCode'])->joinWith(['cityCode.provCode']);
$covered= LibAreas::find()->where(['user_id'=>yii::$app->user->identity->id])->all();
$query->all();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort'=> ['defaultOrder' => ['id'=>SORT_DESC]],
]);
$dataProvider->sort->attributes['city'] = [
'asc' => ['lib_Cities.city_name' => SORT_ASC],
'desc' => ['lib_Cities.city_name' => SORT_DESC],
];
$dataProvider->sort->attributes['province'] = [
'asc' => ['lib_provinces.prov_name' => SORT_ASC],
'desc' => ['lib_provinces.prov_name' => SORT_DESC],
];
In LibCities model add new relation:
public function getProvince()
{
return $this->hasOne(LibProvince::className(),['prov_code'=>'prov_code']);
}
And change getCityName relation. You should add with() for relation:
public function getCityName()
{
return $this->hasOne(LibCities::className(),['city_code'=>'city_code'])->with(['province']);
}
And in view correct your columnto this:
[
'attribute'=>'prov code',
'value'=>'cityName.province.prov_name'
],
You have to use the function relations() in models.
In tbl_target_cities model:
public function relations()
{
return array(
'city' => array(self::HAS_ONE, 'LibCities', 'city_code'),
);
}
In LibCities model :
public function relations()
{
return array(
'province' => array(self::HAS_ONE, 'LibProvinces', 'prov_code'),
'targets' => array(self::HAS_MANY, 'TargetCity', 'city_code',
);
}
This will allowed you to jump throw the LibCities model,
now you can simply acces to prov name like this :
$model->city->province->prov name;
Note : You need to have the 3 models defined.
EDIT
array(
'name' => 'province name',
'value' => $data->city->province->prov_name;
),
Done it, Heres how.
in addition to the above code (original post)
// in Model I added an additional function
public function getTaskowner()
{
return $this->hasOne(Tasks::className(), ['id' => 'task_id'])
->with(
['location','taskowner']
);
}
and in view i did this
....
'columns' => [
....
[
'class' => 'kartik\grid\DataColumn',
'value'=> 'tasks.location.taskowner.name',
.....
],
.....
and it worked
key points. used an array with the 'with->(..)' to include both then in the view added 'tasks.location.taskowner.name', to join them all