Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I simply return objects in PDO?

Tags:

object

php

pdo

Trying out PDO for the first time.

$dbh = new PDO("mysql:host=$hostname;dbname=animals", $username, $password);

$stmt = $dbh->query("SELECT * FROM animals");
$stmt->setFetchMode(PDO::FETCH_INTO, new animals);

foreach($stmt as $animals)
{
    echo $animals->name;
}

If i skip the setFetchMode() method, then I need to call $animals["name"] which I do not want.

But I do not want to call the setFetchMode() for each query I do.

Is there a way to set the default FetchMode ? Or some other method to make the query() return objects with one global setting.

like image 793
Ólafur Waage Avatar asked Apr 14 '09 14:04

Ólafur Waage


People also ask

How do I return data from a result set in PDO?

To return a single row from a result set as an array or object, call the PDOStatement::fetch method. To return all of the rows from the result set as an array of arrays or objects, call the PDOStatement::fetchAll method. By default, PDO returns each row as an array indexed by the column name and 0-indexed column position in the row.

What is the use of fetch() function in PDO?

Fetches the next row and returns it as an object. This function is an alternative to PDOStatement::fetch () with PDO::FETCH_CLASS or PDO::FETCH_OBJ style. When an object is fetched, its properties are assigned from respective column values, and afterwards its constructor is invoked. Name of the created class.

How to create an object from a query in PDO?

Like any other database extension, PDO can create instances of the existing classes right from the selected data. But, unlike other extensions, PDO offers many features for the powerful and flexible object manipulation. To create a single object from the query results you have two options. You can use the either a familiar fetch () method:

What is the result of lastinsertid in PDO?

If a sequence name was not specified for the name parameter, PDO::lastInsertId () returns a string representing the row ID of the last row that was inserted into the database. If a sequence name was specified for the name parameter, PDO::lastInsertId () returns a string representing the last value retrieved from the specified sequence object.


2 Answers

Perhaps you could try extending the PDO class to automatically call the function for you... in brief:

class myPDO extends PDO
{
   function animalQuery($sql)
   {
     $result = parent::query($sql);
     $result->setFetchMode(PDO::FETCH_INTO, new animals);
     return $result;
   }

//   // useful if you have different classes
//   function vegetableQuery($sql)
//   {
//     $result = parent::query($sql);
//     $result->setFetchMode(PDO::FETCH_INTO, new vegetables);
//     return $result;
//   }
}

$dbh = new myPDO("mysql:host=$hostname;dbname=animals", $username, $password);

$stmt = $dbh->animalQuery("SELECT * FROM animals");

foreach($stmt as $animals)
{
    echo $animals->name;
}
like image 60
Martin Avatar answered Oct 25 '22 03:10

Martin


Since PDO would need to know what object you want to fetch into, you would need to specify it manually. But of you just want to use the object to retrieve the data rather than an array and do not care if its not an animal object, you can use anonymous objects by default when you set the attribute after the connection string which could be done in a wrapped constructor

  $connection = new PDO($connection_string);
  //PDO::FETCH_OBJ: returns an anonymous object with property names that correspond to the column names returned in your result set 
  $connection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); 

Then all queries will return objects. Though it's not exactly what you want its close.


You could also inject the data into your animal class:

while($dataObj = ...) {
 $animal = new Animal($dataObj);
}

If you look at the query function it is possible to change some options by passing extra parameters: http://www.php.net/manual/en/pdo.query.php I haven't tested it but it looks like it gets you close to what you want

like image 5
SeanDowney Avatar answered Oct 25 '22 04:10

SeanDowney