robsonrdasilva Posted June 28, 2011 Share Posted June 28, 2011 Hey, what's up? I've the fallowing function which return an array of Products with the type Product: public function get_products() { $sql = 'SELECT * FROM products'; $query = mysql_query($sql); return mysql_fetch_object($query, 'Product'); } But now I need return a list of Products and for each product a category, so I'm changing the sql: SELECT products.*, categories.name as category_name FROM products INNER JOIN categories ON products.id_category = categories.id_category How do I make it? My class Product doesn't have the fields of Category. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/240626-how-do-i-transform-the-result-of-an-inner-join-in-2-classes/ Share on other sites More sharing options...
TeNDoLLA Posted June 28, 2011 Share Posted June 28, 2011 Not exactly sure what you are trying, but I hope this example helps.. $sql = "SELECT products.*, categories.name as category_name FROM products INNER JOIN categories ON products.id_category = categories.id_category"; class Test { function getCategoryAndProduct() { echo $this->category_name . ' - ' . $this->product_name . '<br>'; } } $result = mysql_query($sql); while ($testObject = mysql_fetch_object($result, 'Test')) { $testObject->getCategoryAndProduct(); } Quote Link to comment https://forums.phpfreaks.com/topic/240626-how-do-i-transform-the-result-of-an-inner-join-in-2-classes/#findComment-1235954 Share on other sites More sharing options...
robsonrdasilva Posted June 28, 2011 Author Share Posted June 28, 2011 So, I didn't know that it works in php. You only created a class called Test without fields and PHP brought all fields of database. But, unfortunately I need create my class with all fields, the same fields of table Products. class Product { private $id_product; private $id_category; private $name; } class Category { private $id_category; private $name; } How do I match a Category with a Product? Do I need to create a field in class Product called category with type "Category"? Or in class Category I put an array of products? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/240626-how-do-i-transform-the-result-of-an-inner-join-in-2-classes/#findComment-1235974 Share on other sites More sharing options...
TeNDoLLA Posted June 28, 2011 Share Posted June 28, 2011 I can't really test anything right here now, coz dont have dev environment available but I think you could do something like this... class Category { private $id_category; private $name; } class Product { // Product related vars. private $id_product; private $name; private $category; // Holds category object. function __construct() { $this->category = new Category(); } } EDIT: Actually it would be more logical to have an array of product objects in a category class than the above.. Quote Link to comment https://forums.phpfreaks.com/topic/240626-how-do-i-transform-the-result-of-an-inner-join-in-2-classes/#findComment-1235982 Share on other sites More sharing options...
robsonrdasilva Posted June 28, 2011 Author Share Posted June 28, 2011 I think that I found a solution for it. I've created a class called ProductList that extends Product which has all variables that doesn't belong to class Product. For example, variable $category_name, a sum of values, etc. Can I make by this way? Or it is wrong? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/240626-how-do-i-transform-the-result-of-an-inner-join-in-2-classes/#findComment-1236024 Share on other sites More sharing options...
TeNDoLLA Posted June 28, 2011 Share Posted June 28, 2011 Sure you can do. All depends what you wanna achieve. Quote Link to comment https://forums.phpfreaks.com/topic/240626-how-do-i-transform-the-result-of-an-inner-join-in-2-classes/#findComment-1236028 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.