Created
September 13, 2010 15:38
-
-
Save cwage/577475 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This class has been auto-generated by the Doctrine ORM Framework | |
*/ | |
class AddUniqueVendorIndex extends Doctrine_Migration | |
{ | |
public function up() | |
{ | |
$sql = "select count(*) as count, name, company from vendor group by name, company having count >1;"; | |
$conn = Doctrine_Manager::connection(); | |
try | |
{ | |
$result = $conn->execute($sql); | |
} | |
catch( Exception $e ) | |
{ | |
throw new Exception( $e->getMessage() ); | |
} | |
$vendors = $result->fetchAll(); | |
foreach ($vendors as $vendor) | |
{ | |
$res = Doctrine::getTable('Vendor')->createQuery('v') | |
->addWhere("v.name = ?", $vendor['name']) | |
->addWhere("v.company = ?", $vendor['company']) | |
->addOrderBy("v.created_at") | |
->limit($vendor['count'] - 1) | |
->execute(); | |
foreach ($res as $ven) | |
{ | |
$ven->delete(); | |
} | |
} | |
exit; | |
$this->addIndex('vendor', 'name_company', array('type' => 'unique', 'fields' => array("name", "company"))); | |
} | |
public function down() | |
{ | |
$this->removeIndex('vendor', 'name_company'); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment