Its not extendable I have to copy over _join because of type checks
and another one as its private, i doubt it was ever thought out to be
extended ?
require_once 'Zend/Db/Select.php';
class Zend_Db_Select_Extended extends Zend_Db_Select
{
const NATURAL_LEFT_JOIN = 'natural left join';
public function __construct(Zend_Db_Adapter_Abstract $adapter)
{
parent::__construct($adapter);
}
/**
* Add a NATURAL LEFT JOIN table and colums to the query.
* A natural join assumes an equi-join across any column(s)
* that appear with the same name in both tables.
* Only natural inner joins are supported by this API,
* even though SQL permits natural outer joins as well.
*
* The $name and $cols parameters follow the same logic
* as described in the from() method.
*
* @param array|string|Zend_Db_Expr $name The table name.
* @param array|string $cols The columns to select from the
joined table.
* @param string $schema The database name to specify, if any.
* @return Zend_Db_Select This Zend_Db_Select object.
*/
public function joinLeftNatural($name, $cols = '*', $schema = null)
{
return $this->_join(self::NATURAL_LEFT_JOIN, $name, null,
$cols, $schema);
}
/**
* Populate the {@link $_parts} 'join' key
*
* Does the dirty work of populating the join key.
*
* The $name and $cols parameters follow the same logic
* as described in the from() method.
*
* @param null|string $type Type of join; inner, left, and null
are currently supported
* @param array|string|Zend_Db_Expr $name Table name
* @param string $cond Join on this condition
* @param array|string $cols The columns to select from the
joined table
* @param string $schema The database name to specify, if any.
* @return Zend_Db_Select This Zend_Db_Select object
* @throws Zend_Db_Select_Exception
*/
protected function _join($type, $name, $cond, $cols, $schema =
null)
{
$joinTypes = array(self::INNER_JOIN, self::LEFT_JOIN,
self::RIGHT_JOIN, self::FULL_JOIN, self::CROSS_JOIN,
self::NATURAL_JOIN, self::NATURAL_LEFT_JOIN);
if (!in_array($type, $joinTypes)) {
/**
* @see Zend_Db_Select_Exception
*/
require_once 'Zend/Db/Select/Exception.php';
throw new Zend_Db_Select_Exception("Invalid join type
'$type'");
}
if (empty($name)) {
$correlationName = $tableName = '';
} else if (is_array($name)) {
// Must be array($correlationName => $tableName) or array
($ident, ...)
foreach ($name as $_correlationName => $_tableName) {
if (is_string($_correlationName)) {
// We assume the key is the correlation name and
value is the table name
$tableName = $_tableName;
$correlationName = $_correlationName;
} else {
// We assume just an array of identifiers, with
no correlation name
$tableName = $name;
$correlationName = $this->_uniqueCorrelation
($tableName);
}
break;
}
} else if ($name instanceof Zend_Db_Expr) {
$tableName = $name;
$correlationName = $this->_uniqueCorrelation('t');
} else if (preg_match('/^(.+)\s+AS\s+(.+)$/i', $name, $m)) {
$tableName = $m[1];
$correlationName = $m[2];
} else {
$tableName = $name;
$correlationName = $this->_uniqueCorrelation($tableName);
}
// Schema from table name overrides schema argument
if (false !== strpos($tableName, '.')) {
list($schema, $tableName) = explode('.', $tableName);
}
if (!empty($correlationName)) {
if (array_key_exists($correlationName, $this->_parts
[self::FROM])) {
/**
* @see Zend_Db_Select_Exception
*/
require_once 'Zend/Db/Select/Exception.php';
throw new Zend_Db_Select_Exception("You cannot
define a correlation name '$correlationName' more than once");
}
$this->_parts[self::FROM][$correlationName] = array(
'joinType' => $type,
'schema' => $schema,
'tableName' => $tableName,
'joinCondition' => $cond
);
}
// add to the columns from this joined table
$this->_tableCols($correlationName, $cols);
return $this;
}
/**
* Generate a unique correlation name
*
* @param string|array $name A qualified identifier.
* @return string A unique correlation name.
*/
private function _uniqueCorrelation($name)
{
if (is_array($name)) {
$c = end($name);
} else {
// Extract just the last name of a qualified table name
$dot = strrpos($name,'.');
$c = ($dot === false) ? $name : substr($name, $dot+1);
}
for ($i = 2; array_key_exists($c, $this->_parts
[self::FROM]); ++$i) {
$c = $name . '_' . (string) $i;
}
return $c;
}
}
Post by Jack SleightI don't know why it's not included I'm afraid, perhaps someone from
the DB team could let us know? You could create a new issue in JIRA
for this to be added.
There is no built in way to change the object returned by
Zend_Db_Adapter::select no, as it's hard coded. You can either
extend the adapter as well, and override the select method, or just
create the select objects without the helper method, i.e.
$select = new My_Db_Select($adapter);
Post by Daniel RossiI just added this to zend_db_select, it was easy to add, not sure
why only half of the things are supported ?
const NATURAL_LEFT_JOIN = 'natural left join';
public function joinLeftNatural($name, $cols = '*', $schema = null)
{
return $this->_join(self::NATURAL_LEFT_JOIN, $name, null,
$cols, $schema);
}
I dont really like to break or add things but oh well, maybe I
could extend it ?
--
Jack