Source for file Parser.php
Documentation is available at Parser.php
* $Id: Table.php 1397 2007-05-19 19:54:15Z zYne $
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
* Doctrine_Relation_Parser
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision: 1397 $
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @var Doctrine_Table $_table the table object this parser belongs to
* @var array $_relations an array containing all the Doctrine_Relation objects for this table
* @var array $_pending relations waiting for parsing
* @param Doctrine_Table $table the table object this parser belongs to
* @return Doctrine_Table the table object this parser belongs to
* @return array an array defining a pending relation
public function bind($name, $options =
array())
if (isset
($this->relations[$name])) {
unset
($this->relations[$name]);
$alias = isset
($e[1]) ?
$e[1] :
$name;
if ( ! isset
($options['type'])) {
if (isset
($options['onDelete'])) {
if (isset
($options['onUpdate'])) {
* @param string $alias relation alias
// check if reference class name exists
// if it does we are dealing with association relation
if (isset
($def['refClass'])) {
if ( ! isset
($this->_pending[$def['refClass']]) &&
$parser =
$def['refTable']->getRelationParser();
'local' =>
$def['local'],
'foreign' =>
$def['local'],
if (in_array($def['class'], $localClasses)) {
// simple foreign key relation
if (isset
($def['localKey'])) {
// unset pending relation
* returns an array containing all relation objects
* @return array an array of Doctrine_Relation objects
* returns the table class of the concrete implementation for given template
* if the given template is not a template then this method just returns the
* table class for the given record
* @param string $template
$impl =
$this->_table->getImpl($template);
return $conn->getTable($impl);
* Completes the given association definition
* @param array $def definition array to be completed
* @return array completed definition array
$def['table'] =
$this->getImpl($def['class']);
$def['class'] =
$def['table']->getComponentName();
$def['refTable'] =
$this->getImpl($def['refClass']);
$id =
$def['refTable']->getIdentifier();
if ( ! isset
($def['foreign'])) {
// try to guess the foreign key
$def['foreign'] =
($def['local'] ===
$id[0]) ?
$id[1] :
$id[0];
if ( ! isset
($def['local'])) {
// try to guess the foreign key
$def['local'] =
($def['foreign'] ===
$id[0]) ?
$id[1] :
$id[0];
if ( ! isset
($def['foreign'])) {
// try to guess the foreign key
$def['foreign'] =
$columns;
if ( ! isset
($def['local'])) {
// try to guess the local key
$def['local'] =
$columns;
* gives a list of identifiers from given table
* the identifiers are in format:
* [componentName].[identifier]
* @param Doctrine_Table $table table object to retrieve identifiers from
if (is_array($table->getIdentifier())) {
foreach((array)
$table->getIdentifier() as $identifier) {
$columns[] =
strtolower($table->getComponentName())
.
'_' .
$table->getIdentifier();
.
'_' .
$table->getIdentifier();
* @param array $classes an array of class names
* @param Doctrine_Table $foreignTable foreign table object
* @return array an array of column names
public function guessColumns(array $classes, Doctrine_Table $foreignTable)
$conn =
$this->_table->getConnection();
foreach ($classes as $class) {
$table =
$conn->getTable($class);
foreach ((array)
$columns as $column) {
if ( ! $foreignTable->hasColumn($column)) {
* Completes the given definition
* @param array $def definition array to be completed
* @return array completed definition array
$conn =
$this->_table->getConnection();
$def['table'] =
$this->getImpl($def['class']);
$def['class'] =
$def['table']->getComponentName();
$foreignClasses =
array_merge($def['table']->getOption('parents'), array($def['class']));
if (isset
($def['local'])) {
if ( ! isset
($def['foreign'])) {
// local key is set, but foreign key is not
// try to guess the foreign key
if ($def['local'] ===
$this->_table->getIdentifier()) {
$def['foreign'] =
$this->guessColumns($localClasses, $def['table']);
// the foreign field is likely to be the
// identifier of the foreign class
$def['foreign'] =
$def['table']->getIdentifier();
if ($def['local'] !==
$this->_table->getIdentifier() &&
if (isset
($def['foreign'])) {
// local key not set, but foreign key is set
// try to guess the local key
if ($def['foreign'] ===
$def['table']->getIdentifier()) {
$def['local'] =
$this->_table->getIdentifier();
$def['local'] =
$this->_table->getIdentifier();
// neither local or foreign key is being set
// try to guess both keys
$conn =
$this->_table->getConnection();
// the following loops are needed for covering inheritance
foreach ($localClasses as $class) {
.
'_' .
$table->getIdentifier();
foreach ($foreignClasses as $class2) {
if ($table2->hasColumn($column)) {
$def['foreign'] =
$column;
$def['local'] =
$table->getIdentifier();
foreach ($foreignClasses as $class) {
.
'_' .
$table->getIdentifier();
foreach ($localClasses as $class2) {
if ($table2->hasColumn($column)) {
$def['foreign'] =
$table->getIdentifier();
// auto-add columns and auto-build relation
foreach ((array)
$this->_table->getIdentifier() as $id) {
$col =
$this->_table->getDefinitionOf($id);
$length =
$col['length'];
unset
($col['autoincrement']);
$def['table']->setColumn($column, $type, $length, $col);
if (count($columns) >
1) {
$def['foreign'] =
$columns;
$def['foreign'] =
$columns[0];
$def['local'] =
$this->_table->getIdentifier();