-1

I'm new to OO PHP and I have created a Database Class, And I have a problem when I build My SELECT Query With the JOIN method.

If I do the Querys in Functions.

<?php
    require_once("class.Database.php");

    function test1($db) {
        $test1 = $db->Select("*")
            ->From("Table1")
            ->Join("Table2","table_2_id = table_1_id")
            ->Join("Table3","table_3_id = table_2_id")
            ->BuildSelect();

        return $test1;
    }

    function test2($db) {
        $test2 = $db->Select("*")
            ->From("Table4")
            ->Join("Table5","table_5_id = table_4_id")
            ->Join("Table6","table_6_id = table_5_id")
            ->BuildSelect();

        return $test2;
    }

    echo test1($db);
    echo "<br>";
    echo test2($db);
?>

The problem is that the First function - test1 will print out - SELECT * FROM Table1 LEFT JOIN Table2 ON table_2_id = table_1_id LEFT JOIN Table3 ON table_3_id = table_2_id - Which is good

But then the second function test2 will print out - SELECT * FROM Table4 LEFT JOIN Table2 ON table_2_id = table_1_id LEFT JOIN Table3 ON table_3_id = table_2_id LEFT JOIN Table5 ON table_5_id = table_4_id LEFT JOIN Table6 ON table_6_id = table_5_id

The test2 function seems to be printing out the Values from the test1 function's JOIN method as well as its own Values from the JOIN method.

Can someone please help.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Programit
  • 555
  • 1
  • 4
  • 10
  • what happens when you call test2 before test1 ? – Maximus2012 Aug 09 '13 at 20:42
  • 1
    Try `unset( $db )` between the queries. Without being able to see your Database class, my guess is that `BuildSelect()` is adding to the query each time you use the `$db` instance. Edit: It looks like whatever is happening in `Join()` needs to be reset for each invocation of `BuildSelect()` Would need to see your Database class to be more specific. – Chris Ostmo Aug 09 '13 at 20:44
  • The test2 function prints out - SELECT * FROM Table4 LEFT JOIN Table5 ON table_5_id = table_4_id LEFT JOIN Table6 ON table_6_id = table_5_id which is what I want it to do. – Programit Aug 09 '13 at 20:46
  • That last comment was to Maximus2012 – Programit Aug 09 '13 at 20:47
  • @ChrisOstmo - When you mean unset($db), Do you mean do it between the calls to the function and also How do I fix the BuildSelect query? – Programit Aug 09 '13 at 20:50
  • 1
    @Programit Post your `class.Database.php` file. The problem is in there. – Chris Ostmo Aug 09 '13 at 20:51
  • I will post it anyway but I think I have just fixed it - I created a method that reseted all variables to null or in the joins case an array and then I called that method after the BuildSelect method. – Programit Aug 09 '13 at 20:56
  • @ChrisOstmo should I still post it - it is about 600 lines? – Programit Aug 09 '13 at 20:59
  • @Programit You should still post relevant code. Otherwise your problem can't be solved. You could just add the relevant methods to your question and post the whole thing via pastebin or some similar service and provide a link. – Bart Aug 09 '13 at 21:27
  • Ok heres a link to pastebin just with the methods related to the question - http://pastebin.com/qniqMD0J – Programit Aug 09 '13 at 22:45

1 Answers1

1

Your addition of the Reset() method is what you needed.

All of your member variables except your join array were being reset on each call. For example:

$this->where = trim($where);

Which will replace your where member every time you call your Where() method. In contrast, you are doing this in your Join() method:

 $this->join[] = ...

Which adds a value to the end of the member array rather than rewriting what is already in the array. It has the same affect as using array_push() (see here)

One last suggestion I was going to make was to call Reset() at the end of BuildSelect() so that you wouldn't have to remember to do so in your queries, but it looks like you did that. My recommendation, however, would be to change this:

self::Reset();

To this:

$this->Reset();

In reality, either will work and PHP makes them functionally the same for what you are trying to accomplish, but other languages aren't as forgiving. :: is designed to be used to call static members, but earlier versions of PHP do the "automagic" thing of realizing that you are calling from an instantiated class and treating self:: the same as $this->

My understanding is that later versions of PHP (5.3+) refer to what is contained in scope at the point of definition, not the point of execution when you use self::. In most cases, you are going to want to reference the scope of the class at execution time. The two notes I have about that are:

  1. I have never allowed the use of self:: when $this-> is appropriate, so what I am saying here is only hearsay (I have never tested it to be true)

  2. Given the fact that your Reset() method doesn't do any form of logic on instantiated variables, it won't have any adverse affect on the specific code you are using. But it is best to start with good coding habits. Some time down the road, this practice WILL bite you if you continue coding in PHP.

PHP can be great when you are first learning, but the things that make it great for learning often come back to haunt you when you get to making any project of long-lasting consequence if you are not coding deliberately.

Community
  • 1
  • 1
Chris Ostmo
  • 1,202
  • 1
  • 10
  • 21