Can't instantiate model in controller of different name

How come, in BalanceSheetController, I can’t do

$line_item = new LineItem;

You should be able to call any model in any controller, but it’s not easy to know why with that few information.

Did you define in your config file to load the models?




'import' => array(

        'application.models.*',

),



Or if not did you import LineItem’s model in BalanceSheetController?

There is no information. I have an echo statement just before this line of code and just after. The first echo statement is executed then the app completely stops. Nothing after this line happens. No error message, no stack trace, no nothing.

Yes, main.php contains that import. Did that automatically and I haven’t changed it.

As to doing the import in the controller, I’m game but how exactly do you do that? Do you do it just before you instantiate the object, or do imports go at the top of the file, or above the class statement?

This is crazy - IMHO. When I replace the line

$line_item = new LineItem;

with

Yii::import(‘application.models.LineItem’);

it works. I just start assigning values:

$line_item->sequence_number = "blablabla";

And it knows that $line_item is a LineItem object! I don’t like that.

May be you could try to put


Yii::import('application.models.LineItem');

at the begining of your controller’s file like:


<?php

Yii::import('application.models.LineItem');

class BalanceSheetController extends Controller

{

...

}

?>

And call the previous line in your method:




$line_item = new LineItem;



Does it bring an error?

This is disturbingly weird. As previously mentioned I found a terrifically ugly work-around (getting rid of the “new” statement and using import). Further in the code, then, I successfully instantiated a different object - using new. Huh?? But the variable name wasn’t much like the model name… So I went back to the LineItem and tried new with variable $item instead of $line_item and it worked. Not cool. So, it was dependent on the variable name??? That variable name appeared in only one other place in the file, the line that called this function. To recreate the problem I then changed $item back to $line_item - in other words, I made it exactly as it was to begin with - and the damn thing now works. I spent almost a day on this nonsense. Non-printable character? I cannot imagine what else it could have been.

Sounds like something with the inflector.

Yii converts table names like app_user to appUser internally when generating models, etc. So if you have a variable line_item and a model lineItem this may conflict. Hoping that someone else can chime in and confirm.