We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

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.

belongsTo and hasMany Relationship

Hello,

I am using phalcon 3.0, php7 and the newest vokuro version, but i get a notice.

Here is the notice:

Notice: Undefined property: Phalcon\Mvc\Model\Resultset\Simple::$diverse

This is my Papers.php model:

public function initialize()
{
    $this->belongsTo('usersId', __NAMESPACE__ . '\Users', 'id', [
        'alias' => 'user'
    ]);
}

And my Users.php Model:

    $this->hasMany('id', __NAMESPACE__ . '\Papers', 'usersId', [
        'alias' => 'paper',
        'foreignKey' => [
            'message' => 'User cannot be deleted because he/she has activity in the system'
        ]
    ]);

The volt index.volt:

{{ user.paper.diverse }}

Thank you for your help :-)

Rgds Stefan



1.4k
Accepted
answer

I think "paper" would be a collection or an array. Try to loop or access within index:

{{ user.paper[0].diverse }}

or

{% for p in user.paper %}
    {{ p.diverse }}
{% endfor %}


59.7k

Hello Jimmy Chandra,

thank you for your reply, i will try this at the evening :-)

But in the search.volt of vokuro we have this syntax for volt: {{ user.profile.name }}

How can i reach this? Here is the link: https://github.com/phalcon/vokuro/blob/master/app/views/users/search.volt

Rgds

Stefan

edited Aug '16

By using hasOne, but then this be 1-1 relation. If you have 1-n relation it's obvious that you need to use array on some point.