Error reading DB-component development

For Joomla! 5.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Post Reply
JJSJJS
Joomla! Intern
Joomla! Intern
Posts: 91
Joined: Wed Jun 11, 2014 7:33 pm

Error reading DB-component development

Post by JJSJJS » Mon Nov 20, 2023 8:32 pm

Hi,

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;
}
}
This is in the site/src/View/firstpage/HtmlView.php:

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);
	}
}
And this in site/tmpl/firstpage/default.php

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>
Any idea what I'm doing wrong?
Thanks a lot!

User avatar
brian
Joomla! Master
Joomla! Master
Posts: 12733
Joined: Fri Aug 12, 2005 7:19 am
Location: Leeds, UK
Contact:

Re: Error reading DB-component development

Post by brian » Mon Nov 20, 2023 8:57 pm

are you trying to get the current user id?

$user = $this->getCurrentUser();
$fluerid = $user->id
"Exploited yesterday... Hacked tomorrow"
Blog http://brian.teeman.net/
Joomla Hidden Secrets http://hiddenjoomlasecrets.com/

JJSJJS
Joomla! Intern
Joomla! Intern
Posts: 91
Joined: Wed Jun 11, 2014 7:33 pm

Re: Error reading DB-component development

Post by JJSJJS » Mon Nov 20, 2023 9:12 pm

Yes, indeed.
When used in default.php like this:
use Joomla\CMS\Factory;
$user = JFactory::getUser();
$flyerid = $user->get('id');
I could echo the correct current user id.

Therefore I thought it would be useable in the Model too.
And based on that it should get the rows where that id is present.


Post Reply

Return to “Joomla! 5.x Coding”