PDO fetch single class not working

Yuri Blanc

i am making a database abstraction class that binds objects like an ORM. I'm having issue with a particular case, fetching a single row and binding to a class. While the same is working well with fetchAll() i can't figure out why using fetch(PDO::FETCH_CLASS) the object returns null. if i use PDO::FETCH_LAZY it works, but isn't a correct binding to the passed class. Here the code. The Database() class is connects to db using PDO. Products() is a class made of public attributes with same name of tables. The controller:

public function editProducts($params) {
    $products = new Products();
    $db = new Database ();
    $id = array_keys($params);
    $products = $db->findById($products, $id[0]); // auto Bind object fetched=no and POST params?
    $this->template = new Template();
    $this->template->renderArgs("product", $products);
    $this->template->renderArgs("page_title", "Edit product " . $products->title);
    $this->template->render(get_class($this), "editProducts");

}

The DB class:

public function findById($object,$id) {
    try {
    $table = $this->objectInjector($object);
    } catch (Exception $e) {
        if (APP_DEBUG) {
            d($e->getTrace());
        }
        return;
    }
    $statement = "SELECT * FROM $table WHERE id=:id";
    $this->stm = $this->pdo->prepare($statement);
    $this->bindValue(":id",$id);
    return $this->fetchSingleObject($object);
}

the method that abstract fetch:

public function fetchSingleObject($object) {
    $this->execute();
    $this->stm->setFetchMode(PDO::FETCH_CLASS, get_class($object));
    return $this->stm->fetch(PDO::FETCH_CLASS);
    //return $this->stm->fetch(PDO::FETCH_LAZY); this works!
}

I missed something? the fetchAll() works nicely in this way:

 public function fetchObjectSet($object) {
        $this->execute();
        $this->stm->setFetchMode(PDO::FETCH_CLASS, get_class($object));
        return $this->stm->fetchAll(PDO::FETCH_CLASS);
    }

Thank you so much. PS: some methods like $this->execute() are just abastractions to pdo->statment method since pdo and stm are db class instance variables.

Yuri Blanc

I found the answer to the question by myself, i post the answer for everyone.

Instead of using directly PDO::FETCH_CLASS, $Class, i switched using setFetchMode() passing PDO_FETCH_INTO, new $Object instance.

This return correctly new instance of given object (with object methods and fields). Works well with public attributes and overloaded constructors.

The previously statement "findAll() works" wasn't true, i was returning somehow like FETCH_OBJ, an object representation of the database table.

Here the solution:

public function fetchSingleObject($object) {
    $this->stm->setFetchMode(PDO::FETCH_INTO, new $object());
    $this->execute();
    return $this->stm->fetch();
}

Return a new instance of passed in object.

Works also as fetchAll()

EDIT:

public function fetchObjectSet($object) {

    $this->execute();
    return $this->stm->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, get_class($object));
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PDO fetch single class not working

From Dev

php pdo fetch not working with functions

From Dev

Fetch Array from class with PDO

From Dev

Opposite To PDO::FETCH_CLASS

From Dev

PDO fetch and fetchAll with conditional statement not working

From Dev

Is there an INSERT or UPDATE equivalent to PDO::FETCH_CLASS?

From Dev

How to fetch the first row as object of class? (PDO)

From Dev

Casting Issue in PHP PDO::FETCH_CLASS

From Dev

multiple PDO insert with single statement not working

From Dev

Fetch a single result from a MySQL Database using PDO

From Dev

PDO: how do I fetch a single record by column name

From Dev

PDO::FETCH_CLASS without passing class name as a parameter

From Dev

Having PDO::fetch(PDO::FETCH_CLASS) return a null-valued instance of the object instead of a bool(false)

From Dev

Can PDO::FETCH_CLASS mode be used for one row (not fetchAll)?

From Dev

PDO FetchAll Fetch_Class not returning all rows/objects

From Dev

Using PDO::FETCH_CLASS to bind table fields to object properties

From Dev

PDO FETCH_CLASS when fields and columns have different names

From Dev

How to store single value or entire array from fetch(PDO::FETCH_ASSOC) in a variable?

From Dev

Async and a single instance class working with Models

From Dev

PDO fetch issue

From Dev

fetch multiple select in pdo

From Dev

PDO fetch into existing object

From Dev

myqsli -> fetch TO pdo

From Dev

PDO Fetch with while and foreach

From Dev

PDO fetch array as row

From Dev

Does the contrary of PDO::FETCH_CLASS exists or mapping an object back in the database

From Dev

PDO fetch(PDO::FETCH_ASSOC) not returning values

From Dev

PHP PDO FetchAll vs Fetch

From Dev

PDO Fetch, Execute, statements etc

Related Related

HotTag

Archive