Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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?



99.5k
Accepted
answer

What happens if you will change Additional to lower case ?



4.1k
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.