2010-09-13 93 views
1

我目前在後端使用了與MySQL的Doctrine 1.2.2。當我嘗試使用記錄或延遲加載水化模式檢索多個項目的結果集時,只顯示一個項目。但是,當我使用陣列水合模式時,我會看到所有的結果。始終只檢索結果集中的最後一個項目。爲什麼Doctrine只檢索結果集中的最後一條記錄?

我誤解了API嗎?模型定義不正確(它是自動生成的)。難道不是這麼簡單嗎?

現在,我打開了我的調試器,我將逐步通過Doctrine的源代碼。

// Model used: 

<?php 

/** 
* BaseEntryVote 
* 
* This class has been auto-generated by the Doctrine ORM Framework 
* 
* @property integer $contest_id 
* @property integer $vote_id 
* @property integer $entry_id 
* @property integer $subcategory_id 
* @property string $company 
* @property string $website 
* @property string $email 
* @property string $comments 
* @property integer $votes 
* 
* @package ##PACKAGE## 
* @subpackage ##SUBPACKAGE## 
* @author  ##NAME## <##EMAIL##> 
* @version SVN: $Id: $ 
*/ 
abstract class BaseEntryVote extends Doctrine_Record 
{ 
    public function setTableDefinition() 
    { 
     $this->setTableName('entry_vote'); 
     $this->hasColumn('contest_id', 'integer', 4, array(
      'type' => 'integer', 
      'length' => 4, 
      'fixed' => false, 
      'unsigned' => false, 
      'primary' => true, 
      'notnull' => false, 
      'autoincrement' => false, 
      )); 
     $this->hasColumn('vote_id', 'integer', 4, array(
      'type' => 'integer', 
      'length' => 4, 
      'fixed' => false, 
      'unsigned' => false, 
      'primary' => false, 
      'notnull' => true, 
      'autoincrement' => false, 
      )); 
     $this->hasColumn('entry_id', 'integer', 4, array(
      'type' => 'integer', 
      'length' => 4, 
      'fixed' => false, 
      'unsigned' => false, 
      'primary' => false, 
      'default' => '0', 
      'notnull' => false, 
      'autoincrement' => false, 
      )); 
     $this->hasColumn('subcategory_id', 'integer', 4, array(
      'type' => 'integer', 
      'length' => 4, 
      'fixed' => false, 
      'unsigned' => false, 
      'primary' => false, 
      'default' => '0', 
      'notnull' => false, 
      'autoincrement' => false, 
      )); 
     $this->hasColumn('company', 'string', 100, array(
      'type' => 'string', 
      'length' => 100, 
      'fixed' => false, 
      'unsigned' => false, 
      'primary' => false, 
      'notnull' => false, 
      'autoincrement' => false, 
      )); 
     $this->hasColumn('website', 'string', 75, array(
      'type' => 'string', 
      'length' => 75, 
      'fixed' => false, 
      'unsigned' => false, 
      'primary' => false, 
      'notnull' => false, 
      'autoincrement' => false, 
      )); 
     $this->hasColumn('email', 'string', 75, array(
      'type' => 'string', 
      'length' => 75, 
      'fixed' => false, 
      'unsigned' => false, 
      'primary' => false, 
      'notnull' => false, 
      'autoincrement' => false, 
      )); 
     $this->hasColumn('comments', 'string', 255, array(
      'type' => 'string', 
      'length' => 255, 
      'fixed' => false, 
      'unsigned' => false, 
      'primary' => false, 
      'notnull' => false, 
      'autoincrement' => false, 
      )); 
     $this->hasColumn('votes', 'integer', 4, array(
      'type' => 'integer', 
      'length' => 4, 
      'fixed' => false, 
      'unsigned' => false, 
      'primary' => false, 
      'notnull' => false, 
      'autoincrement' => false, 
      )); 
    } 

    public function setUp() 
    { 
     parent::setUp(); 

    } 
} 


// Below is the code used to access the doctrine api 

/* 
Table entry_vote 
================ 
contest_id, vote_id, entry_id, subcategory_id, company, website, email, comments, votes 
---------------- 
contest_id INT 
vote_id  INT 
entry_id  INT 
subcategory_id INT 
company  VARCHAR 
website  VARCHAR 
email  VARCHAR 
comments  VARCHAR 
votes  INT 

Data in db: 
'0', '1', '1', '0', 'Foo Bank', 'http://localhost/foo', '[email protected]', NULL, '2' 
'0', '0', '0', '0', 'TPS Corp', 'http://localhost/tps', '[email protected]', NULL, '1' 
*/ 

$result = Doctrine_Core::getTable('EntryVote')->findAll(); 

foreach ($result as $entry) { 
    print $entry->company; 
} 

/* Here is the query that Doctrine is generating: */ 
SELECT e.contest_id AS e__contest_id, e.vote_id AS e__vote_id, 
e.entry_id AS e__entry_id, e.subcategory_id AS e__subcategory_id, 
e.company AS e__company, e.website AS e__website, 
e.email AS e__email, e.comments AS e__comments, e.votes AS e__votes 
FROM entry_vote e WHERE (e.contest_id = ?) 

回答

2

感謝#doctrine irc頻道上的好人,我現在正在工作。

問題的根源在於我沒有在投票表的主鍵上設置自動增量。因此,如果您遇到類似問題,請確保您選擇的任何列作爲模型的主鍵,並且在數據庫模式中設置了自動增量位。

相關問題