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 relations: alias doesn't work, getRelated() does

I've got a class I'm initializing from my Controller:

<?PHP

namespace Model;

class Contact extends Person{

    public function getSource(){
        return 'person';
    }

    public function initialize(){
        $this->hasMany('username','\Model\Organization','contact_username',['alias'=>'Organizations']);
        $this->hasOne('username','\Model\PersonContact','username',['alias'=>'Additional']);
    }

    public function isAssociated(){
        if(count($this->Organizations)){
            return TRUE;
        }
        return FALSE;
    }
}

From my controller (and view), I get a WTF error

$Contact = Person::findFirstAsType($username);
echo $Contact->Additional->position; // This generates an error
echo $Contact->getRelated('Additional')->position; // This works just fine

Any ideas as to why the alias fails, but getRelated() works?



145.0k
Accepted
answer

What happens if you will change Additional to lower case ?



5.7k
edited Apr '16

All of the following should work.

echo $Contact->additional->position;
echo $Contact->getAdditional()->position; 
echo $Contact->getRelated('Additional')->position;

I usually use the middle option.

Me too, but with adding this method to model. So ide got it.

What happens if you will change Additional to lower case ?

That did it, but I'm at a loss as to why - I've used uppercase aliases in the past with no problems. Thanks for the answer nonetheless.

I guess beacause there were some update.