Warning: foreach() argument must be of type array|object, bool given

For Joomla! 4.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.
Locked
User avatar
JurajB
Joomla! Guru
Joomla! Guru
Posts: 642
Joined: Fri Oct 02, 2015 3:28 pm

Warning: foreach() argument must be of type array|object, bool given

Post by JurajB » Sun Dec 04, 2022 11:26 am

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!

SharkyKZ
Joomla! Hero
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

Post by SharkyKZ » Sun Dec 04, 2022 4:55 pm

This should be self-explanatory. $this->items is a boolean and not a an array/object.

User avatar
JurajB
Joomla! Guru
Joomla! Guru
Posts: 642
Joined: Fri Oct 02, 2015 3:28 pm

Re: Warning: foreach() argument must be of type array|object, bool given

Post by JurajB » Mon Dec 05, 2022 7:09 am

This is the code from model.

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;
    }
This is the code from HtmlView:

Code: Select all

protected $items = [];
$this->items = $this->get('Items');
This is the loop from tmpl/ircchat/default.php:

Code: Select all

foreach ($this->items as $i => $item) {
    echo $item->name;
}
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!

SharkyKZ
Joomla! Hero
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

Post by SharkyKZ » Mon Dec 05, 2022 7:29 am

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.

User avatar
JurajB
Joomla! Guru
Joomla! Guru
Posts: 642
Joined: Fri Oct 02, 2015 3:28 pm

Re: Warning: foreach() argument must be of type array|object, bool given

Post by JurajB » Tue Dec 06, 2022 10:11 am

Hello I'm back!
Isnt there some doc about the model's error ?
Thank you very much! <3

SharkyKZ
Joomla! Hero
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

Post by SharkyKZ » Tue Dec 06, 2022 12:22 pm

The methods are documented in the API documentation https://api.joomla.org/cms-4/namespaces ... model.html.

User avatar
JurajB
Joomla! Guru
Joomla! Guru
Posts: 642
Joined: Fri Oct 02, 2015 3:28 pm

Re: Warning: foreach() argument must be of type array|object, bool given

Post by JurajB » Sun Dec 11, 2022 5:00 am

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:

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;
    }
}


Locked

Return to “Joomla! 4.x Coding”