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.