CodeIgniter Forums
simple question with DMZ - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: simple question with DMZ (/showthread.php?tid=27465)



simple question with DMZ - El Forum - 02-11-2010

[eluser]Unknown[/eluser]
Hi everybody:
i'm working on my first CI project and I decided to use DMZ as well - just to keep things simple! I've run into a problem I don't really understand... I have three models: document, user and line. Relationships are defined as follows:

Code:
class Document extends DataMapper {

    var $table = 'documents';
    var $model = 'document';

    var $has_one = array('user');
...
Code:
class User extends DataMapper {

    var $table = 'users';
    var $model = 'user';

    var $has_one = array('line','document');
...
and finally
Code:
class Line extends DataMapper {

    var $table = 'lines';
    var $model = 'line';

    var $has_many = array('user');
...

Then in my controller I pass an $id for user, so that I can load:

Code:
$user = new User($id);

Easily enough, I can get the line for user by setting:

Code:
$user->line->get()

And now I would like to get the document for the user (which is a one-to-one relation which I have set up with an auxiliary table, BTW), but when I do this:

Code:
$user->document->get();

I get an error: "Fatal error: Call to a member function get() on a non-object".

Does anybody know what I'm doing wrong?


Thanks for the help,
Antonio


simple question with DMZ - El Forum - 02-11-2010

[eluser]12vunion[/eluser]
The most common reason is that the relationship isn't defined in both models. But yours is. Chances are that there's a problem with your Document model. Could be that you might have already loaded another class called Document. Try calling it the other way to be sure:
Code:
$document = new Document();
$document->where_related($user)->get();

If that errors out, then you have a problem with your Document model. Try just calling it Doc.

Also, you don't need to set the $model/$table variables for every model. That's only in very odd cases (like db->people, model->person... actually, that one probably works but you get it) where it can't easily pluralize the model name. I've had problems in DMZ where I set them, but didn't need to. Probably fine, but I'm just letting you know.