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

Weird issue with model relationship

Not sure if I'm doing something wrong here but here goes..

I have 2 models, Products and ProductsPages that contain relations in the initialize() method in each file respectively:

$this->hasMany('id', 'Pkg\Lib\Core\Products\Models\ProductsPages', 'product_id');
$this->belongsTo('product_id', 'Pkg\Lib\Core\Products\Models\Products', 'id');

However upon calling $product->getProductPages(); the following exception is thrown:

The method "getProductsPages" doesn't exist on model "Pkg\Lib\Core\Products\Models\Products"

$product = Products::findFirst($productId); // correctly returns object of Pkg\Lib\Core\Products\Models\Products
$pages = $product->getProductsPages(); // throws exception


4.5k
Accepted
answer
edited Aug '14
<?php
   $this->hasMany('id', 'Pkg\Lib\Core\Products\Models\ProductsPages', 'product_id', array(
     'alias' => 'ProductsPages'
        ));

If you want to you like

    $produc->getProductsPages();

Then you should add alias, cause Phalcon doesnt understand like that, maybe use of that model at start would fix that too, but alias is faster solution!

I'm sure I tried that before posting and it didn't work!! Either way it's working now.

Thanks.