I'm using the boilerplate from github to create a component, installs ok, DB is created so far so good.
So I added a row in a simple table manually via phpmyadmin. It has id, flyerid, nonflyerid.
Now trying to get the data (list) based on the users id onto the site page,but whatever I try I'm getting this error: foreach() argument must be of type array|object, null given.
I tried multiple examples e.g. J4 MVC component tutorial, Techfry tutorial, Astrid's Tutorial, J4.x:Selecting_data_using_JDatabase etcetera.
Perhaps one of you can find the culprit and guide me a bit to the right direction.
This is what I have in site/src/Model/firstpageModel.php (I left out all non-used comments, and tried several combinations of the below):
Code: Select all
namespace FlyerRoot\Component\Flyer\Site\Model;
defined('_JEXEC') or die;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Model\ListModel;
use Joomla\Database\ParameterType;
class firstpageModel extends ListModel
{
public function __construct($config = [])
{
parent::__construct($config);
}
protected function getListQuery()
{
$user="";
//use Joomla\CMS\Factory;
$user = JFactory::getUser();
$flyerid = $user->get('id');
$db = $this->getDatabase();
$query = $db->getQuery(true);
$query
->select($db->quoteName(['id', 'flyerid', 'nonflyerid']))
->from($db->quoteName('#__flyerlist'))
->where($db->quoteName('flyerid') . ' = :flyerid')
->bind(':flyerid', $flyerid);
// Reset the query using our newly populated query object.
$db->setQuery($query);
//$query->from($db->quoteName('#__flyerlist'));
$results = $db->loadObjectList();
//$row = $db->loadRowList();
//print_r($row);
return $results;
//return $query;
// Select the required fields from the table.
//$query->select(
// $db->quoteName(['id', 'flyerid', 'nonflyerid'])
//);
//$query->from($db->quoteName('#__flyerlist'));
//return $query;
}
}
Code: Select all
<?php
namespace FlyerRoot\Component\Flyer\Site\View\firstpage;
\defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\View\GenericDataException;
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
class HtmlView extends BaseHtmlView
{
public function display($tpl = null): void
{
// Check for errors.
/*
if (count($errors = $this->get('Errors')))
{
throw new GenericDataException(implode("\n", $errors), 500);
}
*/
$this->items = $this->get('Items');
parent::display($tpl);
}
}
Code: Select all
<?php
\defined('_JEXEC') or die('Restricted access');
use Joomla\CMS\Factory;
?>
<div class="col-md-4">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>FlyerID</th>
<th>NonFlyer ID</th>
</tr>
</thead>
<tbody>
<!-- <?php //if (is_array($this->items) || is_object($this->items)) { ?> -->
<?php foreach ($this->items as $i => $item) : ?>
<!-- <?php //foreach ((array) $this->$items as $i => $item) : ?> -->
<tr>
<td><?php echo $item->flyerid; ?></td>
<td><?php echo $item->nonflyerid; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
Thanks a lot!