Too few arguments to function Joomla\CMS\Table\Table::__construct(), 1 passed

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

Moderators: ooffick, General Support Moderators

Forum rules
Locked
Drammm
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Sun Jan 01, 2023 7:34 am

Too few arguments to function Joomla\CMS\Table\Table::__construct(), 1 passed

Post by Drammm » Sun Jan 01, 2023 7:40 am

There is a site on Joomla 3.10 for it has written a self-written component (not by me). I want to switch from php 7.4 to 8.2. Took a test server, moved the site. On the front seems to be without bugs, everything works. Site functional of the Jumla version 3.10 also works fine with PHP 8.2, but there are errors with the self-written component. Some I could fix myself, and for others not enough knowledge, please help.
Image
As I understand it, the constructor of the TABLE class gets 1 value, and I need three. Here is the constructor of a native Jumla class (line 148)
Image
I assume that the problem is in the model (specifically in the designer) of the self-written component (but I may be wrong). The logic is - since saving and editing native jumla materials works. So it's not in the kernel class, but in the self-descriptor.
Image
When you click save the request goes to the controller, method apply
Image
From there the request goes to the model of the item, method store (class VuzModelItem extends JModelLegacy - the beginning of this class at the beginning of this post)
Image
This is where the line confuses me

Code: Select all

$row =& $this->getTable();
I don't quite understand how the link transfer works, and it seems to cause an error (but taking away the ampedance doesn't solve the problem)

But beyond this line the request does not go, I checked.

User avatar
pe7er
Joomla! Master
Joomla! Master
Posts: 24986
Joined: Thu Aug 18, 2005 8:55 pm
Location: Nijmegen, Netherlands
Contact:

Re: Too few arguments to function Joomla\CMS\Table\Table::__construct(), 1 passed

Post by pe7er » Sun Jan 01, 2023 10:42 am

Welcome to Joomla forum!

Code: Select all

$row =& $this->getTable();
should be

Code: Select all

