ActiveStructure

[size=“1”]i don’t have match time and describe how i came to it.(if some one will be interested in that just comment i will answer later).[/size]

[center]ActiveStructure[/center]

Active structure represent structure of DB, that currently used,

  1. when DB is empty it possible to fill it with needed structure

  2. when DB contained some structure its possible to generate this ActiveSctructure objects

  3. and synchronizing structure betwin Db and AS objects.

  4. possible to use DB structure changes in realtime, for creating DB-structure programmatically.

some usage example

class Post extends ActiveStructure

{

public $structure = array(

array(‘id’=>‘int’,‘primarykey’),

array(‘post’=>‘text’),

array(‘user’=>‘text’),

)

(overrides) public synchronize(){

return false;//no sincronization

return $this;//sincronize db to this file

return ‘data_base’;//sincronize this to target database;

}

}

Looks like AR + migrations?

Yes, looks like you right its something like migrations but working with it more in manner AR, and definitely synchronized between AR <-> ActiveStructure.

Well, I don’t think Yii should go this way. It makes things much more complex. Now we are syncing models with database. There are a lot of nice tools to work with DB, a lot of articles and books on it.

In your case we have to learn new syntax that will generate DB and model. Additionally it will have a big performance impact since we’ll need to check if database and model is up to date.

  1. Complexity - how you measure it?

  2. If you want to change application behavior with data go to nice tool, make changes - apply, regenerate Model, make changes to model, insted of just changing such ActivStructure class.

  3. no new syntax, just in special cases when you need to optimize performance etc.Generally database used to store php-types , why should do (int),(string),(bool),(data), etc(php base clases), and another thing is great that create some serialized objects and store it in AR nativity db via ActiveStructure.

  4. don’t forget that will be possible to create/manage/delete Tables, in real time (great thing for temporary,memory, etc. tables)

  5. of what performance impact you talk, if you already use AR, impact is not more then regenerate Model, when needed.(done once per change)

  1. Requirement to learn new syntax and rules.

  2. Yes, it’s nice during development but, I think, migrations are more convenient. You can track what was changed by your team and adapt your code accordingly.

  3. How can I tell if a change was made?

  1. syntax ыhould be as intuitive as possible (php or AR like).

example try:

class Post extends ActiveStructure {

public $columns=(

‘id’=>array(‘int’,‘pk’),

‘message’=>array(‘string’,‘nn’),//not null

‘user_id’=>array(‘int’,HAS_MENY_LIKE_AR_RELETIONSHIP),

‘uniq_record’=>array(‘char(32)’,‘nn’,‘unique’);// something like md5 of message

);

} // is it to hard, or not understandable?

// my favorite is OOP approach like this

class Post extends ActiveStructure {

public $id;

public $message;

public $user_id;

public $uniq_record;

public $syncronize = ActiveStructure::ClassPriority;

//or ActiveStructure::DataBasePriority;

public function inicialize{ // or __construct()

$this->id = new ASculumn();

$this->id->type = ASculumn::int;

$this->id->pk = true;

$this->message= new ASculumn();

$this->message->type = ASculumn::string;

$this->message->nn = true;

$this->user_id= new ASculumn();

$this->user_id->type = ASculumn::int;

$this->user_id->relations = ‘HAS_MENY_LIKE_AR_RELETIONSHIP’;

$this->uniq_record = new ASculumn(Asculumn::char(32)); '=>array(‘char(32)’,‘nn’,‘unique’);

$this->uniq_record->notnull = true;

$this->uniq_record->unique = true;

}

// btw, is it possible to create Yii’s ActiveRecord like that?

}

  1. in team development there more advantage ,imagine you will always have "fresh" structure class, and will be very usable when adopt new changes.

  2. select structure from DB , create object ActiveSctucture and compare them or , create some kind of hash and compere them, and then just mark it as synchronized.

  1. So what is the advantage if we have to manually compare and synchronize changes? Aren’t migrations better for this task?
  1. so its not manual, is it not possible to compare it in ActiveStructure class it self?

like ,

1-> select * from data_base_structure even posible to use AR to get it.

2-> initialize current table;

3-> compare between 1,2 in foreach () foreach loop.

If we’ll compare it in the class itself, this comparison will degrade performance slightly because of usage of PHP reflexion and SQL schema queries. If we’ll cache it then there are no pros compared to migrations.

its like migrations but with possibility to change structure in real-time, even create and delete tables and use them.

Well, with migrations you can change structure. And you can do the same in realtime using query builder (see http://code.google.com/p/yii/source/browse/trunk/docs/guide/database.query-builder.txt?r=2738, starting from "Building Schema Manipulation Queries").

generally speaking why using active record when you can use query builder, why not? meaby its because of manageability?

In some consideration it seems that ActiveStructure is really a part of ActionRecord, because their already is mention of relations, and its really possible to back-translate to database structure.

More of that its not a bad idea to have some information about types of columns in AR.

Well, maybe. I don’t think we’ll implement this in 1.1 releases so if you really want this you can try creating an extension to see if it actually works as a feature.

yes, i think its can be some major thing in 1.2 maybe?

about extension i don’t realy have knowledge about database abstraction if it has some(i hope)

i can create it for mysql that just proofing the idea, will it help?

I think so. At least you’ll receive some comments about idea itself.

Right now, as I can see, there is only one positive vote in this topic poll.

i believe opinion of 8 active users of forum not enough to judge an idea.

Some similar idea is discussed here.

I am personally think automatic database changes can be too dangerous - some bug can dynamically destroy database. Usually when I upgrade database I make backup of current structure and make sure that everything is ok on local database copy before upgrading production version. So I prefer to run migrations manually.

Another one problem is support for different databases and support for some features like triggers, view and procedures which can be hard to describe as php array.

Also CActiveRecord already has information about database columns - see getMetaData() method, so back sync from database to AR is already present.

I think something along the lines of this idea, but implemented in a more Yii-aware way, would be nice.

Such as, automatically creating migration classes from DB (on-the-fly even, when change is detected).

Then, using migrations, an option could be added to automatically attempt applying migrations when a newer migration version is detected.

No need for the complexities suggested in the ActiveStructure proposal, while still retaining some of the possible advantages.