简体   繁体   中英

How to link three MySQL tables using Zend?



I have a problem.

I have three tables in MySQL, one contains tags , one contains posts and the third one links tags and posts using ids.

When I add a post that contains tags, I insert the tags and then I make the links between tables. Now I have two parent tables (tags and posts) and one child that links them.

My objective : when I delete a post, I want to delete all the links between tags and posts and I also want to remove the tags that are not linked with other posts but depended on the post that I deleted.

Here are my tables. I know that they are not linked well. I though that if I make posts parent for posts_tags and posts_tags parent for tags would solve my problem but in case of detele on cascade from posts, tags will also be deleted. I want them deleted only if there are no links between them and other posts.

I forgot to mention that I encountered this problem using zend framework and I still need to solve it winth 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    Default    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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM