Permalink
Browse files

Merge remote branch 'estahn/DC-825'

  • Loading branch information...
jwage committed Aug 31, 2010
2 parents 826bc90 + 5691b11 commit bd40ec8de481ea38de2433c5ff31253d66b9bde0
Showing with 109 additions and 4 deletions.
  1. +12 −0 lib/Doctrine/Import/Builder.php
  2. +4 −4 lib/Doctrine/Record/Generator.php
  3. +5 −0 lib/Doctrine/Table.php
  4. +88 −0 tests/Ticket/DC825TestCase.php
@@ -530,6 +530,18 @@ public function buildColumns(array $columns)
$build = null;
foreach ($columns as $name => $column) {
+ // An alias cannot passed via column name and column alias definition
+ if (isset($column['name']) && stripos($column['name'], ' as ') && isset($column['alias'])) {
+ throw new Doctrine_Import_Exception(
+ sprintf('When using a column alias you cannot pass it via column name and column alias definition (column: %s).', $column['name'])
+ );
+ }
+
+ // Update column name if an alias is provided
+ if (isset($column['alias']) && !isset($column['name'])) {
+ $column['name'] = $name . ' as ' . $column['alias'];
+ }
+
$columnName = isset($column['name']) ? $column['name']:$name;
if ($manager->getAttribute(Doctrine_Core::ATTR_AUTO_ACCESSOR_OVERRIDE)) {
$e = explode(' as ', $columnName);
@@ -280,14 +280,14 @@ public function buildForeignKeys(Doctrine_Table $table)
{
$fk = array();
- foreach ((array) $table->getIdentifier() as $column) {
- $def = $table->getDefinitionOf($column);
+ foreach ((array) $table->getIdentifier() as $field) {
+ $def = $table->getDefinitionOf($field);
unset($def['autoincrement']);
unset($def['sequence']);
unset($def['primary']);
- $col = $column;
+ $col = $table->hasColumn($field) ? $field : $table->getColumnName($field) . ' as ' . $field;
$def['primary'] = true;
$fk[$col] = $def;
@@ -431,7 +431,7 @@ public function generateClassFromTable(Doctrine_Table $table)
$definition['columns'] = $table->getColumns();
$definition['tableName'] = $table->getTableName();
$definition['actAs'] = $table->getTemplates();
-
+
return $this->generateClass($definition);
}
View
@@ -1374,6 +1374,10 @@ public function setColumn($name, $type = null, $length = null, $options = array(
$options['type'] = $type;
$options['length'] = $length;
+
+ if (strtolower($fieldName) != $name) {
+ $options['alias'] = $fieldName;
+ }
foreach ($defaultOptions as $key => $value) {
if ( ! array_key_exists($key, $options) || is_null($options[$key])) {
@@ -2620,6 +2624,7 @@ public function getFieldValidators($fieldName)
|| $name == 'length'
|| $name == 'fixed'
|| $name == 'comment'
+ || $name == 'alias'
|| $name == 'extra') {
continue;
}
@@ -0,0 +1,88 @@
+<?php
+/*
+ * $Id$
+ *
+ * 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.doctrine-project.org>.
+ */
+
+/**
+ * Doctrine_Ticket_DC825_TestCase
+ *
+ * @package Doctrine
+ * @author Enrico Stahn <mail@enricostahn.com>
+ * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
+ * @category Object Relational Mapping
+ * @link www.doctrine-project.org
+ * @since 1.0
+ * @version $Revision$
+ */
+class Doctrine_Ticket_DC825_TestCase extends Doctrine_UnitTestCase
+{
+ public function prepareTables()
+ {
+ $this->tables[] = 'Ticket_DC825_Model';
+ parent::prepareTables();
+ }
+
+ public function testTest()
+ {
+ Doctrine_Manager::getInstance()->setAttribute(Doctrine_Core::ATTR_USE_DQL_CALLBACKS, true);
+
+ $user = new Ticket_DC825_Model();
+ $user->username = 'jwage';
+ $user->password = 'changeme';
+ $user->save();
+ $user->delete();
+
+ $version = $user->getAuditLog()->getVersion($user, 2, Doctrine_Core::HYDRATE_RECORD);
+ $versionTable = $version[0]->getTable();
+ $versionTableColumns = $versionTable->getColumnNames();
+ $recordTableColumns = $user->getTable()->getColumnNames();
+
+ $this->assertFalse(in_array('id', $versionTableColumns));
+ $this->assertTrue(in_array('model_id', $versionTableColumns));
+ $this->assertTrue(count($versionTableColumns) == count($recordTableColumns));
+ $this->assertTrue(count(array_diff($versionTableColumns, $recordTableColumns)) == 0);
+
+ Doctrine_Manager::getInstance()->setAttribute(Doctrine_Core::ATTR_USE_DQL_CALLBACKS, false);
+ }
+}
+
+class Ticket_DC825_Model extends Doctrine_Record
+{
+ public function setTableDefinition()
+ {
+ $this->hasColumn('model_id as id', 'integer', null, array(
+ 'type' => 'integer',
+ 'unsigned' => false,
+ 'primary' => true,
+ 'autoincrement' => true,
+ ));
+ $this->hasColumn('username', 'string', 255);
+ $this->hasColumn('password', 'string', 255);
+ }
+
+ public function setUp()
+ {
+ $this->actAs('Timestampable');
+ $this->actAs('SoftDelete');
+ $this->actAs('Versionable', array(
+ 'auditLog' => true,
+ 'generateRelations' => false,
+ 'deleteVersions' => false));
+ }
+}

0 comments on commit bd40ec8

Please sign in to comment.