PDO FETCH_INTO Factory?

NathanG

New Member
I have a factory called \[code\]ProductFactory\[/code\]I can create a product like so:\[code\]$product = ProductFactory::getProduct($id);\[/code\]Now depending on what type of product we are getting, this function may return a class of type \[code\]Product\[/code\] or a descendant or \[code\]Product\[/code\].But using the method above, the \[code\]$product\[/code\] class would be responsible for connecting to the database and retrieving its data.So if I were to select 20 products to be displayed on a category list, I would have to first get a list of all the ID's then call the above function 20 times. This would be 21 calls to the database.So I added another function to my factory like so:\[code\]$product = ProductFactory::createProduct($data);\[/code\]This does the same thing, except you pass in the data from the database, saving the \[code\]$product\[/code\] class from having to make the trip. (depending on what data you pass, the factory will return the right type of class).So now I want to make one call to select all products data and \[code\]FETCH_INTO\[/code\] my factory to generate a $product object for each row.How can I do that?updateHere is what I tried that did not work:\[code\]$stmt->setFetchMode(PDO::FETCH_INTO,ProductFactory::createProduct);foreach($stmt as $product){ echo get_class($product) . '<br>';}\[/code\]
 
Back
Top