Skip to content

Instantly share code, notes, and snippets.

@cwage
Created September 13, 2010 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cwage/577475 to your computer and use it in GitHub Desktop.
Save cwage/577475 to your computer and use it in GitHub Desktop.
<?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