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.

Access to undefined property Apps\Main\Models\Items::Comments

hi

i make a Multiple application.

i used this code for get Comments in Current Item :

$item = \Apps\Main\Models\Items::findFirst($id);

$this->view->setVar('comments',$item->Comments);

in Items.php model :


<?php
namespace Apps\Main\Models;
class Items extends Model
{

    public function initialize()
    {        
        $this->hasMany('id','\Apps\Main\Models\Comments','item_id');
    }
}

And in Comments.php model:

<?php
namespace Apps\Main\Models;
class Comments extends Model
{   
    public function initialize()
    {
        $this->belongsTo('item_id','\Apps\Main\Models\Items','id');         
    }
}

but Show this error:

Notice: Access to undefined property Apps\Main\Models\Items::Comments in /home/xxx/ebazal.ir/public_html/apps/main/controllers/VController.php on line 135

Why ?

sry for bad type english.



4.6k
Accepted
answer
edited Jun '15

Try adding alias to relationship

In items


    public function initialize()
    {        
        $this->hasMany('id','\Apps\Main\Models\Comments','item_id', array('alias' => 'Comments'));
    }

In comments


    public function initialize()
    {
        $this->belongsTo('item_id','\Apps\Main\Models\Items','id', array('alias' => 'Items'));            
    }