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.

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


3.3k
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.