CodeIgniter Forums

Full Version: the best practice of writing active record
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
Code:
function get_product($options=array())
    {
        $this->db->select("t_product.product_id as product_id",false);
        
        //define what to show
        if(isset($options['qty_purchased']))
        {
            $this->db->select("(prd_suggested_retail_price*".$options['qty_purchased'].") as subtotal",false);        
        }
        if(isset($options['get_supplier']))
        {
            $this->db->join("t_supplier","t_product.supplier_id=t_supplier.supplier_id");
            $this->db->select("sup_name");
            $this->db->select("sup_description");
        }
        if(isset($options['get_publisher']))
        {
            $this->db->join("t_publisher","t_product.publisher_id=t_supplier.publisher_id");
            $this->db->select("sup_name");
            $this->db->select("sup_description");
        }
        if(isset($options['get_voucher']))
        {
            $this->db->join("t_voucher","t_voucher.product_id=t_product.product_id");
            $this->db->select("voucher_id");
            
                    
        }
        if(isset($options['get_seller']))
        {
            $this->db->join("t_seller_price","t_seller_price.product_id=t_product.product_id");
            $this->db->select("seller_user_id");
            
        }
        $this->db->from("t_product");

if(isset($options['some_selection_thing']))
{
$this->db->where('some rules');
}

//----------and other selection criteria

I have this code written in my current model.....but someone suggest me to break this model down into several functions....rather than using a parameterized function like my current one.
I used this kind of function because I only need to access one function and make sure that only one query is executed everytime.....


Need opinion.....Thank you very much to all of you....

El Forum

[eluser]toopay[/eluser]
[quote author="wolfgangerstorm" date="1306488402"]
I used this kind of function because I only need to access one function and make sure that only one query is executed everytime...[/quote]

Your code is indeed weird, but if it can serve your (unique) needs, then go with them. I have been see more bizzare structure then yours, and as long as they work as expected, then it fine(IMO). I just suggest you, to only refactor it(to make them clean), not change the behaviour.

El Forum

[eluser]Unknown[/eluser]
whats your suggestion as on how to refactor this kind of code?

basically what i need is i need to get product data, but sometimes....I need some other data which exist in different table.....my idea is that i can call single function...only with different parameters and then I will get the query and the result I want.....