Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8069467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:01:50+00:00 2026-06-05T13:01:50+00:00

I am using Doctrine and trying to implement an infinite scroll on a collection

  • 0

I am using Doctrine and trying to implement an infinite scroll on a collection of notes displayed on the user’s browser.

The application is very dynamic, therefore when the user submits a new note, the note is added to the top of the collection straightaway, besides being sent (and stored) to the server.

Which is why I can’t use a traditional pagination method, where you just send the page number to the server and the server will figure out the offset and the number of results from that.

To give you an example of what I mean, imagine there are 20 notes displayed, then the user adds 2 more notes, therefore there are 22 notes displayed. If I simply requests “page 2”, the first 2 items of that page will be the last two items of the page currently displayed to the user.

Which is why I am after a more sophisticated method, which is the one I am about to explain.

Please consider the following code, which is part of the server code serving an AJAX request for more notes:

// $lastNoteDisplayedId is coming from the AJAX request
$lastNoteDisplayed = $repository->findBy($lastNoteDisplayedId);

$allNotes = $repository->findBy($filter, array('numberOfVotes' => 'desc'));

$offset = getLastNoteDisplayedOffset($allNotes, $lastNoteDisplayedId);

// retrieve the page to send back so that it can be appended to the listing
$notesPerPage = 30       
$notes = $repository->findBy(
  array(), 
  array('numberOfVotes' => 'desc'), 
  $notesPerPage, 
  $offset
);
$response = json_encode($notes);
return $response;

Basically I would need to write the method getLastNoteDisplayedOffset, that given the whole set of notes and one particoular note, it can give me its offset, so that I can use it for the pagination of the previous Doctrine statement.

I know probably a possible implementation would be:

getLastNoteDisplayedOffset($allNotes, $lastNoteDisplayedId) {
    $i = 0;
    foreach ($allNotes as $note) {
        if ($note->getId() === $lastNoteDisplayedId->getId()) {
                break;
        }
        $i++;   
    }
    return $i;
}

I would prefer not to loop through all notes because performance is an important factor.
I was wondering if Doctrine has got a method itself or if you can suggest a different approach.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T13:01:56+00:00Added an answer on June 5, 2026 at 1:01 pm

    In an old project, I used to create an infinite scroll as you want to do.

    What I’ve done is a web service that can receive a parameter called offset.

    In my javascript, I add an event that detect if the user has scroll down enough the document. When the event is triggered, I create the ajax query, I count number of elements allready present in the page (they were in a table).

    $(window).scroll(function(){
                if  ($(window).scrollTop() + $(window).height() >= $(document).height() - 50){
                    lastEvents();
                }
            });
    
    
    var isLoading = false;
    function lastEvents() {
        if (!isLoading) {
          isLoading = true;
          var nb = $('table#events tr').length
    
          $.ajax({
            url: 'getEvents.php',
            data: {
              offset: nb
            },
            success: function (data) {
              isLoading = false;
              $('.table#events').append(data);
            }
          });
        }
    }
    

    Then in my doctrine query, I’ve done something like that:

    $events = Doctrine_Query::create()
                ->select()
                ->from('Events e')
                ->orderBy('e.created_at ASC')
                ->limit(10)
                ->offset($_REQUEST['offset'])
                ->execute();
    

    Then in my template, I generate new table row that will be appended with JS.

    Note: That now, I certainly return json then parse it on client side 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to delete records in many to many using Doctrine. I used
im using symfony with doctrine, and Im trying to generate the schema yml file
What I am trying to do is to get instance of Doctrine EM (using
I'm using Symfony2 and Doctrine 2.0. I'm trying to read data from an XML
Using the guide http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html i'm trying to customize error message, but i've a problem:
I'm using Doctrine 2 for my DB and I'm currently trying to set a
I am trying to create a weighted search using doctrine. This is how i
I'm using Doctrine 2 in Zend. I'm trying to figure out how to access
Using Doctrine 2. I have an entity called 'size' and I'm trying to form
Im using DoctrineFixturesBundle and trying to load fixtures app/console -e=test doctrine:fixtures:load --purge-with-truncate All data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.