1

我有桌子

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,title,parent_title,状态

意味着我想要相对标题代替 parent_id(在这里 parent_title)

4

3 回答 3

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)
于 2013-06-14T06:25:55.073 回答
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');
于 2013-06-14T06:52:12.850 回答
1

Cakephp 代码

在类别控制器中

$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' => ''
        )
);
于 2013-06-14T06:55:53.697 回答