表定义:
create table think_product(
id int(8) unsigned NOT NULL AUTO_INCREMENT,
menu_id int(11) null,
title varchar(256) null,
PRIMARY KEY (`id`)
)ENGINE=MyISAM;
CREATE TABLE `think_menu` (
`id` int(8) UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` varchar(256) NULL DEFAULT NULL ,
PRIMARY KEY (`id`)
)
ENGINE=MyISAM;class ProductModel extends RelationModel{
protected $_link = array(
'menu'=>array(
'mapping_type'=>HAS_ONE,
'class_name' =>'Menu',//关联模型
'foreign_key' =>'menu_id'
)
);
}我这样调用 $product = D('Product');
$this->list = $product->relation(true)->select();
dump($this->list);
dump($product->getLastSql());得出的结果为:array(1) {
[0] => array(8) {
["id"] => string(1) "2"
["menu_id"] => string(1) "2"
["title"] => string(1) "2"
["menu"] => bool(false)
}
}string(30) "SELECT * FROM `think_product` "问题: 关联的表死活出不来 ,我确认字段menu_id在表think_menu中是有对应数据的,为什么得出的结果却是
["menu"] => bool(false)
而且我试了只有当 'foreign_key' =>'id'时才能关联出数据,但那不是我想要的结果啊 ,求指教
最佳答案