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

INVO: How is defined Products->getProductTypes()

Can someone who know INVO example, to explain to me where it comes from the method Producst->getProductTypes() ? It looks not defined on INVO Products model class.

I am trying to integrate INVO products into VOKURO. Everything else works fine in VOKURO but I get the error Model 'ProductTypes' could not be loaded with post data to Products search page and it looks the getProductTypes() is nowhere defined in the INVO code. Probably is something related with ORM but I am lost.

Thanks a lot!



22.7k
Accepted
answer

Hello, you misspelled "Producst->getProductTypes()" in your comment, however can you post your Product model? usually they ProductTypes is a "Has Many" ajoined and the "get" part is magic



3.4k

models/Products.php is joined with models/ProductTypes.php

you need both models in vokuro if you want get ProductTypes.

In Products, you have this function, who is in charge to do join with orm to call Products->getProductTypes()

public function initialize()
    {
        $this->belongsTo('product_types_id', 'ProductTypes', 'id', array(
            'reusable' => true
        ));
    }

If you want reflexion with ProductTypes->getProducts(), you need to create ProductTypes initialize() function too

/**
     * ProductTypes initializer
     */
    public function initialize()
    {
        $this->hasMany('id', 'Products', 'product_types_id', array(
            'foreignKey' => array(
                'message' => 'Product Type cannot be deleted because it\'s used in Products'
            )
        ));
    }

For more information, use google cache to see phalcon models relationships

https://webcache.googleusercontent.com/search?q=cache:7huF6N3jTUYJ:https://docs.phalcon.io/en/3.0.0/reference/model-relationships.html+&cd=1&hl=fr&ct=clnk&gl=fr&client=firefox-b-ab
edited Jun '17

Thanks Trent, and sorry for misspelling. Thanks enrollv, too.

Your clue helped me in understanding how tables relationships are defined with HasMany and BelongsTo, and 'getProductTypes()' method looks appearing magically if tables are linked correctly by the models.

The Products model is the same from INVO ( https://github.com/phalcon/invo/blob/master/app/models/Products.php ) but adding:

namespace Vokuro\Models;

Now all it works if I use namespaces (as Vokuro use) and 'alias' (I don't know why, have to learn about).

Changing Products initialization from

public function initialize()
{
    $this->belongsTo('product_types_id', 'ProductTypes', 'id', array(
        'reusable' => true
    ));
}

to

public function initialize()
{
    $this->belongsTo('product_types_id', __NAMESPACE__ . '\ProductTypes', 'id', array(
         'alias' => "ProductTypes",
        'reusable' => true
    ));
}

did the job.

Thanks again.



3.4k

can you pass your post in solved if everyting works now ? ;o))