CodeIgniter Forums
Best way for two models in one controller? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Best way for two models in one controller? (/showthread.php?tid=91888)



Best way for two models in one controller? - trunky - 10-30-2024

Hi everyone,
My question is about following: I have two tables, products and products_description. First holds the general information like price and article number of the products, the description the textual description for each language, so 1:n (like 1 product can have many descriptions in different languages).
So I have controller "Products" and two models for each table.
When I like to query ALL product info in one call.... like general information plus description and details, what do I do best?


RE: Best way for two models in one controller? - ozornick - 10-30-2024

Example. Or create new class ProductManager with new methods.
PHP Code:
$products model(ProductsModel::class)->withDescription() 



RE: Best way for two models in one controller? - trunky - 10-31-2024

I am probably not experienced enough for this, but how do I define withDescription? Is it a model function that includes the products_description model?


RE: Best way for two models in one controller? - ozornick - 10-31-2024

PHP Code:
class ProductModel extends Model
{
    public function withDescription() {
        return $this->join('products_description''products.id = products_description.product_id''LEFT')
            ->where('products.id'1);
    }





RE: Best way for two models in one controller? - trunky - 10-31-2024

ok so I wasn't wrong with this. Thank you!