Yii Framework Forum: Using Active Records In Migrations - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Using Active Records In Migrations Rate Topic: -----

#1 User is offline   InsaneWookie 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 04-September 12

Posted 11 July 2013 - 11:20 PM

Not sure if this is a bug or by design. But I am unable to use an active record model in a migration against the non default database

I'm trying to run the migrations against a test database. But if I call
new MyModel()
or
MyModel::model()


The model always uses the default database, not the database provided with
yiic migrate --connectionID=testdb


I'm thinking you're not supposed to use active records in migrations by use the provided SQL query help functions?

But I found this post http://www.yiiframew...8981#entry78981 that seems to suggest that using an active record in a migration is fine?
0

#2 User is offline   codesutra 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 613
  • Joined: 15-March 11
  • Location:India

Posted 12 July 2013 - 12:32 AM

Your test database have a same tables which you are using in your development database.
Since, MyModel class related table is present in your dev database. And please also check console.php database settings which database you are accessing there.
CodeSutra
0

#3 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,992
  • Joined: 17-January 09
  • Location:Russia

Posted 12 July 2013 - 06:04 AM

It's by design. You should never use model in migrations since model code can be changed slightly while one should be able to run migrations from the very first (where model was different) to the current.
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I work on Yii fulltime: https://www.patreon.com/samdark
0

#4 User is offline   InsaneWookie 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 04-September 12

Posted 12 July 2013 - 08:13 PM

View Postsamdark, on 12 July 2013 - 06:04 AM, said:

It's by design. You should never use model in migrations since model code can be changed slightly while one should be able to run migrations from the very first (where model was different) to the current.


Cheers, that makes sense.
That was the conclusion I was coming to.

Time to fix up my migrations.
0

#5 User is offline   Trejder 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,257
  • Joined: 06-October 10
  • Location:Southern Poland

Posted 13 March 2015 - 03:39 AM

View Postsamdark, on 12 July 2013 - 06:04 AM, said:

It's by design. You should never use model in migrations since model code can be changed slightly while one should be able to run migrations from the very first (where model was different) to the current.

Two questions here:

1. How to import model to migration, when (temporarily) ignoring your advice? Neither this:

Yii::import('application.models.*');

echo Content::CONTENT_STATUS_CLOSED;

nor that:

Yii::import('application.models.Content');

echo Content::CONTENT_STATUS_CLOSED;

works:

PHP Error[2]: include(C:\XAMPP\websites\website\protected\models\Content.php):
failed to open stream: No such file or directory
	in file C:\XAMPP\websites\yii\framework\YiiBase.php at line 404

2. Following previous one. How to deal with the situation like above, that you have a constant defined in your model and you want to use it in migration?

I've solved above with:

require_once dirname(__FILE__).'/../models/data/Content.php';

But this looks like a pretty ugly way of doing this. Is there a better way?
Proud Cookbook author, though still learning powerful Yii! :] See my generic profile for more information. Cheers!
0

#6 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,992
  • Joined: 17-January 09
  • Location:Russia

Posted 17 March 2015 - 02:46 AM

Why shooting yourself in a leg when you were warned?
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I work on Yii fulltime: https://www.patreon.com/samdark
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users