$row = $this->getTable();
It calls the getTable method in /libraries/src/MVC/Model/BaseDatabaseModel.php
(see https://github.com/joomla/joomla-cms/bl ... eModel.php )
public function getTable($name = '', $prefix = 'Table', $options = array())

That function expects 3 parameters. In your case you have to few parameters, hence "too few arguments" and "exactly 3 expected".

Maybe you could try something like

Code: Select all

$row = $this->getTable('#__table_name_of_your_component', 'Table', [ ] );
Kind Regards,
Peter Martin, Global Moderator
Company website: https://db8.nl/en/ - Joomla specialist, Nijmegen, Netherlands
The best website: https://the-best-website.com

Drammm
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Sun Jan 01, 2023 7:34 am

Re: Too few arguments to function Joomla\CMS\Table\Table::__construct(), 1 passed

Post by Drammm » Sun Jan 01, 2023 12:24 pm

Thanks for your help, you seem to be right, but how to correctly spell out the arguments to call the function?
But still all three arguments are optional and have default values, and this should not cause an error.
Image


If I have table #__vuz_items and I write

Code: Select all

$row = &$this->getTable('#__vuz_items', 'Table', []);
then I get an error

Code: Select all

Table __vuz_items is not supported. The file has not been found.
/var/www/vuz/libraries/src/MVC/Model/BaseDatabaseModel.php:490
In the /administrator/components/com_vuz folder
I have a file vuz.php
with the following contents

Code: Select all

<?php

defined('_JEXEC') or die('Restricted access');

require_once(JPATH_COMPONENT.'/controller.php');

$controller = JRequest::getWord('controller', 'items');
$path = JPATH_COMPONENT.'/controllers/'.$controller.'.php';

if(file_exists($path))
{
	require_once $path;
}else{
	$controller = '';
}

$classname	= 'VuzController'.$controller;
$controller	= new $classname();

$controller->execute(JRequest::getVar('task'));
$controller->redirect();

Drammm
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Sun Jan 01, 2023 7:34 am

Re: Too few arguments to function Joomla\CMS\Table\Table::__construct(), 1 passed

Post by Drammm » Sun Jan 01, 2023 5:29 pm

In general, I realized the problem in the file /libraries/src/Table/Table.php line 326

Code: Select all

return new $tableClass($db);
as far as I understand it calls a constructor with one parameter, and you need three. For some reason it didn't cause an error in PHP 7.4, but >8 does.
What should I do? Is it possible to solve this problem without changing Joomla core files?

User avatar
pe7er
Joomla! Master
Joomla! Master
Posts: 24986
Joined: Thu Aug 18, 2005 8:55 pm
Location: Nijmegen, Netherlands
Contact:

Re: Too few arguments to function Joomla\CMS\Table\Table::__construct(), 1 passed

Post by pe7er » Mon Jan 02, 2023 11:41 am

Drammm wrote:
Sun Jan 01, 2023 12:24 pm
If I have table #__vuz_items and I write

Code: Select all

$row = &$this->getTable('#__vuz_items', 'Table', []);
then I get an error
You have used the database table name, which is not correct.

Look at how Joomla 3 does it in
/administrator/components/com_content/models/article.php line 294

Code: Select all

public function getTable($type = 'Content', $prefix = 'JTable', $config = array())
{
	return JTable::getInstance($type, $prefix, $config);
}
and how it overrides the default values in
/administrator/components/com_content/models/article.php line 734

Code: Select all

$table = $this->getTable('Featured', 'ContentTable');
And the table class "ContentTableFeatured" in the file
/administrator/components/com_content/tables/featured.php
knows where to get the table from the database.
Kind Regards,
Peter Martin, Global Moderator
Company website: https://db8.nl/en/ - Joomla specialist, Nijmegen, Netherlands
The best website: https://the-best-website.com

Drammm
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Sun Jan 01, 2023 7:34 am

Re: Too few arguments to function Joomla\CMS\Table\Table::__construct(), 1 passed

Post by Drammm » Mon Jan 02, 2023 6:01 pm

Thank you, it looks like you're right!
So, I have in /administrator/components/com_vuz/models/item.php
this is where there was an error with getTable, the class is called VuzModelItem
I made the getTable call like this:

Code: Select all

$row = &$this->getTable('Item', 'VuzModel');
and the request went on to the store method (which is responsible for saving)
And now I got an error

Code: Select all

Call to undefined method VuzModelItem::bind()
/var/www/vuz/administrator/components/com_vuz/models/item.php:236
Here is the full code of the store method

Code: Select all

	function store()
	{	
		$row = &$this->getTable('Item', 'VuzModel');

		$data = JRequest::get('post');

		$fields = JRequest::getVar('fields', '', 'post', 'array', JREQUEST_ALLOWRAW);
        $statistics = JRequest::getVar('statistics', '', 'post', 'array', JREQUEST_ALLOWRAW);
		$data['text'] = JRequest::getVar('text', '', 'post', 'string', JREQUEST_ALLOWRAW);

		if(empty($data['name']))
		{
			$this->setError($this->_db->getErrorMsg());
			return false;
		}else{
			if(empty($data['alias']))
			{
				$data['alias'] = $this->transliterate($data['name']);
			}
		}
		if(!empty($fields))
		{
			$this->setFields($fields);
		}
        if (!empty($statistics)) {
            $this->setStatistics($statistics);
        }
		if(!empty($data['images']))
		{
			$this->setImages($data['images']);
		}
		if(!empty($data['city']))
		{
			$data['city_alias'] = $this->transliterate($data['city']);
		}
		if(!empty($data['hideexam']))
		{
			$data['hideexam'] = 1;
		}else{
			$data['hideexam'] = 0;
		}
		if(empty($data['id']))
		{
			$data['created'] = JHTML::_('date', time(), 'Y-m-d H:M:S');
		}
		$data['modified'] = JHTML::_('date', time(), 'Y-m-d H:M:S');

		if (!$row->bind($data)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		if (!$row->check()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		if (!$row->store()) {
			$this->setError( $row->getErrorMsg() );
			return false;
		}
		return true;
	}

User avatar
jcalvert
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 244
Joined: Sun Feb 19, 2006 10:00 am

Re: Too few arguments to function Joomla\CMS\Table\Table::__construct(), 1 passed

Post by jcalvert » Thu Mar 02, 2023 10:25 am

I'm seeing this error in Joomla 3.10.11 using PHP 8.0.25:
Too few arguments to function Joomla\CMS\Table\Table::__construct(), 1 passed in /var/www/vhosts/XXXXXX.com/httpdocs/libraries/src/Table/Table.php on line 328 and exactly 3 expected
Line 328:

Code: Select all

return new $tableClass($db);
The code in Table.php is only passing 1 argument.

The constuctor in Table.php:

Code: Select all

/**
     * Object constructor to set table and key fields.  In most cases this will
     * be overridden by child classes to explicitly set the table and key fields
     * for a particular database table.
     *
     * @param   string            $table  Name of the table to model.
     * @param   mixed             $key    Name of the primary key field in the table or array of field names that compose the primary key.
     * @param   \JDatabaseDriver  $db     \JDatabaseDriver object.
     *
     * @since   1.7.0
     */
    public function __construct($table, $key, $db)
    {
The constructor for the component calling libraries Table.php:

Code: Select all

/**
   * Constructor
   *
   * @return  void
   * @since   1.5.5
   */
  public function TableJoomGalleryConfig(&$db)
  {
    parent::__construct(_JOOM_TABLE_CONFIG, 'id', $db);
  }

nonoland
Joomla! Apprentice
Joomla! Apprentice
Posts: 14
Joined: Fri Jul 16, 2010 7:28 am

Re: Too few arguments to function Joomla\CMS\Table\Table::__construct(), 1 passed

Post by nonoland » Tue Apr 25, 2023 1:16 pm

Hi everyone,

I experienced the same issue as jcalvert and Drammm.

The root cause of this very annoying issue has nothing to deal with getTable number of parameters. The 3 parameters defined are all optional.

The root cause is coming from the fact that the 1-parameter constructor of the custom table object is not detected. In jcalvert's example, I am talking about this one:

Code: Select all

class TableJoomGalleryConfig extends JTable
{
...
/**
   * Constructor
   *
   * @return  void
   * @since   1.5.5
   */
  public function TableJoomGalleryConfig(&$db)
  {
    parent::__construct(_JOOM_TABLE_CONFIG, 'id', $db);
  }
 }
According to what we can read in this article :
https://www.php.net/manual/en/language.oop5.decon.php
Prior to PHP 8.0.0, classes in the global namespace will interpret a method named the same as the class as an old-style constructor. That syntax is deprecated,...
Thus, with php 8.x, your constructor, jcalvert, must be written like this:

Code: Select all

/**
   * Constructor
   *
   * @return  void
   * @since   1.5.5
   */
  public function __construct(&$db)
  {
    parent::__construct(_JOOM_TABLE_CONFIG, 'id', $db);
  }

User avatar
jcalvert
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 244
Joined: Sun Feb 19, 2006 10:00 am

Re: Too few arguments to function Joomla\CMS\Table\Table::__construct(), 1 passed

Post by jcalvert » Tue Apr 25, 2023 11:32 pm

Much thanks, nonoland, this was the solution I suspected but didn't know how to accomplish. I'll give it a try.


Locked

Return to “Joomla! 3.x Coding”