开发者

How to left outer join next tables?

I have two tables like this:

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` tinyi开发者_运维知识库nt(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`)
) 

All I need is to have list of all records from table publications and publication_numerations. Tryed with cake bake model/controller/view, but all the time it joins publications and properties, without publication_numerations.

Can you help me to solve this?

Thank you very much in advance!

UPDATED: so, all I need is to create and use it as default query:

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

UPDATED: here are my models

  1. publications model

        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 model

    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. Collection modes

    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' => '' ) ); } ?>


You can do joins in cake like so:

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

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


Tryed with cake bake model/controller/view, but all the time it joins publications and properties, without publication_numerations.

So, create associations manually - Associations: Linking Models Together. It does not seems that you need 'joins'. By setting associations correctly you will be able to get the list you need by using find('list') in your controller - http://book.cakephp.org/view/1017/Retrieving-Your-Data#find-list-1022.


Try making the type attribute of $join array 'LEFT OUTER' instead of 'LEFT'.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