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

[eluser]dreamnid[/eluser]
Yes, that's what I envisioned the getId( ) for.

My major quip is that it is a separate function so if you don't call the setId function, then everything else will be blank.

I guess I can either see if it returns blank, or have each get function check if the id has been set and exists in the database... which complicates the code some.

Sigh, just more thinking that needs to be done.

Thanks though :-)
#12

[eluser]MirceaBotez[/eluser]
Hi, I ran into the same issue with extending the Model class.
I've also posted to the bug tracker.

Unfortunately (or fortunately), the source for Loader.php has changed and the line 159 doesn't do the same thing as suggested in the "hack" above.

Anyone know the status of this issue or if another workaround exists?
#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).




Theme © iAndrew 2016 - Forum software by © MyBB