Welcome Guest, Not a member yet? Register   Sign In
Datamapper wrongly reconstructing relationships?
#1

[eluser]Dracos[/eluser]
I just spent an hour trying to figure out why saving a relationship didn't work. I have the following models (reduced for brevity):

Code:
class locale extends DataMapper {
    $has_many = array('display_prefs');
}

class display_prefs extends DataMapper {
    $table = 'display_prefs';
    $has_one = array('locale', 'user');

    // table fields: locale_id, user_id
}

I was working in a controller to set the locale_id in display_prefs:

Code:
$dp = new display_prefs();
$dps = $dp->where(array('user_id' => $this->session->user->id))->get()->all;

foreach ($dps as $p) {
    $p->locale_id = $new_locale->id;
    $p->save();
}

The query is OK, it produces the expected result.

First I was getting "Fatal error: Class name must be a valid object or a string in libraries/datamapper.php line 2525" that didn't make any sense.

Then I shuffled the controller code around to what you see above and got this error stack:

Code:
An Error Was Encountered
DataMapper Error: 'display_pref' is not a valid parent relationship for locale.  Are your relationships configured correctly?
Full backtrace:
Filename: core/Common.php
Line Number: 309
Filename: /[some path]/application/libraries/datamapper.php
Line Number: 2843
Filename: /[some path]/application/libraries/datamapper.php
Line Number: 1379
Filename: /[some path]/application/controllers/account.php
Line Number: 167
Filename: /[some path]/application/core/MY_Controller.php
Line Number: 534
Filename: core/CodeIgniter.php
Line Number: 325
Filename: /[some path]/htdocs/index.php
Line Number: 209

There is no such model 'display_pref', nor is that a relationship key. Somewhere DataMapper is internally and wrongly determining that the related model is 'display_pref' instead of 'display_prefs'.

This is CI 2.0.3 and DM 1.8.2.
#2

[eluser]WanWizard[/eluser]
Datamapper expects model names to be singular, and tables to be plural. If no class name is defined in the relationship, it will use CI's inflector helper to do the transformation. This is documented in the models docs.

If you want to 'voilate' the rule, you have to switch to an advanced relationship definition and define the class name manually.
#3

[eluser]Aken[/eluser]
DataMapper is also a third-party addon, and problems like these should be addressed in that addon's particular thread. This section of the forum is meant for bugs relating to CI itself.




Theme © iAndrew 2016 - Forum software by © MyBB