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

Possible Bug → Trying to assign a Related Model to a view variable in the control. Throws fatal PHP error yet still works...

Controller:

public function detailsAction() {

        $params  = $this->dispatcher->getParams();
        $id      = $params[0];
        $product = Product::findFirstById($id);
        if (empty($product)) {
            $this->response->redirect('/');
        }
        $this->view->setVar('product', $product);

        $this->view->setVar('images', $product->ProductImage());

        //Featured Products
        $this->view->setVar('related_products', $this->getRelatedProducts(4));
        $this->view->setVar('featured_products', $this->getRandomProducts(4));

    }

Models:

<?php

class Product extends \Phalcon\Mvc\Model {

    protected $config;

    public function initialize() {
        $this->hasMany('id','ProductCategory','product_id');
        $this->hasMany('id','ProductImage','product_id');
    }
}

<?php

class ProductImage extends \Phalcon\Mvc\Model {

    protected $config;

    public function initialize() {
        $this->hasOne('product_id','Product','id');
    }
}
[Thu Mar 05 18:41:50.126457 2020] [php7:error] [pid 24611] [client xxx.xxx.xxx.xxx:12345] PHP Fatal error:  Uncaught Error: Call to a member function getProductImage() on null in /var/www/example/app/controllers/ProductController.php:42\nStack trace:\n#0 [internal function]: ProductController->detailsAction('assets', 'images', 'banners', 'banner-sidebar....')\n#1 [internal function]: Phalcon\\Dispatcher\\AbstractDispatcher->callActionMethod(Object(ProductController), 'detailsAction', Array)\n#2 [internal function]: Phalcon\\Dispatcher\\AbstractDispatcher->dispatch()\n#3 /var/www/example/public/index.php(43): Phalcon\\Mvc\\Application->handle('/product/detail...')\n#4 {main}\n  thrown in /var/www/example/app/controllers/ProductController.php on line 42, referer: https://example.com/product/details/65

However when you go to the view on the .phtml the image appears fine.

The way I got around this error showing, was moving the relational call to the view itself

FROM

   <? foreach ($images as $image) { ?>
                                        <div class="product-item">
                                            <img class="product-single-image" src="<?=$image->url?>" data-zoom-image="<?=$image->url?>"/>
                                        </div>
                                    <? } ?>

TO

   <? foreach ($product->getProductImage() as $image) { ?>
                                        <div class="product-item">
                                            <img class="product-single-image" src="<?=$image->url?>" data-zoom-image="<?=$image->url?>"/>
                                        </div>
                                    <? } ?>

No idea why this works that way and why the error is thrown the other way yet still works.

Any help for others to find the root cause of this would be much appreciated.



77.7k
Accepted
answer

In your controller:

        $this->view->setVar('images', $product->ProductImage());

You are trying to access the named related records via magic __get, but you call it as a function. Just drop the parenthesis from the end, so you pass in the property, not a function result (which doesnt even exist)

Thank you very much! Sometimes you need a 2nd pair of eyes! Cheers!

In your controller:

       $this->view->setVar('images', $product->ProductImage());

You are trying to access the named related records via magic __get, but you call it as a function. Just drop the parenthesis from the end, so you pass in the property, not a function result (which doesnt even exist)