Problem With Building My Own Generator

Hello, friends!

I have a problem with creating gii generator.

The full story:

  1. I’ve found Yii extension (now I can’t find that on the Yii site) - gii generator, that creates SQL migrations. I’ve improved the generator (at start It didn’t supported rollback of migrations, and I’ve added this support). The generator is working very fine, and helps me and my team to commit database changes into git, and have the same database structures at local machines, and on main server.

  2. But, sooner I’ve understood that I need new generator, that creates migrations which would automatically fill some table with dumped data. For example, if someone works on a module, that require some DB tables filled with some initial data, he need to manually get dump from this tables, put it into migrate generator, and make a migration. Because of high frequency of this operations I’ve decided to write new generator - that would create migration cansists of sql, filling selected table with dump of local one.

  3. And I’ve made it, Migrate Dump Generator. It’s generating what I want, except of the last stage - the file doesn’t write to the folder! This is a my problem…

As you know, gii generation consists of 3 or more steps:

-> enter data, needed to generate and press "Preview"

-> If there are no errors - you can click on the file in table “Code file” and view it’s content (this step is forks great for me), also you could uncheck files in this table, if you don’t want them to be generated.

-> press "Generate", to put generated file to the place where it have to be. At this step there are a grey frame appearing at the bottom of page, and showing the stages of progress. In my case, It shows something like this:

Generating code using template "./protected/gii/MigrateDump/templates/default"...

   skipped migrations/m130730_165814_user.php


I’ve digged into framework files, and found that “skipped” is written by operator on line 338 of file CCodeModel.php, and it used in case, when user unchecked checkbox near the file on “Preview” page of generator. But I didn’t unchecked this checkbox! I’ve tried to analyze this strange behavior with xdebug step-by-step execution, but no luck…

So, I need help from someone familiar with building generators… This bug is very strange, because of my generator is just a copy of first generator, mentioned here with little differencies…

More details:

Answering to my question bu myself.

When building migrations, name of file is differs from time to time, because it has format like m130808_095902_test.php (mYYMMDD_hhmmss_classname.php), but checkbox “Generate” in the form name = md5($filename), so, when filename changes, checkbox doesn’t transfer it’s value into CCodeModel object, because the name is changed.

To avoid this behavior, we need to save filename somewhere, for example, pass it through <input type=hidden> to the next action. But, since all $_POST parameters, passed to next step are putted to the model by $model->attributes = $_POST[$modelClass], the field must be in rules() array.

So, adding the line:

array('_migrateName','match','pattern' => '#m[0-9]{6}_[0-9]{6}_[a-zA-Z0-9\._]+#i'), 

to the rules() function of my model, I’ve solved the problem. Looks like very dummy problem, but without understanding mechanism of inner work of CCodeModel it was hard to understand, what is the source of problem.

Also, for anyone, who interested, I’ve updated my repos on github (mentioned early) and you could use this gii generators for your pleasure.

Thank you anyone who tried to think in the direction of my problem.