推进多次返回相同记录

时间:2016-08-31 15:54:50

标签: php mysql propel

我有一个非常简单的数据库(MySql),其中有一个表,我使用Propel访问此代码...

<?php
$autoloader = require '/vendor/autoload.php';
$autoloader->add('', __DIR__ . '/generated-classes/');

use Propel\Runtime\Propel;
use Propel\Runtime\Connection\ConnectionManagerSingle;

require './generated-conf/config.php';
require './includes/pagebuilder.php';

Propel::getConnection("default")->useDebug(true);

$videos = VideosQuery::create()
    ->orderByAddeddate()
    ->paginate($page = 1, $maxPerPage = 20);

echo GetMainPage($videos);
echo Propel::getConnection()->getLastExecutedQuery();
?>

查询似乎正确生成...

SELECT videos.id, videos.AddedDate, videos.Rating, videos.Views, videos.Title, videos.Description, videos.ImageUrl, videos.EmbedUrl FROM videos ORDER BY videos.AddedDate ASC LIMIT 20

如果我通过phpMyAdmin运行此查询,我得到预期的结果,但是,Propel似乎返回查询找到的第一条记录20次。有人知道这里会发生什么吗? 感谢

编辑:记录循环

function GetMainContent($videos) {
    $mc = '<main>
            <div id="video-box-wrapper">';

    foreach($videos as $video) {
        $mc .= '<div class="video-box">
                    <a href="#">
                        <img src="' . $video->getImageUrl() . '" />
                        <span>' . $video->getTitle() . '</span>
                        <br />
                        <p>' . $video->getViews() . ' views</p>
                        <p>Rating: ' . $video->getRating() . '/10</p>
                    </a>
                </div>';
    }
    $mc .= '</main>';

    return $mc;
}

1 个答案:

答案 0 :(得分:3)

The schema I had generated from my existing database using propel's init command was created prior to setting a primary key for that table. This caused Propel to return the same record multiple times in one query. This was resolved by regenerating the schema with the primary key in place.