Reputation: 336
I am breaking my head over this problem the whole day already, and I cant figure it out why my code is behaving like this. I am using PDO to get data from the database and when I var_dump
the $stmt->fetchAll()
I get the expected results(an array with 2 objects) but when I call the method and var_dump
it the result is the object that calls the method.
The user class derives from the ORM class
The code is as follows:
In index.php
User::find(1);
In ORM.php
protected static $table_name;
private $query;
public function __construct()
{
$this->query = new Query(static::$table_name);
}
public static function find($id, $columns = "*")
{
$user = User::get($columns, get_called_class(), PDO::FETCH_CLASS);
return $user;
}
public static function findAll($columns = "*")
{
return User::get($columns);
}
/**
* Magic method to call methods
*/
public function __call($method, $args = array())
{
call_user_func_array( array( $this->query, $method ), $args);
return $this;
}
/**
* Magic method to call static methods
*/
public static function __callStatic($method, $args = array())
{
$static = new static;
call_user_func_array( array( $static->query, $method ), $args);
return $static;
}
and in Query.php
public function get($columns = "*", $class_name = "", $fetch_mode = PDO::FETCH_ASSOC)
{
$stm = DB::prepare("select * from user");//the DB class is working properly
$stm->execute();
$fetch = $stm->fetchAll();
var_dump($fetch); // This prints the correct array with 2 users in it
return $fetch; // doesnt return somehow
}
Thanks in advance I have no idea how to fix this, as I am looking at this problem for the entire day already
Upvotes: 0
Views: 81
Reputation: 6292
Robert
User::find(1);
find($id, $columns = "*")
{
$user = User::get($columns, get_called_class(), PDO::FETCH_CLASS);
return $user;
In your call to find you are passing 1 which I guess is $id
but in the get you are passing $columns
which means to me you are not passing $id
In summary, to me it looks like you are passing 1 but not doing anything with it ?
Let me know if this fixes it ?
Upvotes: 1