Alex Young
Alex Young

Reputation: 4039

Propel returning same record multiple times

I have a very simple database (MySql) with one table which I am accessing using Propel with this code...

<?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();
?>

The query seems to be generated correctly...

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

If I run this query through phpMyAdmin I get the expected results, however, Propel seems to be returning the first record found by the query 20 times. Anyone know what might be happening here? Thanks

edit: Record loop

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;
}

Upvotes: 2

Views: 442

Answers (1)

Alex Young
Alex Young

Reputation: 4039

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.

Upvotes: 4

Related Questions