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 4026646
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:59:48+00:00 2026-05-20T10:59:48+00:00

Hallo, I have sort of daily random image system build with Codeigniter and Doctrine.

  • 0

Hallo,

I have sort of daily random image system build with Codeigniter and Doctrine.

There is key function, which returns daily image or sets new one. I am using recursive calling, but sometimes are two images selected for one day.

class Daily extends Controller {

    function find_daily(){
        $connection = Doctrine_Manager::connection();
        $connection->setCharset('utf8');

        $q = Doctrine_Query::create()
            ->select('COUNT(id) as dailycount')
            ->from('Image')
            ->where('daily_date = ?',date("Y-m-d"));



        $set = $q->execute();

        if( $set[0]->dailycount != 0 ){ //theres one or more for today



            $q = Doctrine_Query::create()
            ->select('id')
            ->from('Image')
            ->where('daily_date = ?',date("Y-m-d"))
            ->limit(1);


            $set = $q->execute();
            $i = Doctrine::getTable('Image')->findOneById($set[0]["id"]);

            return $i;

            }else { // none image selected with today date


                $q = Doctrine_Query::create()
                ->select('id')
                ->from('Image')
                ->where('daily_date = ?', "0000-00-00")
                ->andWhere("dir =?","queue")
                ->orderBy("rand()")
                ->limit(1);


                $set = $q->execute();


                $i = Doctrine::getTable('Image')->findOneById($set[0]["id"]);

                $i->daily_date = date("Y-m-d");
                $i->dir = "archive";
                $i->save();

                $this -> find_daily();
                };

        }

...

I think theres some stupid mistake.. maybe calling something like “Doctrine::doAllAndClear” before $this -> find_daily(); or I forget something…

Or is better not to use recursive calls?

Thanks.

  • 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-05-20T10:59:49+00:00Added an answer on May 20, 2026 at 10:59 am

    Well, I’ve not tested this at all, but I’d probably be more likely to write your code more like this; hopefully this will give you some ideas:

      function find_daily() {
        $connection = Doctrine_Manager::connection();
        $connection->setCharset('utf8');
    
        // Make sure "today" doesn't change while the script runs
        $today = date("Y-m-d"); 
    
        // Find an image with today's date.
        $q = Doctrine_Query::create()
          ->from('Image')
          ->where('daily_date = ?', $today)
          ->limit(1);
    
        $image = $q->fetchOne();
    
        if (!$image) {
          // There was no image with today's date, so pick one from the queue at random instead.
          $q = Doctrine_Query::create()
            ->from('Image')
            ->where('daily_date = ?', "0000-00-00")
            ->andWhere("dir =?","queue")
            ->orderBy("rand()")
            ->limit(1);
    
          $image = $q->fetchOne();
          // ToDo: Cope with there being no image in the queue
    
          $image->setDailyDate($today);
          $image->setDir("archive");
          $image->save();
        }
        return $image;
      }
    

    However, you need to think about what happens if two people hit your script at the same time. In that case, they may both fetch a new image for the day (because they could each run the first SELECT at the same time, and both get a result that says there’s no daily image yet.)

    To avoid that, I’d guess you’d want to wrap this in a transaction, and set a transaction isolation level such that the first caller of the function “wins” the race condition, and subsequent callers block until the update is safely saved. Not entirely sure how you’d do that with Doctrine, but it shouldn’t be hard.

    Checking the manual, a beginTransaction() at the start, a commit() at the end, and a setup before everything else involving $tx = $conn->transaction; $tx->setIsolation('SERIALIZABLE'); (otherwise two users could probably still run the SELECT at once) would probably be all you need, but you might want to wait for someone who really knows Doctrine to comment on that…

    I also saved “today”‘s date at the start of the script, otherwise there’s a danger of it being different during the SELECT and the later UPDATE, if the script runs across midnight.

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

Sidebar

Related Questions

i have a requirement to build some sort of services that can easily be
I'm having some trouble with subroutines, namely the sort function being used to sort
i am trying to write a boot loader(hello world sort). i am using Bochs
Possible Duplicate: Sorting an associative array in PHP Hello all, I have an array
hello I want to learn how cpu works from scale of bit level to
Possible Duplicate: Windows batch (files) Hello . I am trying to do a batch
VB.NET 2010, .NET 4 Hello, I tried running code analysis the first time and
edited in response to comments * Hello, I am an XSLT noob and need
Hi i wanna work with some bluetooth communication using my Android device. How can

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.