How to link three MySQL tables using Zend?
posts Field Type Null Default Comments id int(11) No title varchar(200) No description longtext No address varchar(100) No added datetime No post_tag Field Type Null Defau开发者_开发知识库lt Links to Comments id_tag int(11) No 0 etichete -> id id_post int(11) No 0 turism -> id tags Field Type Null Default Comments id int(11) No name varchar(50) No
Thanks
you need more powerfull DB abstraction layer. look at http://www.doctrine-project.org/ for example.
Zend_Db is really good. my cli/cron scripts are often uses it as secondary db layer, but if you need more, u have to use other tools or write(construct) sql by hands. it is possible to do with zend_db - 3 or more queries... but...
have you tried to use protected $_dependentTables and protected $_referenceMap ? here is the link http://framework.zend.com/manual/en/zend.db.table.relationships.html and here is an example
class Model_DettagliOrdine extends Gestionale_Model_Abstract_Crud
{
protected $_name = 'dettagli_ordine';//zend by default assumes table name = class names, we ovveride it
protected $_primary = 'id';//primary key
protected $_dependentTables = array('Model_DettagliScontoMerce');//per far si che se cancelliamo qui si cancella anche dall'altra tabella
protected $_referenceMap = array(
// rule name
'Ordine' => array(
'columns' => 'ordine_id', // this column
'refTableClass' => 'Model_Ordine', // references that table object
'refColumns' => 'id', // and references that column
'onDelete' => self::RESTRICT,
'onUpdate' => self::CASCADE
),
'Prodotto' => array(
'columns' => 'prodotto_id', // this column
'refTableClass' => 'Model_Prodotto', // references that table object
'refColumns' => 'id', // and references that column
'onDelete' => self::RESTRICT,
'onUpdate' => self::CASCADE
)
);
....
for the delete stuff, you're gonna need a custom delete function with something like this
// find the row that matches the id
$current = $this->find($id)->current();
if (empty($current) || !is_a($current, 'Zend_Db_Table_Row_Abstract'))
{
throw new Exception("errore cancellazione riga non trovata");//da tradurre in view o in controller
}
//controlla che non ci sono dipendenze
$dependent = $this->_dependentTables;
foreach($dependent as $used_by)
{
$depends_on = $this->find($id)->current()->findDependentRowset($used_by)->toArray();
if (!empty($depends_on))
{
//$view = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('view');
//$errore[] = $view->translate('la riga che si vuole cancellare è in uso da un altra tabella', $used_by);
//throw new Exception($errore);
throw new Exception("la riga che si vuole cancellare è in uso da un altra tabella");
}
}
// if (!empty($errore))
// throw new Exception(implode("<br \>\n", $errore));
$ok = $current->delete();
精彩评论