代码之家  ›  专栏  ›  技术社区  ›  Elijah

为什么条令只检索结果集中的最后一条记录?

  •  1
  • Elijah  · 技术社区  · 14 年前

    我误解API了吗?模型定义不正确(它是自动生成的)。不是应该这么简单吗?

    // 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', 'bob@foo.com', NULL, '2'
    '0', '0', '0', '0', 'TPS Corp', 'http://localhost/tps', 'bob@tps.com', 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 = ?)
    
    1 回复  |  直到 14 年前
        1
  •  2
  •   Elijah    14 年前

    多亏了irc频道的好心人,我现在可以工作了。

    问题的根源是我没有在投票表的主键上设置自动递增。因此,如果您遇到类似的问题,请确保在模型中选择作为主键的任何列在数据库模式中也设置了自动增量位。

    推荐文章