如何离开外连接下表?

时间:2010-08-26 12:06:55

标签: cakephp

我有两张这样的表:

CREATE TABLE IF NOT EXISTS `publications` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `numerated` tinyint(1) NOT NULL DEFAULT '0',
  `title` text COLLATE utf8_unicode_ci,
  `collection_id` int(11) NOT NULL,
  `note` text CHARACTER SET utf8,
  `adminNote` text CHARACTER SET utf8,
  PRIMARY KEY (`id`),
  KEY `collectionId` (`collection_id`)
  KEY `numerated` (`numerated`),
) ;



CREATE TABLE IF NOT EXISTS `publication_numerations` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `publication_id` int(11) NOT NULL,
  `published` tinyint(1) NOT NULL DEFAULT '0',
  `book` int(11) DEFAULT NULL,
  `number` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `publication_id` (`publication_id`)
) ;



CREATE TABLE IF NOT EXISTS `properties` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `publication_id` int(11) NOT NULL,
  `value` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `publication_id` (`publication_id`)
) 

我需要的是从表发布和publication_numerations中获取所有记录的列表。尝试蛋糕烘烤模型/控制器/视图,但它一直加入 出版物和属性,没有publication_numerations。

你能帮我解决一下吗?

非常感谢你!

更新:所以,我需要的是创建并使用它作为默认查询:

SELECT * FROM `publications` 
LEFT JOIN `publication_numerations` ON (`publications`.`id` = publication_numerations.`publication_id`) 

更新:这是我的模特

  1. 出版物模型

        var $hasOne = array(
            'Publisher' => array(
                'className' => 'Publisher',
                'foreignKey' => 'publication_id',
                'conditions' => '',
                'fields' => '',
                'order' => ''
            ), 
        );
    
        var $belongsTo = array(
            'Collection' => array(
                'className' => 'Collection',
                'foreignKey' => 'collection_id',
                'conditions' => '',
                'fields' => '',
                'order' => ''
            )
        );
    
        var $hasMany = array(
            'PublicationProperty' => array(
                'className' => 'PublicationProperty',
                'foreignKey' => 'publication_id',
                'dependent' => false,
                'conditions' => '',
                'fields' => '',
                'order' => '',
                'limit' => '',
                'offset' => '',
                'exclusive' => '',
                'finderQuery' => '',
                'counterQuery' => ''
            ),
            'PublicationNumeration' => array(
                'className' => 'PublicationNumeration',
                'foreignKey' => 'publication_id',
                'conditions' => '',
                'fields' => '',
                'order' => ''
            )
        );
    
    }
    ?>
    
  2. PublicationNumeration模型

    var $belongsTo = array( 'Publication' => array( 'className' => 'Publication', 'foreignKey' => 'publication_id', 'conditions' => '', 'fields' => '', 'order' => '' ) ); var $hasMany = array( 'Publication' => array( 'className' => 'Publication', 'foreignKey' => 'publication_id', 'conditions' => '', 'fields' => '', 'order' => '' ) ); } ?>
  3. 收集模式

    var $hasMany = array( 'Property' => array( 'className' => 'Property', 'foreignKey' => 'collection_id', 'dependent' => false, 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '', 'exclusive' => '', 'finderQuery' => '', 'counterQuery' => '' ), 'Publication' => array( 'className' => 'Publication', 'foreignKey' => 'collection_id', 'dependent' => false, 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '', 'exclusive' => '', 'finderQuery' => '', 'counterQuery' => '' ) ); } ?>

3 个答案:

答案 0 :(得分:2)

你可以像这样加入蛋糕:

$joins = array(
     array('table' => 'publication_numerations',
       'alias' => 'PublicationNumeration',
       'type' => 'LEFT',
       'conditions' => array(
          'Publication.id = PublicationNumeration.publication_id',
       )
     )
);

$this->Publication->find('all', array('joins' => $joins));

答案 1 :(得分:0)

  

尝试用蛋糕烘烤   模型/控制器/视图,但所有   它加入出版物的时间和   属性,没有   publication_numerations。

因此,手动创建关联 - Associations: Linking Models Together。你似乎不需要'加入'。通过正确设置关联,您可以使用控制器中的find('list')获取所需的列表 - http://book.cakephp.org/view/1017/Retrieving-Your-Data#find-list-1022

答案 2 :(得分:0)

尝试将 $ join 数组的类型属性设置为'LEFT OUTER',而不是'LEFT'