Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4619038
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:22:12+00:00 2026-05-22T02:22:12+00:00

I am going to attempt to keep this as simple as possible, but the

  • 0

I am going to attempt to keep this as simple as possible, but the use case is outside the original intention of Zend_Db I fear. It concerns a set of tables I have for tagging pages (or anything else eg. documents) in a CMS.

I have three tables:

  1. Pages (pages)
  2. Tags (tags)
  3. TagLink (tags_link) which is a many-to-many linking table between Pages and Tags

Pages is a simple table (I have removed the inconsequential columns from the code below):

CREATE TABLE `pages` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  FULLTEXT KEY `search` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8

Tags is quite simple as well although there is a self-referential column (parent_tag_id):

CREATE TABLE `tags` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `tag` varchar(255) NOT NULL,
  `parent_tag_id` int(11) NOT NULL DEFAULT '0',
  `updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `GetByParentTagId` (`parent_tag_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8

TagLink is again fairly simple:

CREATE TABLE `tags_link` (
  `tag_id` int(11) NOT NULL,
  `module_type` varchar(50) NOT NULL,
  `foreign_key` int(11) NOT NULL,
  UNIQUE KEY `Unique` (`tag_id`,`module_type`,`foreign_key`),
  KEY `Search` (`module_type`,`foreign_key`),
  KEY `AllByTagId` (`tag_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8

The complicating factor is that TagLink is able to link against any other table in the database and not just Pages. So if for example I had a documents upload section then that could also be tagged. To facilitate this way of working there is effectively a multi-column key.

To make this clearer I will demonstrate a couple of insert queries that might be run when tags are added to a table (eg. Pages):

INSERT INTO `tags_link`
SET `tag_id` = '1',
    `module_type` = 'Pages',
    `foreign_key` = '2'

INSERT INTO `tags_link`
SET `tag_id` = '1',
    `module_type` = 'Documents',
    `foreign_key` = '3'

So as you can see the module_type column is simply an arbitrary string that describes where the foreign key can be found. This is not the name of the table however as anything with an ID can have tags linked to it even if it is not necessarily in the MySQL database.

Now to the Zend_Db_Table $_referenceMap in PageTable:

protected $_referenceMap = array(
        'TagLink' => array(
            'columns' => 'id',
            'refTableClass' => 'Models_Tag_TagLinkTable',
            'refColumns' => 'foreign_key'
        ),
    );

But this does not take into account my arbitrary module_type column and will return any TagLink with the same foreign key. Obviously this is bad because you get TagLinks for documents mixed in with those for pages for instance.

So my question is how can I take into account this additional column when setting up this reference? The aim is to avoid having a TagLink class for each module_type as I have now.

I would imagine something like the following could explain my requirements although obviously this is not how it would be done:

protected $_referenceMap = array(
        'TagLink' => array(
            'columns' => 'id',
            'refTableClass' => 'Models_Tag_TagLinkTable',
            'refColumns' => 'foreign_key',
            'where' => 'module_type = "Pages"'
        ),
    );

My current implementation overrides the _fetch method in the Documents_TagLinkTable in the following way:

protected function _fetch(Zend_Db_Table_Select $select) {
    $select->where("module_type = 'Documents_Secondary_Tags' OR module_type = 'Documents_Primary_Tags' OR module_type = 'Documents'");
    return parent::_fetch($select);
}

As you can see there maybe more than one set of tags added to any object as well.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:22:12+00:00Added an answer on May 22, 2026 at 2:22 am

    Example 3 in “Fetching Dependent Rowsets” in the Zend Framework reference demonstrates a technique you could use:

    http://framework.zend.com/manual/en/zend.db.table.relationships.html

    Whilst it doesnt show a “where” clause being included in the select, it should work.

    Duncan

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm going to attempt to be as descriptive as possible in this question, because
I should be able to figure this out, but I keep going in circles.
Here is my attempt in jquery that kind of works but something is going
No matter what i attempt i keep getting the following exception being thrown by
I'm going to attempt to integrate paypal with my php and html website and
I'm going to attempt to optimize some code written in MATLAB, by using CUDA.
I am going to attempt to teach some iphone application development at my job,
This is a brute force attempt at the chromatic number of a matrix. It
So, to explain this, I'll start out by going through the application stack. The
Okay this is going to be hard to explain. I have a page with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.