views:

59

answers:

5

I'm new to "Object Oriented" PHP, but have managed to figure most things out so far. I'm building a website where users can register an account and then add, let's say, hobbies onto their profile.

I've managed to figure out creating the class, object, storing a single user in the database, and retrieving a single user from the database - creating a new object from the associative array that is returned from the SQL "select" query.

Where I am now getting stuck is when I need it to return multiple rows (records) from the database. For example, a user may have 7 hobbies; the SQL query returns all 7 rows into an associative array (with fields e.g. hobby_id, hobby_name, hobby_created), but how then do I make each one of those rows/hobby records into its own object?

I have tried searching all sorts of terms but I don't know if I'm just missing the buzz word that I need to search for. If anyone could please let me know the best way to go about this I would be eternally greatful.

Many thanks, Steve

A: 

First you'd have to actually create an object to handle those hobbies. You can't just sprinkle Acme OOP sauce on some data and have it magically turn into an object. An object is both data and functions (methods, actually) to work with that data. So, first figure out what you want your code to do with those hobbie data bits and work from there.

Marc B
A: 

When you have an array of hobbies, create the hobby objects in a foreach.

$hobby = Array();
foreach ($query->results as $row)
  $hobby = new Hobby($row['person_id'], $row['hobby']...

or perhaps a hash of objects.

johnny
Thanks, I think I will give this one a go and see how I get on.
SirMalius
Quick question - wont the code above just end up with $hobby being an object derived from the final row in the array? Surely it will just keep iterating through and overwriting $hobby with the next object? Or does the "Array();" syntax make PHP realise that each new Hobby object is added to the array rather than replacing?
SirMalius
I believe it needs push added to it.
johnny
A: 

Is there any reason the hobbies needs to be its own object? It seems like it functions fine as a hash/array. If you really want to create an object, though, then you can create a hobby class that stores the hobby name/created date and has getters for them. Is there any other data to associate with the hobby, or calculations that need to be done? If so, you can create methods of the Hobby class to do this for you.

tandu
+3  A: 

You can either loop through the result and create hobbies from the data or if you're using PDO you can use:

$stmt->fetchAll( PDO::FETCH_CLASS, 'Hobby' );

This will create a Hobby class for each row and populate properties with the columns from the query.

From http://www.php.net/manual/en/pdostatement.fetch.php

PDO::FETCH_CLASS: returns a new instance of the requested class, mapping the columns of the result set to named properties in the class. If fetch_style includes PDO::FETCH_CLASSTYPE (e.g. PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE) then the name of the class is determined from a value of the first column.

Note: if you're using fetch() and not fetchAll() you have to use setFetchMode() before calling fetch()

$stmt->setFetchMode( PDO::FETCH_CLASS, 'Hobby' );
Galen
A: 

You can easily typecast an associative array to object with properties, like:

// $obj is instance of stdClass
$obj = (object)array('hobby_name'=>'name');

Of course the question wasn't that clear for what you need objects, or if they need to be of any explicit class (@Galean's answer).

mhitza