Skip to content

Commit

Permalink
New - PHP: Editor->readTable() method which can be used to tell the…
Browse files Browse the repository at this point in the history
… instance to read information from a different table (or more typically a VIEW). This allows complex select expressions to be created in the database, and Editor can read them, while writing updates to the data to the original table.

- DD-65
  • Loading branch information
Allan Jardine committed Sep 19, 2018
1 parent 8d6cbde commit 0942e2a
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions Editor.php
Expand Up @@ -175,6 +175,9 @@ function __construct( $db=null, $table=null, $pkey=null )
/** @var string[] */
private $_table = array();

/** @var string[] */
private $_readTableNames = array();

/** @var boolean */
private $_transaction = true;

Expand Down Expand Up @@ -707,6 +710,25 @@ public function process ( $data )
}


/**
* The CRUD read table name. If this method is used, Editor will create from the
* table name(s) given rather than those given by `Editor->table()`. This can be
* a useful distinction to allow a read from a VIEW (which could make use of a
* complex SELECT) while writing to a different table.
*
* @param string|array $_,... Read table names given as a single string, an array
* of strings or multiple string parameters for the function.
* @return string[]|self Array of read tables names, or self if used as a setter.
*/
public function readTable ( $_=null )
{
if ( $_ !== null && !is_array($_) ) {
$_ = func_get_args();
}
return $this->_getSet( $this->_readTableNames, $_, true );
}


/**
* Get / set the table name.
*
Expand Down Expand Up @@ -1032,7 +1054,7 @@ private function _get( $id=null, $http=null )

$query = $this->_db
->query('select')
->table( $this->_table )
->table( $this->_read_table() )
->get( $this->_pkey );

// Add all fields that we need to get from the database
Expand Down Expand Up @@ -1481,7 +1503,7 @@ private function _ssp_query ( $query, $http )
// Get the number of rows in the result set
$ssp_set_count = $this->_db
->query('select')
->table( $this->_table )
->table( $this->_read_table() )
->get( 'COUNT('.$this->_pkey[0].') as cnt' );
$this->_get_where( $ssp_set_count );
$this->_ssp_filter( $ssp_set_count, $http );
Expand All @@ -1491,7 +1513,7 @@ private function _ssp_query ( $query, $http )
// Get the number of rows in the full set
$ssp_full_count = $this->_db
->query('select')
->table( $this->_table )
->table( $this->_read_table() )
->get( 'COUNT('.$this->_pkey[0].') as cnt' );
$this->_get_where( $ssp_full_count );
if ( count( $this->_where ) ) { // only needed if there is a where condition
Expand Down Expand Up @@ -2140,5 +2162,12 @@ private function _pkey_separator ()

return hash( 'crc32', $str );
}

private function _read_table ()
{
return count($this->_readTableNames) ?
$this->_readTableNames :
$this->_table;
}
}

0 comments on commit 0942e2a

Please sign in to comment.