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.