views:

64

answers:

3

I'm using Doctrine together with CodeIgniter.

I have can't quite figure out the logic of why the generated models from my YaML starting in 'A' crash.

For example generating a model from the following YaML will work fine:

Pguy:
  columns:
    ptext: string(255)

Likewise:

Zguy:
  columns:
    ztext: string(255)

will produce models/Zguy.php and models/generated/BaseZGuy.php which also work fine.

However, if I start my class name with 'A', the generated model will cause to fail:

Aguy:
  columns:
    atext: string(255)

Starting with 'B' does the same thing - crash!

I can't see why this is. The code within the files Zguy.php | BaseZguy.php are exactly the same as the code in Aguy.php | BaseAguy.php - with the exception of the class and variable starting letter.

Ideas?

(Disclaimer: I am blonde)

A: 

I do know that doctrine loads models alphabetically, this can cause problems with dependencies, for example, your model "Amodel" has a ManyToMany relation with your "Kmodel", that will cause problems, because the Kmodel is not yet knwon by Doctrine. This also can happen with *B*aseModels.

Does this help? If not please define "crash" better.

smoove666
A: 

I had the same problem once. It's been a long time ago, but in my case it had something to do with the order models are loaded and the fact that every class extends his own BasicClass parent. i.e.:

Class A extends BaseA {

}

Doctrine autoloads the classes alphabetical. In this case, the autoloading tries to load the A class which extends the BaseA class which is not yet loaded. This explains why all classes that start with an A will crash and all classes that have a name starting later in the alphabet then Base won't crash.

Maybe I'm completely wrong, but that was the problem I had in the past. If I remember correct I fixed it by putting all my Base classes in an Abstract directory. In my case I did not have classes with names earlier in the alphabet, so for me it was a dirty but effective workaround.

BTW, when posting a problem like this, some extra code examples would be nice. Now people just have to guess.

Stegeman
Thanks Stegeman. I will have a play around and see if this explains things.
So Over It
A: 

Thanks 'Stegeman' and 'smoove666'. It indeed seemed that the alphabetical autoloading of models was causing my issues.

The generated models ended up in a directory structure as follows:

./models/Aguy.php
./models/Pguy.php
./models/Zguy.php
./models/generated/BaseAguy.php
./models/generated/BasePguy.php
./models/generated/BaseZguy.php

Thus any class name starting with a letter before 'g' would "crash" as the Base* models in the generated folder had not yet loaded (ie. Aguy.php would "crash" but Pguy.php and Zguy.php would work)

Moving all the Base* models out of the generated folder into the models directory partially solved this problem. Now any class starting with a letter > 'b' would work (ie. now classes starting in C to F would not crash as Base* models had autoloaded loaded prior).

To completely get the solution working, I placed all Base* models into a directory called _generated as _ comes before 'a' alphabetically.

The directory structure now looks like:

./models/_generated/BaseAguy.php
./models/_generated/BasePguy.php
./models/_generated/BaseZguy.php
./models/Aguy.php
./models/Pguy.php
./models/Zguy.php

Problem solved - little hair left.

Thanks for your input.

edit:

A more elegant solution to this problem can be found at: http://stackoverflow.com/questions/1856260/why-cant-i-create-a-doctrine-model-named-album/1856827#1856827

The problem can be solved by telling Doctrine explicitly the loading order of the directory containing the models

Doctrine::loadModels(array(APPPATH.'/models/generated', APPPATH.'/models'));

Credit to Sander Versluys

So Over It