在单个查询中从多个表中检索Cakephp数据

时间:2012-12-20 22:04:38

标签: cakephp-2.0 data-retrieval

我有以下mysql表:

CREATE TABLE Articles (
   ArticleID SMALLINT NOT NULL PRIMARY KEY,
   ArticleTitle VARCHAR(60) NOT NULL,
   Copyright YEAR NOT NULL
)
ENGINE=INNODB;


INSERT INTO Articles VALUES (12786, 'How write a paper', 1934),
                            (13331, 'Publish a paper', 1919),
                            (14356, 'Sell a paper', 1966),
                            (15729, 'Buy a paper', 1932),
                            (16284, 'Conferences', 1996),
                            (17695, 'Journal', 1980),
                            (19264, 'Information', 1992),
                            (19354, 'AI', 1993);


CREATE TABLE Authors (
   AuthID SMALLINT NOT NULL PRIMARY KEY,
   AuthorFirstName VARCHAR(20),
   AuthorMiddleName VARCHAR(20),
   AuthorLastName VARCHAR(20)
)
ENGINE=INNODB;


INSERT INTO Authors VALUES (1006, 'Henry', 'S.', 'Thompson'),
                           (1007, 'Jason', 'Carol', 'Oak'),
                           (1008, 'James', NULL, 'Elk'),
                           (1009, 'Tom', 'M', 'Ride'),
                           (1010, 'Jack', 'K', 'Ken'),
                           (1011, 'Mary', 'G.', 'Lee'),
                           (1012, 'Annie', NULL, 'Peng'),
                           (1013, 'Alan', NULL, 'Wang'),
                           (1014, 'Nelson', NULL, 'Yin');


CREATE TABLE AuthorArticle (
   AuthID SMALLINT NOT NULL,
   ArticleID SMALLINT NOT NULL,
   PRIMARY KEY (AuthID, ArticleID),
   FOREIGN KEY (AuthID) REFERENCES Authors (AuthID),
   FOREIGN KEY (ArticleID) REFERENCES Articles (ArticleID)
)
ENGINE=INNODB;


INSERT INTO AuthorArticle VALUES (1006, 14356), 
                              (1008, 15729), 
                              (1009, 12786), 
                              (1010, 17695),
                              (1011, 15729), 
                              (1012, 19264), 
                              (1012, 19354), 
                              (1014, 16284);

现在我想运行一个查询来从所有三个表中获取数据。我的查询如下:

SELECT ArticleTitle, Copyright, CONCAT_WS(' ', AuthorFirstName, AuthorMiddleName, AuthorLastName) AS Author
FROM Articles AS b STRAIGHT_JOIN AuthorArticle AS ab STRAIGHT_JOIN Authors AS a
WHERE b.ArticleID=ab.ArticleID AND ab.AuthID=a.AuthID AND Copyright<1980
ORDER BY ArticleTitle;

所以我的问题是如何在CakePHP 2+中实现这一目标。任何帮助表示赞赏...

1 个答案:

答案 0 :(得分:1)

我能够回答我自己的问题,但在某种程度上,即连接多个表但无法加入'STRAIGHT_JOIN'。所以我的解决方案如下:

$art = $this->Article->find('all', array('joins' => array(
    array(
        'table' => 'authorarticle',
        'alias' => 'AuthArt',
        'type' => 'inner',
        'foreignKey' => false,
        'conditions'=> array('AuthArt.ArticleID = Article.ArticleID')
    ),
    array(
        'table' => 'authors',
        'alias' => 'Author',
        'type' => 'inner',
        'foreignKey' => false,
        'conditions'=> array('AuthArt.AuthID = Author.AuthID')
        )
    ), 'fields'=>array('Article.ArticleTitle','CONCAT_WS(" ", Author.AuthorFirstName, Author.AuthorMiddleName, Author.AuthorLastName) As AuthorName','Article.Copyright'), 'conditions' => array('Article.Copyright' => 1980)));
$this->set('articles', $art); // debug($articles) in the view to get results

希望它会帮助其他人寻找相同的东西。如果我自己的问题解决方案很好或者可以进一步优化以获得CAKEPHP方式的最佳结果,我想听听社区的意见。感谢