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.

magic getter

I'm trying to get use the magic getter.

I get this: Phalcon\Mvc\Model\Exception: The method "getGroups" doesn't exist on model "Module\Inventory\Models\Items"

Can someone please let me know what I'm doing wrong?

Table: Items {ItemID, ItemGroupID, Name}, ItemGroups {ItemGroupID, GroupName}

// Model: Items

namespace Module\Inventory\Models;

class Items extends BaseModel
{
    function initialize()
    {
        $this->setSource("xyz_items");

        $this->belongsTo('ItemGroupID', '\Module\Inventory\Models\Groups', 'ItemGroupID', array(
            'reusable' => true
        ));
    }
}
// Model: Groups

namespace Module\Inventory\Models;

class Groups extends BaseModel
{
    public $ItemGroupID;
    public $Name;

    function initialize()
    {
        $this->setSource("xyz_groups");

        $this->hasMany('ItemGroupID', '\Module\Inventory\Models\Items', 'ItemGroupID');
    }
}
// Controller: Items

use Module\Inventory\Models\Items as Items;

class IndexController extends ControllerBase
{

    public function indexAction()
    {
        $items = Items::find();

        $this->view->setVar("items", $items);
    }

}
// View: Index

foreach ($items as $item) 
{
    echo $item->getGroups()->GroupName ?>
}


84.5k
Accepted
answer

Alias the relation:

$this->belongsTo('ItemGroupID', '\Module\Inventory\Models\Groups', 'ItemGroupID', array(
    'reusable' => true
    'alias' => 'groups'
));