2011年2月22日星期二

Re: [fw-db] Zend_Db_Table Issue

Il 22/02/2011 0.44, Bill Karwin ha scritto:
> I coded the cascading functionality in Zend_Db_Table. I don't
> recommend using it for any RDBMS, whether the RDBMS supports DRI or not.
How can a RDBMS cascading delete unlink files on the filesystem (eg.
images of a product) ?
in some case you should consider to implement your own cascading
operation in your models (imho)
eg.

$depTables = $this->getTable()->getDependentTables();
if (!empty($depTables))
{
/**
* Foreach dependent table instantiate the model
* and call ->delete() on each of them
*/
foreach($depTables as $tableName)
{
$modelName = str_replace('DbTable_', '', $tableName);
$depModel = new $modelName();
$depTable = $depModel->getTable();
$reference =
$depTable->getReference(get_class($table));

switch ($reference['onDelete'])
{
case Zend_Db_Table::CASCADE:

if(count($depTable->info(Zend_Db_Table::PRIMARY)) > 1)
{
$depTable->delete(

$depTable->getAdapter()->quoteInto($reference['columns'][0] . ' = ?', $id)
);
break;
}
$select = $depTable->getAdapter()
->select()
->distinct()

->from($depTable->info(Zend_Db_Table::NAME),
array($reference['refColumns'][0]))

->where($reference['columns'][0] . ' = ?', $id);

$toBeDeleted =
$depTable->getAdapter()->fetchCol($select);

foreach($toBeDeleted as $idToDelete)
$depModel->delete($idToDelete);

break;

case Zend_Db_Table::RESTRICT:
$select = $depTable->getAdapter()
->select()
->distinct()

->from($depTable->info(Zend_Db_Table::NAME),
array($reference['refColumns'][0]))

->where($reference['columns'][0] . ' = ?', $id);

$toBeDeleted =
$depTable->getAdapter()->fetchCol($select);

if (count($toBeDeleted) > 0)
throw new Planet_Exception('Can\'t
delete restricted reference in '.get_class($depTable));

break;

default:
continue;
}
}
}
$n += $row->delete();
$this->notify(__METHOD__, array($primaryKey => $id));

--
________________________________________
Simone Cosci - NextIdea
Software engineering
Planet S.r.l. Sistemi informatici
Via Giorgio Ambrosoli, 39
50018, Scandicci, Firenze, Italy
WEB http://www.planetweb.it
E-Mail s.cosci@nextidea.it
Tel. +39 055 7350271 / 4630416
Fax. +39 055 7351109
Cell. +39 339 8672146
Voip. +39 055 5357896
________________________________________

没有评论: