/**
  * Method to set the publishing state for a row or list of rows in the database
  * table.  The method respects checked out rows by other users and will attempt
  * to checkin rows that it can after adjustments are made.
  *
  * @param   mixed    $pks     An optional array of primary key values to update.
  *                            If not set the instance property value is used.
  * @param   integer  $state   The publishing state. eg. [0 = unpublished, 1 = published]
  * @param   integer  $userId  The user id of the user performing the operation.
  *
  * @return  boolean  True on success; false if $pks is empty.
  *
  * @link    http://docs.joomla.org/JTable/publish
  * @since   11.1
  */
 public function publish($pks = null, $state = 1, $userId = 0)
 {
     $k = $this->_tbl_keys;
     if (!is_null($pks)) {
         foreach ($pks as $key => $pk) {
             if (!is_array($pk)) {
                 $pks[$key] = array($this->_tbl_key => $pk);
             }
         }
     }
     $userId = (int) $userId;
     $state = (int) $state;
     // If there are no primary keys set check to see if the instance key is set.
     if (empty($pks)) {
         $pk = array();
         foreach ($this->_tbl_keys as $key) {
             if ($this->{$key}) {
                 $pk[$this->{$key}] = $this->{$key};
             } else {
                 return false;
             }
         }
         $pks = array($pk);
     }
     foreach ($pks as $pk) {
         // Update the publishing state for rows with the given primary keys.
         $query = $this->_db->getQuery(true);
         $query->update($this->_tbl);
         $query->set('published = ' . (int) $state);
         // Determine if there is checkin support for the table.
         if (property_exists($this, 'checked_out') || property_exists($this, 'checked_out_time')) {
             $query->where('(checked_out = 0 OR checked_out = ' . (int) $userId . ')');
             $checkin = true;
         } else {
             $checkin = false;
         }
         // Build the WHERE clause for the primary keys.
         $this->appendPrimaryKeys($query, $pk);
         $this->_db->setQuery($query);
         $this->_db->execute();
         // If checkin is supported and all rows were adjusted, check them in.
         if ($checkin && count($pks) == $this->_db->getAffectedRows()) {
             $this->checkin($pk);
         }
         $ours = true;
         foreach ($this->_tbl_keys as $key) {
             if ($this->{$key} != $pk[$key]) {
                 $ours = false;
             }
         }
         if ($ours) {
             $this->published = $state;
         }
     }
     $this->setError('');
     return true;
 }
 /**
  * Sets the SQL statement string for later execution.
  *
  * @param   mixed    $query          The SQL statement to set either as a JDatabaseQuery object or a string.
  * @param   integer  $offset         The affected row offset to set.
  * @param   integer  $limit          The maximum affected rows to set.
  * @param   array    $driverOptions  The optional PDO driver options
  *
  * @return  JDatabaseDriver  This object to support method chaining.
  *
  * @since   12.1
  */
 public function setQuery($query, $offset = null, $limit = null, $driverOptions = array())
 {
     $this->connect();
     $this->freeResult();
     if (is_string($query)) {
         // Allows taking advantage of bound variables in a direct query:
         $query = $this->getQuery(true)->setQuery($query);
     }
     if ($query instanceof Limitable && !is_null($offset) && !is_null($limit)) {
         $query->setLimit($limit, $offset);
     }
     $sql = $this->replacePrefix((string) $query);
     $this->prepared = $this->connection->prepare($sql, $driverOptions);
     // Store reference to the JDatabaseQuery instance:
     parent::setQuery($query, $offset, $limit);
     return $this;
 }