Back to Table class

Method __construct

public
__construct
(mixed $table, mixed $key, \Joomla\Database\DatabaseDriver $db, \Joomla\Event\DispatcherInterface $dispatcher = null)
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.
Parameters
  • string $table Name of the table to model.
  • mixed $key Name of the primary key field in the table or array of field names that compose the primary key.
  • \Joomla\Database\DatabaseDriver $db DatabaseDriver object.
  • \Joomla\Event\DispatcherInterface $dispatcher Event dispatcher for this table
Since
  • 1.7.0
Class: Table
Project: Joomla

Method __construct - Source code

/**
 * 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   DatabaseDriver       $db          DatabaseDriver object.
 * @param   DispatcherInterface  $dispatcher  Event dispatcher for this table
 *
 * @since   1.7.0
 */
public function __construct($table, $key, DatabaseDriver $db, DispatcherInterface $dispatcher = null)
{
    parent::__construct();
    // Set internal variables.
    $this->_tbl = $table;
    // Set the key to be an array.
    if (\is_string($key)) {
        $key = array($key);
    } elseif (\is_object($key)) {
        $key = (array) $key;
    }
    $this->_tbl_keys = $key;
    if (\count($key) == 1) {
        $this->_autoincrement = true;
    } else {
        $this->_autoincrement = false;
    }
    // Set the singular table key for backwards compatibility.
    $this->_tbl_key = $this->getKeyName();
    $this->_db = $db;
    // Initialise the table properties.
    $fields = $this->getFields();
    if ($fields) {
        foreach ($fields as $name => $v) {
            // Add the field if it is not already present.
            if (!$this->hasField($name)) {
                $this->{$name} = null;
            }
        }
    }
    // If we are tracking assets, make sure an access field exists and initially set the default.
    if ($this->hasField('asset_id')) {
        $this->_trackAssets = true;
    }
    // If the access property exists, set the default.
    if ($this->hasField('access')) {
        $this->access = (int) Factory::getApplication()->get('access');
    }
    // Create or set a Dispatcher
    if (!\is_object($dispatcher) || !$dispatcher instanceof DispatcherInterface) {
        // @todo Maybe we should use a dedicated "behaviour" dispatcher for performance reasons and to prevent system plugins from butting in?
        $dispatcher = Factory::getApplication()->getDispatcher();
    }
    $this->setDispatcher($dispatcher);
    $event = AbstractEvent::create('onTableObjectCreate', ['subject' => $this]);
    $this->getDispatcher()->dispatch('onTableObjectCreate', $event);
}