2012-02-29 2 views
0

Zend Framework beginner here。私はビデオゲームデータベースのすべてのXboxタイトルを取得しようとしています。 1つのテーブルにはゲームが含まれます。別のテーブルにはゲームタイプ(Xbox、Xbox Live Arcadeなど)が含まれています。私は通常、次のクエリを使用してXboxのタイトルを取得します。Zend_Dbを使用したSQLクエリの場所

Zend_Dbを使用して同じクエリを実行するにはどうすればよいですか? Zend Frameworkの中でいくつかの方法を書き換えることができ

おかげで、

SELECT titleGame 
FROM Game 
WHERE idGameType IN (
    SELECT idGameType 
    FROM GameType 
    WHERE nameGameType = 'Xbox') 

答えて

6

。ここで私は通常、Zend_Db_Table_Selectを使ってそのような選択を書く方法です。それはクエリがよりポータブルになり、そして最も重要なクエリを確保することが非常に容易になりますので

<?php 

// For brevity, $dbTable = a Zend_Db_Table object 

// first construct the subquery/join for the IN clause 
// SELECT idGameType FROM GameType HERE nameGameType = 'Xbox' 
$subselect = $dbTable->select() 
        ->from('GameType', array('idGameType')) 
        ->where('nameGameType = ?', 'Xbox'); // quotes Xbox appropriately, prevents SQL injection and errors 

// construct the primary select 
// SELECT titleGame FROM Game WHERE idGameType IN (subquery) 
$select = $dbTable->select() 
        ->setIntegrityCheck(false) // allows us to select from another table 
        ->from($dbTable, array('titleGame')) 
        ->where('idGameType IN (?)', $subselect); 

$results = $select->query()->fetchAll(); // will throw an exception if the query fails 
if(0 === count($results)) { 
    echo "No Results"; 
}else{ 
    foreach($results as $result){ 
     echo $result['titleGame'] . '<br />'; 
    } 
} 

あなたはまた、文字列としてSQLを記述することができますが、可能な場合は、オブジェクト指向のアプローチが理想的です。

例:

$db = Zend_Db_Table::getDefaultAdapter(); // get the default Db connection 
$db->select("select * from table where id = 3"); // doable, but not recommended 

また、PHPのPDO延長にZend_Db_Statementprepared statementを作成することができます。

$sql = 'SELECT * FROM bugs WHERE reported_by = ? AND bug_status = ?'; 
$stmt = new Zend_Db_Statement_Mysqli($db, $sql); 
$stmt->execute(array('goofy', 'FIXED')); 

最初のアプローチは、オブジェクト指向の流れるようなインターフェイスは、あなたが最もを見るものであり、この方法は、私は始めと使用することをお勧めします。

詳細は、Zend_Db Manual Pages、特にZend_Db_Table_SelectZend_Db_Table、およびZend_Db_Adapterを読んでください。 ZF Quickstartを読んでも、Db部分に特に注意を払うだけでも役立ちます。アプリケーションとデータベースの間のゲートウェイになるようにテーブルクラスを設定する方法を示します。

+0

私はそれを試してみました(私のコントローラ[ここ](http://pastebin.com/JnPdAnba)のinit関数を参照してください)。アプリケーションエラーが発生します。例外は「選択クエリは別のテーブルと結合できません」です。埋め込みセレクトで返された結果をハードコーディングしても問題ありません([here](http://pastebin.com/ejVSkxmw)を参照)。 –

+0

GameTypeを照会しているサブクエリに 'Games' DbTableを使用しているので、' setIntegrityCheck(false) 'をそのクエリの' select() '部分の後ろに追加してみてください。あるいは、副選択に 'Application_Model_DbTable_GameType'を使うことができます。それで、今のところ2番目のクエリで' setIntegrityCheck(false) 'だけが必要になります。 – drew010

+0

それはトリックをしました。ありがとう! –

0
SELECT titleGame FROM Game 
LEFT JOIN GameType ON GameType.idGameType = Game.idGameType 
WHERE GameType.nameGameType = 'Xbox' 

なぜ、結合を使用しないのですか?私の経験から、INの副選択はMySQLでは非常に遅いです。

$select = $db->select(); 
$select->from('Game', array('titleGame')); 
$select->joinLeft('GameType', 'GameType.idGameType = Game.idGameType', array()); 
$select->where("GameType.nameGameType = 'Xbox'"); 
+0

私はあなたの答えを考えます。ここのデスクトップ開発者。私はデータベースを扱って以来、しばらくしています。 –

関連する問題