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 relation

It happens that I have to instantiate my referenced model before getting the model relation data. Do you need to do that?

    $customer = new Customers();
    $systemUser = SystemUsers::findFirst();
    echo "<pre>"; var_dump($systemUser->customers); echo "</pre>";
    //works, show correct database row

    //$customer = new Customers();
    $systemUser = SystemUsers::findFirst();
    echo "<pre>"; var_dump($systemUser->customers); echo "</pre>";
    //doesn't work, internal error

update (I might find the cause of the problem)

    public function initialize(){
        //$this->hasOne('id', 'customers', 'id__system_users'); // I used lowercase 'c' for 'customers', and I will have to instantiate the Customers model
          $this->hasOne('id', 'Customers', 'id__system_users'); // it works just like the example from the doc, I get the referenced row without instantiation
    }

In your model do you have a hasmany?

 public function initialize()
    {
        $this->hasMany("id", "Customers", "user_id");
    }


25.7k
edited Jan '15

thanks @trentramseyer. I guess I found the cause, it is my careless mistake. I shall do more test to know it better.



25.7k
edited Jan '15

I did some more testes, and perhaps you already know the information I found. Allow me to still post my study here hopefully it can help other people in the future. If it is "hasMany", I get a "Phalcon\Mvc\Model\Resultset\Simple" object; if it is "hasOne", I get the (I guesss it is) the model object.