1

Suppose, I've two tables with following column:

#__table1 - id, col1, col2
#__table2 - id, col3, col4

I've created JTable class in tables folder of my component:

class ComponentNameTableTable1 extends JTable
{
    function __construct(&$db)
    {
        parent::__construct('#__table1', 'id', $db);
    }
}

In the store method of JTable, I need to access col3 & col4

public function store($updateNulls = false)
{
     $col1 = $this->col1; // This is happening
     $col2 = $this->col2; // This is not happening
     $col3 = $this->col3; // This is not happening
     $col4 = $this->col4; // This is not happening
     return parent::store($updateNulls);
}

Please note that, I've passed the data from my Model to JTable as follows:

public function save($data)
{
    // $data here is an array of containing following value:
    // $data = array('col1' => 'somevalue1', 'col2' => 'somevalue2', 'col3' => 'somevalue3', 'col4' => 'somevalue4');

    // It is then passed to the JTable with following:

    $table = $this->getTable();
    $key   = $table->getKeyName();

    // Get Primary Key Value
    $pk = (!empty($data[$key]) ? $data[$key] : (int) $this->getState($this->getName() . '.id'));

    $isNew = true;

    try
    {
        if ($pk > 0)
        {
            $table->load($pk);
            $isNew = false;
        }

        // Bind the data.
        $table->bind($data);

        // Check the data.
        if (!$table->check())
        {
            return false;
        }

        // Store the data.
        if (!$table->store())
        {
            return false;
        }

        // Clean the cache.
        $this->cleanCache();
    }
    catch (Exception $ex)
    {
        throw new RuntimeException('There was an error while saving data');
    }

    $this->setState($this->getName() . '.id', $table->$key);
    $this->setState($this->getName() . '.new', $isNew);

    return true;
}

My Research

It seems that, JTable execute some function which further filters $data and removes keys which are not present in #__table1. If anyone can atleast get me to that table then, I can override it further. I tried to look at bind() method. But it doesn't seems to be filtering that $data as I tried printing the out of $data before bind() get's executed in JTable function.

1

From my understanding you should be using multiple JTable instances in your model when you have multiple tables.

Take a look at these models in the CMS they might give you some direction for how to set up your model.

https://github.com/joomla/joomla-cms/blob/staging/administrator/components/com_installer/models/manage.php

https://github.com/joomla/joomla-cms/blob/staging/administrator/components/com_contenthistory/models/compare.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.