php - Complex reference maps in Zend_Db_Table to account for multi-column keys -
i going attempt keep simple possible, use case outside original intention of zend_db fear. concerns set of tables have tagging pages (or else eg. documents) in cms.
i have 3 tables:
- pages (
pages
) - tags (
tags
) - taglink (
tags_link
) many-to-many linking table between pages , tags
pages simple table (i have removed inconsequential columns 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 quite simple although there 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 again 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 taglink able link against other table in database , not pages. if example had documents upload section tagged. facilitate way of working there multi-column key.
to make clearer demonstrate couple of insert queries might run when tags added table (eg. pages):
insert `tags_link` set `tag_id` = '1', `module_type` = 'pages', `foreign_key` = '2' insert `tags_link` set `tag_id` = '1', `module_type` = 'documents', `foreign_key` = '3'
so can see module_type
column arbitrary string describes foreign key can found. not name of table id can have tags linked if not in mysql database.
now zend_db_table
$_referencemap
in pagetable
:
protected $_referencemap = array( 'taglink' => array( 'columns' => 'id', 'reftableclass' => 'models_tag_taglinktable', 'refcolumns' => 'foreign_key' ), );
but not take account arbitrary module_type
column , return taglink same foreign key. bad because taglinks documents mixed in pages instance.
so question how can take account additional column when setting reference? aim avoid having taglink class each module_type
have now.
i imagine following explain requirements although not how done:
protected $_referencemap = array( 'taglink' => array( 'columns' => 'id', 'reftableclass' => 'models_tag_taglinktable', 'refcolumns' => 'foreign_key', 'where' => 'module_type = "pages"' ), );
my current implementation overrides _fetch
method in documents_taglinktable
in 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 can see there maybe more 1 set of tags added object well.
example 3 in "fetching dependent rowsets" in zend framework reference demonstrates technique use:
http://framework.zend.com/manual/en/zend.db.table.relationships.html
whilst doesnt show "where" clause being included in select, should work.
duncan
Comments
Post a Comment