0

for to reduce number of queries, when i select something from my database i want to launch the best possible one, which can be one of these:

1- SELECT ... FROM cities JOIN states ON city.stateID = state.stateID WHERE city.cityID = ...
2- SELECT ... FROM cities WHERE city.cityID = ...
3- SELECT ... FROM states WHERE state.stateID = ...

if i execute the first one, i will not need (probably) to execute 2nd neither the 3rd query, because i already have the data.

but how can i share the result of a single join query between two or more classes of instances? and let's say i had to use the 3rd query first, since i already have the data from 3, how would you control from your code the creation of the City instance?

<?php

 class City
 {
      protected $state;

      function getState()
      {
           if(!$this->state)
                 $this->state = State::getByID($this->stateID);
           return $this->state;
      }

      // since i may already have (in the outer scope) the $state instance...
      // to avoid an unnecessary query (State::getByID()) i allow to set the state:
      function setState(State $state)
      {
           if($state->stateID == $this->stateID)
                $this->state = $state;
           else
                throw new Exception("This City doesn't belong to the given State");
      }
 }

is this correct? am I doing right?

for example, i could create the constructor of "Address" like this:

<?php

class Address
{
    const PREFETCH_CITY = 1;
    const PREFETCH_STATE = 2;
    const PREFETCH_ALL = 3;

    construct($cityID, $prefetch = 0)
    {
         if($prefetch & static::PREFETCH_CITY)
              // i use the "JOIN cities" query (i also set $this->city)
         elseif($prefetch & static::PREFETCH_STATE)
              // i use the "JOIN states" query (i also set $this->state)
         elseif($prefetch & static::PREFETCH_ALL)
              // i use the "JOIN states JOIN cities" query
              // (i preset also both $this->city and $this->state)
         else
              // i don't use any JOIN, i just get the city
    }
}

actually now i'm thinking that constructor should be in a separate class, but whatever...

generally talking... what can i read about this argument? books, tutorials are welcome

hope to have been clear, my english is terrible... very thank you in advance :)

5
  • Not knowing PHP I won't try to answer this question, but it sounds like what you need is some sort of smart caching of your results. But I don't know how the PHP lifecycle works so I wouldn't even know how to begin to tell you how to implement something like that in PHP or even if it's possible in PHP itself.
    – Pete
    Mar 22, 2013 at 21:24
  • that's not actually related to php only, i use also other languages, so i'm looking for a general approach to this issue
    – skyline26
    Mar 22, 2013 at 21:26
  • You might want to describe your overall architecture, otherwise it's hard to give you advice beyond general ideas.
    – Pete
    Mar 22, 2013 at 21:28
  • there is no architecture, it is a generic question not related to any actual "real world" code or purpose, i'm just trying to get a general opinion about that argument. thanks
    – skyline26
    Mar 22, 2013 at 21:32
  • The idea of lazy loading (only when you need the state of a city) is a good one. But if you (almost) always need the state, then it's better to retrieve it in one query, since queries are expensive.
    – agim
    Mar 22, 2013 at 22:15

1 Answer 1

0

Is there ever an instance where the Address class would be called and the City class would not have already been called? If not then you can extend the City class with the Address class, you can access variables from the City class within the Address class, so just change it to. Then once the City class has grabbed the info from the database, and assigned it to various variables then they will be accessible from the Address class.

class Address extends City {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.