We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Model Namespaces with CamelHumping Broken?

I have been having a heck of a time getting the loader to work properly in a few different situations. I need to use both registerNamespaces and registerDirs to get it to work, which does not make sense to me, since all my classes are in namespaces.

One specific problem I have is that my app_role table cannot be found by the loader when referencing it, although my organization table can be found without a problem:

    $this->belongsTo('organization_id', "Organization", 'id', ['alias' => 'Organization', 'foreignKey' => TRUE]);
    $this->belongsTo('role_id', 'AppRole', 'id', ['alias' => 'AppRole', 'foreignKey' => TRUE]);
    ...
    $name = $this->organization->name;
    echo "NAME: $name<br/>";
    $name = $this->appRole->name;
    echo "NAME: $name<br/>";

The appRole->name causes a "cannot redeclare class AppRole" exception, no matter what case I use to reference it. The organization->name works just fine. The two classes are nearly identical. I've tried using \App\Models\AppRole in the class, with and without a leading slash. I've tried a require_once of the class, and many other things. Nothing seems to work.

I am new to Phalcon, and have been a Yii user for 5+ years. I've also worked reently with Nette, Slim and RedBean. I like the idea of a compiled MVC framework, and when it works Phalcons seems very intuitive. But with cases like this one can spin one's wheels a long time. Any help would be appreciated.

I have it working now, but I'm not happy with the solution. Part of it was my fault; I used the wrong foreign key for AppRole. The other part was using both registerNameSpaces() and registerDirs() so that models will get loaded. But something even stranger happened!

public function initialize() {
    $this->hasMany('id', 'Cogent\Models\Assessment', 'member_id', ['alias' => 'Assessments']);
    $this->hasMany('id', 'Cogent\Models\Recommendation', 'member_id', ['alias' => 'Recommendations']);
    $this->hasMany('id', 'Cogent\Models\MemberBadge', 'member_id', ['alias' => 'MemberBadges']);

    $this->belongsTo('role_id', 'Role', 'id', ['alias' => 'Role', 'foreignKey' => TRUE]);
    $this->belongsTo('organization_id', "Organization", 'id', ['alias' => 'Organization', 'foreignKey' => TRUE]);
}

Even though the Models directory is mentioned in both the registerNameSpaces() and registerDirs() calls, it needs to be specified in the hasMany() calls, but cannot be specified in the belongsTo() calls. I can't see how this is not a bug in the auto loader.