Take the 2-minute tour ×

I hope the title was descriptive enough, i wasn't sure how to name it.

Let's say i have the following code:

Class Movie_model {

    public method getMoviesByDate($date) {
        // Connects to db
        // Gets movie IDs from a specific date
        // Loop through movie IDs
        // On each ID, call getMovieById() and store the result in an array
        // When all IDs has looped, return array with movies returned from getMovieById().

    public function getMovieById($id) {
         // Get movie by specified ID
         // Also get movie genres from another method
         // Oh, and it gets movie from another method as well.


I always want to get the same result when getting a movie (I always want the result from getMovieById().

I hope you get my point. I will have many other functions like getMoviesByDate(), i will also have getMoviesByGenre() for example, and i want that to return the same movie info as getMovieById() as well.

It it "ok" to do it this way? I know this puts more load on the server and increases load time, but is there any other, better way that i don't know of?

EDIT: I clarified the code in getMoviesByDate() a bit. Also, getMovieByDate() is just an example. As i said, i will be calling methods like getMoviesByGenre() also.

EDIT: I'm currently running 48 database queries on the frontpage of my project, and the frontpage is still far from finished, so that number would at least triple when i'm done. Almost all queries take around 0.0002, but as the database keeps growing that number will rise dramatically i'm guessing. I need to change something.

share|improve this question
Looks like a perfectly good way to do it. Just make sure you add sufficient indexes –  Niklas Modess Oct 11 '12 at 6:43
you should not call two methods to get same data do "joins" in your getMoviesByDate methods and return result . –  Devnegikec Oct 11 '12 at 6:45
add comment

4 Answers

I don't think it's good to work like this in this particular case. The function getMoviesByDate would return an amount of "n" movies (or movie ids) from a single query. For each id in this query you would have a separate query to get the movie by the specified ID.

This would mean if the first function would return 200 movies, you would run the getMovieById() function (and the query inside it) 200 times. A better practice (IMO) would be to just get all the info you require in the getMoviesByDate() function and return it as a collection.

share|improve this answer
Read the comment he has in the getMoviewByDate() function: // On each ID, call getMovieById() and store the result in an array // When all IDs has looped, return array with movies returned from getMovieById(). , I think it pretty much rules out what you are commenting ;) –  KennyDs Oct 11 '12 at 7:14
That was added 10 minutes after my comment but you are correct he is infact considering that which is insane. –  MrCode Oct 11 '12 at 7:21
Sorry about the late reply. Yes, that is indeed insane, and i'm not only considering it but i'm actually using it right now. It works, but i know it's a terrible idea, hence this question. Now that we have esstablished it's a really (really!) bad idea, let's think of a solution shall we? :) –  qwerty Oct 11 '12 at 14:12
As mentioned in my comment: A better practice (IMO) would be to just get all the info you require in the getMoviesByDate() function and return it as a collection. –  KennyDs Oct 12 '12 at 6:28
add comment

It doesn't seem very logical to have getMoviesByDate() and getMoviesById() methods on a Movie class.

An alternative would be to have some sort of MovieManager class that does all of the retrieving, and returns Movie objects.

class MovieManager {

    public function getMoviesByDate($date) {
        // get movies by date, build an array of Movie objects and return

    public function getMoviesByGenre($genre) {
       // get movies by genre, build an array of Movie objects and return

    public function getMovieById($id) {
       // get movie by id, return Movie object


Your Movie class would just have properties and methods specific to a single movie:

class Movie {

    public id;
    public name;
    public releaseDate;


It's OK to have separate methods for getting by date, genre etc etc, but you must ensure that you are not calling for the same records multiple times - in that case you will want a single query that could join the various tables you need.

Edit - after you have clarified your question:

The idea of getting movie IDs by date, then running them all through getMovieById() is bad! The movie data should be pulled when getting by date, so you don't have to hit the database again.

share|improve this answer
I read somewhere using SomthingManager as a class name is a bad practise –  Tarun Oct 11 '12 at 9:05
is MovieManager a Mapper Class? –  Tarun Oct 11 '12 at 9:06
Why doesn't it seen logical to have getMovieByDate() and getMovieById() in a movie class? Imo it doesn't get more logical than that. I didn't mention it in the question because i didn't think it would matter, but i'm using Codeigniter, and the Movie class is actually a model. The whole purpose of the class (model) is to get and/or manage movie data. Now, as for your solution, i don't think it would solve my problem. You see, the problem is to get the same movie info from every getMovieByX method, but preferably with just one (or a few, but definitely not 100's which ... –  qwerty Oct 11 '12 at 14:23
... it results with in some cases as it is now) query. In the getMovieById() method i also fetch other movie information, such as movie images, genres, movie cast, if it's available to watch, and more. That information is received from other methods called from within getMovieById(). What i could do, is call those methods from the getMovieByDate() method too, but that doesn't feel as a good idea. I want consistency between the movie results, if i ever forgot to add a method to getMovieByDate() after adding it to getMovieById() it would break the whole consistency theory. ... –  qwerty Oct 11 '12 at 14:24
Do you get what i'm saying? It's kinda messy. (Had the break the comment up in multiple comments) –  qwerty Oct 11 '12 at 14:24
show 2 more comments

You can modified your getMovieById function. You can pass date as a parameter, the function should return the movies by their id and filtered by date.

share|improve this answer
please explain why downvote? –  Rohit Kumar Choudhary Oct 11 '12 at 8:36
Whoever downvoted, i disagree with. But this still isn't a good solution for me. Let's say i would add $date as a param to getMovieById(), then i would have to do so for getMovieByGenre() and other similar methods too. That would result in very, very messy code. I shouldn't have to pass what i want to fetch as param (well, in some cases it would be completely fine, but not in my case), it wont work. –  qwerty Oct 11 '12 at 14:26
add comment

To keep track which records you've already loaded into RAM previously you can use a base class for your models which saves the id's of the records already loaded and a reference to object the model object in the RAM.

class ModelBase {
    /* contains the id of the current record, null if new record */
    protected $id;

    // keep track of records already loaded
    static $loaded_records = Array();

    public function __construct(Array $attr_values) {
        // assign $attr_values to this classes attributes

        // save this instance in class variable to reuse this object
        if($attr_values['id'] != null) {
            self::$loaded_records[get_called_class()][$attr_values['id']] = $this;

    public static function getConcurrentInstance(Array $attr_values) {

        $called_class = get_called_class();
        if(isset(self::$loaded_records[$called_class][$attr_values['id']])) {

            // this record was already loaded into RAM
            $record = self::$loaded_records[$called_class][$attr_values['id']];

            // you may need to update certain fields of $record
            // from the data in $attr_values, because the data in the RAM may
            // be old data.

        } else {
            // create the model with the given values
            $record = new $called_class($attr_values);
        return $record;

    // provides basic methods to update records in ram to database etc.
    public function save() {
        // create query to save this record to database ...

Your movie model could look something like this.

Class MovieModel extends ModelBase {
    // additional attributes
    protected $title;
    protected $date;
    // more attributes ...

    public static function getMoviesByDate($date) {
       // fetches records from database
       // calls getConcurrentInstance() to return an instance of MovieModel() for every record

    public static function getMovieById($id) {
       // fetches record from database

Other things you could do do decrease the load on the DB:

  • Only connect once to the database per request. There are also possibilities to share a connection to a database between multiple requests.
  • Index thefields in your database which get searched often.
  • only fetch the records you need
  • Prevent to load the same record twice (if it didn't change)
share|improve this answer
Having all of your classes extend the database class is just not good. –  MrCode Oct 11 '12 at 7:01
@MrCode why? Only the models should extend MyDataBase –  MarcDefiant Oct 11 '12 at 7:03
In your answer, Movie extends Database. Well you cannot say that Movie is a Database and therefore inheritance is likely the wrong choice. Take this example: class MySQL extends Database - you can say that MySQL is a database and therefore is a good option to inherit. If movie is your model then it would be better to have a reference to the database rather than extending it. –  MrCode Oct 11 '12 at 7:33
@MrCode what if I simply renamed the class MyDataBase to BaseModel –  MarcDefiant Oct 11 '12 at 7:58
I'm using codeigniter, the built in Active Record is responsible for creating database connection, i trust it does a good job with that. As for the solution, if only it was that simple. I have thought of every possible solution, yours included, but it wont cut it for several reasons. I'm not always fetching my column, in some cases i'm using joins. There are other examples as well, but i don't have the code in front of me, otherwise i would have given more examples. –  qwerty Oct 11 '12 at 14:31
show 1 more comment

Your Answer


