extending AR Models

Hello all,

i’m newbie in framework based web development. I still noticed that some other frameworks and

ORM libraries provide base model classes, to be extended, to avoid the overwriting of actual model

classes that may have been changed by the developer. Since it can be common to modify the db structure

in development, it can be frustrating each time having to re-edit each model file or the like.

I would like to know from more experienced Yii developers if there is a common way to walkaround this problem,

maybe extending the models generated by Yii? How such a thing can be accomplished in Yii-routing-terms?

Thanks in advance for any hint!

PS: i searched the forum about this topic but haven’t found nothing… if i’m duplicating a discussion subject… shame on me…:) just point me to the correct thread… thanks

You don’t really need base model classes because Yii automatically detects the latest db structure. That is, if you re-name, add or remove a column, the existing model class doesn’t need to be changed, unless you already use the changed column in your custom code (in this case, even if you have base class, you still need to change your extended class code).

Hi qiang,

first of all thanks for your steady reply…!

i am still confused about this thing, i mean rules(), search(), attributeLabels(), relations() can become outdated if i add a column or a new fk to the table i’m working on. How does Yii update the structure of those functions if i don’t regenerate them? i’ll have to modify them by hand?

i know i’m missing something and i apologize for being dumb… :)

thanks again for your time and for the beatiful framework you and your fellow developers made.

True, rules, relations, etc. could be outdated. However, you never rely on the rules/relations generated by the code generation tool. They are just a good starting point. In 99% of the time, you will need to remove and add some. Having a base class won’t allow you to easily remove some. And it has the drawback of scattering your code in two places, making your code harder to read.

Thanks for the explanation. Now i understand a little more about all this.

In any case i’m just scratching the surface of it and i will need more and more

sleepless hours to master it all.

best regards!

Hello, I found this thread and thought it was the best-related topic to some questions that I have about Yii, Gii and ActiveRecord models.

I started working with Yii in Jan 2011, but have experience with PHP for 8+ years, Zend Framework, PHP ORM’s, and other development environments such as .NET. I use PHP and .NET professionally at work, and PHP for all of my personal projects. I love what you all have done with Yii, and I am currently using it heavily for a large project, so I am sold on using Yii.

That being said, I am finding some difficulty in my continuous development process with Yii’s ActiveRecord models and using Gii to keep them in sync with the database. Are there any good practices for customizing AR models and still using Gii? I have Gii generating my initial models, but then I continually add tables, add/update columns, and add relationships (through MySQL InnoDB Foreign Key constraints). Whenever I want to make a customization to an AR model class, then later make a structural change to the DB, I would prefer to use Gii to keep the model up-to-date. Gii finds file differences and can only overwrite the changes, leaving me to have to re-apply the AR model customizations manually.

Qiang, I realize in your previous post that you said Gii is intended to be a starting point for classes, then changes should be merged in manually. Though, that is not the preferred route for my development process. Ideally, AR models could be generated in such a way that Gii creates a base class as well as a derived class that can be customized. Then when Gii is run again on all models, customizations can be preserved instead of marking differences in red, with the only option being to overwrite the AR model. An alternative may be to allow Gii to merge changes, similar to SVN diff/merge, however this won’t work in many cases when the generated code directly conflicts with manual changes.

In .NET (C#), there is the concept of Partial Classes, such that the base class can be generated and never touched, then partial classes can be controlled by developers to add functionality and business logic. Often it is the case that working within the same strongly-typed class is better than using extended classes. [Side Note: For anyone who reads this and does not like .NET, there are actually some great features in .NET and something nice about strongly-typed variables that PHP lacks, especially when using a PHP IDE for object introspection and code completion.]

There is an interesting article with an experiment for PHP Partial Classes here: www.toosweettobesour.com/2008/05/01/partial-classes-in-php/ . This looks like a useful approach with two classes, one inside the other, but with some downsides. An alternative mentioned in the comments says to use an Include file for the partial class within the base class. I am considering trying these approaches in my Yii project and custom Gii Model template; I would be interested in contributing my findings back to the Yii platform if other developers would find the concepts useful.

Before going that route, I was wondering if there are any better alternatives in working with Yii’s ActiveRecord models and Gii. Any thoughts and suggestions on the topic? Thanks for your time.