Warning: foreach() argument must be of type array|object, bool given
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.
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.
- JurajB
- Joomla! Guru
- Posts: 642
- Joined: Fri Oct 02, 2015 3:28 pm
Warning: foreach() argument must be of type array|object, bool given
Hello!
I have this error on this part of code:
<?php foreach ($this->items as $i => $item) : ?>
<?php echo $item->name; ?>
</br>
<?php endforeach; ?>
Please help!
I ask because its in the book like this so I want to resolve how reliabe it is.
Thanks!
I have this error on this part of code:
<?php foreach ($this->items as $i => $item) : ?>
<?php echo $item->name; ?>
</br>
<?php endforeach; ?>
Please help!
I ask because its in the book like this so I want to resolve how reliabe it is.
Thanks!
-
- Joomla! Hero
- Posts: 2909
- Joined: Fri Jul 05, 2013 10:35 am
- Location: Parts Unknown
Re: Warning: foreach() argument must be of type array|object, bool given
This should be self-explanatory. $this->items is a boolean and not a an array/object.
- JurajB
- Joomla! Guru
- Posts: 642
- Joined: Fri Oct 02, 2015 3:28 pm
Re: Warning: foreach() argument must be of type array|object, bool given
This is the code from model.
This is the code from HtmlView:
This is the loop from tmpl/ircchat/default.php:
1. why is items an boolean, I see array there ([]).
2. what #__ before foos_details is for?
3. What does "Items" in get('Items') do ? Could I change that or where is this fixed?
Many thanks!
Code: Select all
public function __construct($config = [])
{
parent::__construct($config);
}
protected function getListQuery()
{
$db = $this->getDbo();
$query = $db->getQuery(true);
$query->select(
$db->quoteName(['id', 'name', 'alias'])
);
$query->from($db->quoteName('#__foos_details'));
return $query;
}
Code: Select all
protected $items = [];
$this->items = $this->get('Items');
Code: Select all
foreach ($this->items as $i => $item) {
echo $item->name;
}
2. what #__ before foos_details is for?
3. What does "Items" in get('Items') do ? Could I change that or where is this fixed?
Many thanks!
-
- Joomla! Hero
- Posts: 2909
- Joined: Fri Jul 05, 2013 10:35 am
- Location: Parts Unknown
Re: Warning: foreach() argument must be of type array|object, bool given
1. Probably because an error occurs when running the DB queries. Use model's getErrors() method to check for errors.
2. #__ is the table prefix placeholder that is changed to the actual prefix when executing queries.
3. The view's get() method is shorthand for model's get[Something]() method. In this case $this->get('Items') ends up calling default models' getItems() method, if the model and the method exist. Otherwise, it ends up calling parent (Joomla\CMS\Object\CMSObject::get()) method which essentially returns the view's property, if it exists.
2. #__ is the table prefix placeholder that is changed to the actual prefix when executing queries.
3. The view's get() method is shorthand for model's get[Something]() method. In this case $this->get('Items') ends up calling default models' getItems() method, if the model and the method exist. Otherwise, it ends up calling parent (Joomla\CMS\Object\CMSObject::get()) method which essentially returns the view's property, if it exists.
- JurajB
- Joomla! Guru
- Posts: 642
- Joined: Fri Oct 02, 2015 3:28 pm
Re: Warning: foreach() argument must be of type array|object, bool given
Hello I'm back!
Isnt there some doc about the model's error ?
Thank you very much! <3
Isnt there some doc about the model's error ?
Thank you very much! <3
-
- Joomla! Hero
- Posts: 2909
- Joined: Fri Jul 05, 2013 10:35 am
- Location: Parts Unknown
Re: Warning: foreach() argument must be of type array|object, bool given
The methods are documented in the API documentation https://api.joomla.org/cms-4/namespaces ... model.html.
- JurajB
- Joomla! Guru
- Posts: 642
- Joined: Fri Oct 02, 2015 3:28 pm
Re: Warning: foreach() argument must be of type array|object, bool given
Hello!
Guys I founded that when I remove the plugin and than install it it works.
Anyway I want to debug it so I know whats wrong...
So how do I setup getError(s) in my code, please?
Here is my model:
Guys I founded that when I remove the plugin and than install it it works.
Anyway I want to debug it so I know whats wrong...
So how do I setup getError(s) in my code, please?
Here is my model:
Code: Select all
class IrcchatModel extends ListModel
{
public function __construct($config = [])
{
parent::__construct($config);
}
protected function getListQuery()
{
$db = $this->getDbo();
$query = $db->getQuery(true);
$query->select(
$db->quoteName(['id', 'name', 'alias'])
);
$query->from($db->quoteName('#__foos_details'));
return $query;
}
}