Related record not showing in view inside a foreach loop

Hi guys,

I am having a problem with a relationship between 3 models. (Products, ProductStock, PriceRules). I am showing Products and ProductStock has the current stock level, while PriceRules has the special price available for a product (if any). Products have a hasMany relationship with the other two models and the other models have a belongsTo relationship with Products.

Here are the relationships:

Products:

$this->hasMany('id', __NAMESPACE__ . '\PriceRules', 'product_id', array(
        'alias' => 'PriceRules',
        'foreignKey' => array(
            'action' => Relation::ACTION_CASCADE
        )
    ));

$this->hasOne('id', __NAMESPACE__ . '\ProductStock', 'product_id', array(
        'alias' => 'ProductStock',
        'foreignKey' => array(
            'action' => Relation::ACTION_CASCADE
        )
    ));

ProductStock & PriceRules both look like this:

 $this->belongsTo('product_id', _NAMESPACE_ . '\Products', 'id', array(
        'alias' => 'Products'
    ));

Now, in the view (using Volt), I al doing something like this:

{% for product in products %}
    {{ product.ProductStock.stock }}
    {{ product.PriceRules.price }}
{% endfor %}

While product.ProductStock.stock correctly displays the stock level, product.PriceRules.price comes up null.

I am logging all SQL queries and I can veryfy that the queries return the correct result for PriceRules. I can also verify this but dumping product.PriceRules, which contains the value I want.

Can anyone think of any reason PriceRules does not display the value?

edited Aug '16

product.PriceRules should be an array, so your price is probably in product->priceRules[x]->price, no?

    {% for product in products %}
        {{ product.ProductStock.stock }}
        {% for priceRule in product.PriceRules %}
            {{ priceRule.price }}
    {% endfor %}

PriceRules is resultset. Also dont really use it something like this. You should use querybuilde for joining producststock stock and price form pricerules.

just to expand on this ^

The reason why a querybuilder + joins are more preferable is because relations will generate a select for each model plus related submodels. Using joins will result in a single query. Querybuilder supports hydration, so you can also recreate a model from a joined resultset