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

  • SEARCH
  • Home
  • 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 7769055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:04:19+00:00 2026-06-01T16:04:19+00:00

The following would return the a random record with Doctrine: $name = Doctrine::getTable(‘nametable’) ->createQuery()

  • 0

The following would return the a random record with Doctrine:

$name = Doctrine::getTable('nametable')
->createQuery()
->select('name')
->orderBy('RAND()')
->fetchOne();

But I’m running CI2 + Doctrine2, and so it does not work Call to undefined method Doctrine::getTable()

I’ve tried

$data = $this->doctrine->em->getRepository('ORM\Project\Names')
                 ->orderBy('RAND()')
                 ->fetchOne();

But this does not work either: Uncaught exception 'BadMethodCallException' with message 'Undefined method 'orderBy'. The method name must start with either findBy or findOneBy!'

Perhaps findOneBy is what I want, but it expects an array.

Is there an elegant way to fetch a random record in this setup?

Edit:

This is what I’ve come up with:

$query = $this->doctrine->em->createQuery("select max(u.id) from ORM\Dynasties2\Femalenames u");
$result = $query->getSingleResult();
$highval = $result[1];
$random_name = rand(1,$highval);
$name = $this->doctrine->em->find('ORM\Dynasties2\Femalenames', $random_name);
$mother_name = $name->getName();

Surely there is a cleaner way??? Apparently there’s no such thing as RAND() in CI2/Doctrine2, short of just writing a SQL query.

  • 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-01T16:04:20+00:00Added an answer on June 1, 2026 at 4:04 pm

    This is what I’ve come up with:

    $query = $this->doctrine->em->createQuery("select max(u.id) from ORM\Dynasties2\Femalenames u");
    $result = $query->getSingleResult();
    $highval = $result[1];
    $random_name = rand(1,$highval);
    $name = $this->doctrine->em->find('ORM\Dynasties2\Femalenames', $random_name);
    $mother_name = $name->getName();
    

    I assumed there was another way, but cannot discover it.

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

Sidebar

Related Questions

Given the following XML, I would like to return all eventtitles where the eventtype
Given the following directory: string fullpath = C:\MyDir1\MyDir2\MyDir3; I would like to return MyDir3
I would like to change the contract of the following Quicksort implementation to return
How would you test the following code? public IList<T> Find(DetachedCriteria criteria) { return criteria.GetExecutableCriteria(session).List<T>();
Consider the following method, where I build a string and return it. I would
I would like to only insert a row, if the following SELECT returns a
The intention of following (simplified) code fragment is to return one random row .
Math.random() in javascript is able to return 1, right? Which means if I would
I would like to create a query that returns rows in the following format:
I have following simple program: import std.stdio; int main(string[] argv) { writeln(Hello, world!); return

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.