Welcome Guest, Not a member yet? Register   Sign In
Modelling Questions
#13

[eluser]m4rw3r[/eluser]
Why not use this code in the constructor of your model
Code:
function __construct($id = false)
{
    $CI =& get_instance();
    $this->db =& $CI->db;

    if($id !== false)
    {
       // fetch data here and populate the object
    }
}
and then you treat it like any PHP object?

Example:
Code:
$person = new Person(2);
echo $person->name;

When CI instantiates the model, $id will be false and it won't load any data. But if you instantiate it yourself, the $id will be set.
A load() method is also a good solution (and you can add a method like in_db() which returns true if the result is in database, ie. the Primary Key is set).


Messages In This Thread
Modelling Questions - by El Forum - 11-29-2007, 12:14 PM
Modelling Questions - by El Forum - 11-29-2007, 01:44 PM
Modelling Questions - by El Forum - 12-01-2007, 10:19 AM
Modelling Questions - by El Forum - 12-01-2007, 11:35 PM
Modelling Questions - by El Forum - 12-02-2007, 09:42 PM
Modelling Questions - by El Forum - 12-02-2007, 11:40 PM
Modelling Questions - by El Forum - 12-03-2007, 12:28 AM
Modelling Questions - by El Forum - 12-04-2007, 07:55 PM
Modelling Questions - by El Forum - 12-04-2007, 11:11 PM
Modelling Questions - by El Forum - 12-04-2007, 11:55 PM
Modelling Questions - by El Forum - 12-05-2007, 12:39 AM
Modelling Questions - by El Forum - 11-30-2008, 07:11 AM
Modelling Questions - by El Forum - 11-30-2008, 11:29 AM



Theme © iAndrew 2016 - Forum software by © MyBB