We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.7k
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'));            
    }