Hi.
I think I get the OOP paradigm at its basic level but I'm having a hard time conceptualizing the proper way to lookup records in a database. I suspect this is because I don't really get OOP as much as I think I do...
The application I'm trying to write is a shopping cart because it has a lot of good candidates for objects. The object I'll be working with is a product (yes, this is very primitive).
<?php
class Product
{
public $id = 0;
public $name = '';
public $price = 0;
function __construct($id)
{
// if $id exists try to retrieve
// a product with $id
// else create a new product
// and set $this->id
}
}
$product = new Product();
echo $product->name;
?>
Easy enough. But now I want to look up multiple products (for a search results page) and I'm not sure how to handle it.
My inclination is to write another class that takes some input and performs a SQL query that just returns a list of product ids. I would then loop through those results and create a product object for each one. I could then loop through the array of objects and build my search results page.
I'm not sure why but this just doesn't feel like the right way. Seems like too much work because first I'm doing a query to just get a list of product ids and then I do more queries to get each product's data. It'd be a lot faster if just did one query that returned all the data I need.
What's the right thing to do? Something else perhaps?