我也郁闷这个问题,我是想几个表关联,然后选择出需要的
字段,但是zf好像只列出当前表的字段,而不管父表的字段,所以没办法,我把所有关联的表都写满字段。
摘录:
http://blog.xxiyy.com/?p=13
在
Zend Framework 的 9.8. Zend_Db_Table Relationships 章节中,介绍了使用 Zend_Db_Table 关联表的例子。我听过不少人都觉得这部分相当难理解。我觉得
手册如果能给出例子中所用数据表的E-R图,那么能帮助大家更清晰的理解这个例子。
E-R 图:

建表用
SQL 语句 (
MYSQL 5.0):
–
– 表的结构 `accounts`
–
CREATE TABLE `accounts` (
`account_name` varchar(50) NOT NULL,
PRIMARY KEY (`account_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
–
– 表的结构 `bugs`
–
CREATE TABLE `bugs` (
`bug_id` varchar(50) NOT NULL,
`reported_by` varchar(50) NOT NULL,
`assigned_to` varchar(50) NOT NULL,
`verified_by` varchar(50) NOT NULL,
PRIMARY KEY (`bug_id`),
KEY `reported_by` (`reported_by`),
KEY `assigned_to` (`assigned_to`),
KEY `verified_by` (`verified_by`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
–
– 表的结构 `bugs_products`
–
CREATE TABLE `bugs_products` (
`bug_id` varchar(50) NOT NULL,
`product_id` varchar(50) NOT NULL,
PRIMARY KEY (`bug_id`,`product_id`),
KEY `product_id` (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
–
– 表的结构 `products`
–
CREATE TABLE `products` (
`product_id` varchar(50) NOT NULL,
PRIMARY KEY (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
–
– 限制表 `bugs`
–
ALTER TABLE `bugs`
ADD CONSTRAINT `bugs_ibfk_3` FOREIGN KEY (`verified_by`) REFERENCES `accounts` (`account_name`),
ADD CONSTRAINT `bugs_ibfk_1` FOREIGN KEY (`reported_by`) REFERENCES `accounts` (`account_name`),
ADD CONSTRAINT `bugs_ibfk_2` FOREIGN KEY (`assigned_to`) REFERENCES `accounts` (`account_name`);
–
– 限制表 `bugs_products`
–
ALTER TABLE `bugs_products`
ADD CONSTRAINT `bugs_products_ibfk_2` FOREIGN KEY (`product_id`) REFERENCES `products` (`product_id`),
ADD CONSTRAINT `bugs_products_ibfk_1` FOREIGN KEY (`bug_id`) REFERENCES `bugs` (`bug_id`);
例子中的代码:
class Accounts extends Zend_Db_Table_Abstract
{
protected $_name = ‘accounts’; // 物理表名
protected $_dependentTables = array(‘Bugs’); // 与之关联的表,当前表作为父表
}
class
Products extends Zend_Db_Table_Abstract
{
protected $_name = ‘products’; // 物理表名
protected $_dependentTables = array(‘BugsProducts’); // 与之关联的表,当前表作为父表
}
class
Bugs extends Zend_Db_Table_Abstract
{
protected $_name = ‘bugs’; // 物理表名
protected $_dependentTables = array(‘BugsProducts’); // 与之关联的表,当前表作为父表
protected $_referenceMap = array(
‘Reporter’ => array(
‘columns’ => ‘reported_by’, // 外键
‘refTableClass’ => ‘Accounts’, // 外键引用的表
‘refColumns’ => ‘account_name’ // 外键引用的表的主键
),
‘Engineer’ => array(
‘columns’ => ‘assigned_to’, // 外键
‘refTableClass’ => ‘Accounts’, // 外键引用的表
‘refColumns’ => ‘account_name’ // 外键引用的表的主键
),
‘Verifier’ => array(
‘columns’ => array(‘verified_by’), // 外键
‘refTableClass’ => ‘Accounts’, // 外键引用的表
‘refColumns’ => array(‘account_name’) // 外键引用的表的主键
),
‘Product’ => array(
‘columns’ => array(‘product_id’), // 外键
‘refTableClass’ => ‘Products’, // 外键引用的表
‘refColumns’ => array(‘product_id’) // 外键引用的表的主键
)
);
}
class
BugsProducts extends Zend_Db_Table_Abstract
{
protected $_name = ‘bugs_products’; // 物理表名
protected $_referenceMap = array(
‘Bug’ => array(
‘columns’ => array(‘bug_id’), // 外键
‘refTableClass’ => ‘Bugs’, // 外键引用的表
‘refColumns’ => array(‘bug_id’)
// 外键引用的表的主键
),
‘Product’ => array(
‘columns’ => array(‘product_id’), // 外键
‘refTableClass’ => ‘Products’, // 外键引用的表
‘refColumns’ => array(‘product_id’)
// 外键引用的表的主键
)
);
}