0

I am trying to implement PHP chaining method in my web development project. But I seem can't get it right.

class foo extends base{


    public $query = null;
    public $item = array();

    public function __construct($connection){
        parent::__construct($connection);

    }

    public function select($query){
         $this->query = $query;
         return $this;
    }

    public function where($query){
         $this->query = $query;
         return $this;
    }

    public function __toString()
    {
        $this->item = $this->connection->fetch_assoc($this->query);

        return var_export($this->item, true);
    }


}

$connection = new db($dsn = 'mysql:host=localhost;dbname=xxx',$username = 'xxx',$password = 'xxx');
$foo = new foo($connection);

$select = $foo->select("SELECT * FROM page")->where("page_id = 10 ");
print_r($select->item);

the result I get,

Array
(
)

But I should get a row of data. Just like I normally do it in this way,

class boo extends base{

    ...

    public function select() {

        $sql = "
            SELECT * 
            FROM page
            WHERE page_id = ?
        ";

        $item = $connection->fetch_assoc($sql,array(1));
        return $item;

    }
}

What have I missed in my chaining method?

EDIT:

class base
{
    protected $connection = null;

    public function __construct($connection)
    {
        $this->connection = $connection;

    }


}

if I just print $select,

print_r($select);

the result,

foo Object
(
    [query] => where page_id = 10 
    [item] => Array
        (
        )

    [connection:protected] => db Object
        (
            [connection] => PDO Object
                (
                )

            [dsn] => mysql:host=localhost;dbname=xxx
            [username] => xxx
            [password] => xxx
        )

)
Run
  • 54,938
  • 169
  • 450
  • 748

2 Answers2

0

Try like

$select = $foo->select('*')->from('page')->where('page_id','10');
print_r($select);
GautamD31
  • 28,552
  • 10
  • 64
  • 85
0

With the following call all you're doing is setting and replacing the value of foo class's $query member variable:

$select = $foo->select("SELECT * FROM page")->where("page_id = 10 ");

So, $foo->select("SELECT * FROM page") sets the value of $query member variable, then ->where("page_id = 10 ") replaces that value.

Also, you are not executing the query.

vee
  • 38,255
  • 7
  • 74
  • 78
  • How is your `base` implemented? Can you put that in your question as well? Try printing just the select like `print_r($select)` instead of `print_r($select->item)` – vee Aug 24 '13 at 07:42
  • the `base` is just to keep the property of the db connection. please have a look at my edit above. thanks – Run Aug 24 '13 at 07:47
  • 1
    @lauthiamkok, now with your latest update, you see where the problem is? With this approach you need to either append the where clause to your existing `$query` in the `where` method, or maintain different variables for all the parts of a query e.g. select, where, group by, limit and so on which would then be merged together into one single query before execution. – vee Aug 24 '13 at 07:59
  • Why not just send the full query instead of separating them? – vee Aug 24 '13 at 07:59
  • vinodadhikary, thanks. in my real project, I use full query. but I just want to test how to work it out with a chaining method as it seems many frameworks do so. and I want to learn something new... – Run Aug 24 '13 at 08:02
  • how can I `append the where clause to the existing $query`? – Run Aug 24 '13 at 08:03
  • 1
    Your chaining is working fine as you are returning `$this`. As far as `appending where clause` is concerned the simplest case would be to do `$this->query .= $query` in your `where` method. But you should also handle various conditions for example, "What if `where` gets called before `select`" – vee Aug 24 '13 at 08:06
  • thank you so much! it now works! and thanks for the tip about handling various conditions. – Run Aug 24 '13 at 08:12