我有像
这样的表格CREATE TABLE IF NOT EXISTS `categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`parent_id` int(11) NOT NULL DEFAULT '0',
`status` enum('1','2') NOT NULL COMMENT '''1''=active,''2''=inactive',
PRIMARY KEY (`id`)
)
现在我想要像
这样的记录ID,标题,PARENT_TITLE,状态
表示我希望相对标题代替parent_id(在此处为parent_title)
答案 0 :(得分:2)
这是伙伴:
SELECT c1.id, c1.title, c2.title as parent_title, c1.status
FROM categories c1
LEFT JOIN categories c2 ON (c2.id = c1.parent_id)
答案 1 :(得分:1)
$fields = array('c1.id, c1.title, c2.title as parent_title, c1.status');
$joins = array();
$joins[] = array(
'table' => 'categories',
'foreignKey' => false,
'conditions' => 'c2.id = c1.parent_id',
'type' => 'LEFT',
'alias' => 'c2',
);
$this->paginate = compact('fields', 'joins');
答案 2 :(得分:1)
在类别控制器中
$fields=array(
'Category.*',
'ParentCategory.title'
);
$data = $this->Category->find("all",array(
'fields' => $fields,
'recursive'=>0
));
在类别模型
中var $belongsTo = array(
'ParentCategory' => array(
'className' => 'Category',
'foreignKey' => 'parent_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);