CodeIgniter Forums
What's wrong with this DataMapper code? - 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: What's wrong with this DataMapper code? (/showthread.php?tid=28219)



What's wrong with this DataMapper code? - El Forum - 03-04-2010

[eluser]Unknown[/eluser]
I've stared at it until my eyes hurt (literally) and I can't figure this one out. This uses DataMapper, and I'm trying to create a new Bookmark associated with the User. I've done this more times than I can count in the past, but today...

In my Controller's __construct() I have this, because I use "the current user" often enough that this makes sense:

Code:
$this->whoami = new User();
    $this->whoami->where('email', $this->session->userdata('loggedin') )->get();

And in one of the methods, I have this:

Code:
$b = new Bookmark();
    $b->lon = $_POST['lon'];
    $b->lat = $_POST['lat'];
    $b->save($this->whoami);

The result is always that the Bookmark entry is created in the "bookmarks" table, but no entry is created in the "users_bookmarks" table (nor bookmarks_users if I name it that). There's no errors, no warning, just a Bookmark not associated to a User.

The classes are really vanilla, not even methods. Here they are:

Code:
class User extends DataMapper {
var $table = 'users';
var $has_many = array('bookmark');

function __construct() {
    parent::__construct();
}

}

Code:
class Bookmark extends DataMapper {
var $table = 'bookmarks';
var $has_one = array('user');

function __construct() {
    parent::__construct();
}

}

The tables are very simple as well:

users -- id, email, password
bookmarks -- id, lon, lat, title, description
users_bookmarks -- id, bookmark_id, user_id

What I have tried:

* Replacing $b->save($this->whoami) with $this->whoami->save($b) to no effect.
* Renaming users_bookmarks to bookmarks_users
* Not using a 'id' column in users_bookmarks

Probably something really simple, but it's getting late and I've already spent over 2 hours poring over these same 30 lines of code.


What's wrong with this DataMapper code? - El Forum - 03-05-2010

[eluser]ntheorist[/eluser]
perhaps in your method try a more straightforward
Code:
$b->save();
$this->whoami->save($b);

are you sure that $this->whoami always retrieves a result? checking if($this->whoami->exists()) for instance.. relationships need both models to have non-null ids

also turn your output profiler on, what queries is it generating? plus which version of datamapper are you using?



_n_


What's wrong with this DataMapper code? - El Forum - 03-05-2010

[eluser]Unknown[/eluser]
[quote author="ntheorist" date="1267810430"]perhaps in your method try a more straightforward
Code:
$b->save();
$this->whoami->save($b);
[/quote]

Yeah, did that as per my older code. Nada.

But, I switched over to DataMapper OverZealous anyway, as I wanted to dispense with the joining table. And then it worked.

[quote author="ntheorist" date="1267810430"]are you sure that $this->whoami always retrieves a result?
[/quote]

Yep, positive. Other code already checked $this->whoami->id and exited (not logged in) a while ago.


[quote author="ntheorist" date="1267810430"]also turn your output profiler on, what queries is it generating?/quote]

A profiler? That will show me actual queries? Please tell me more! I have missed that more than any other potential feature!


What's wrong with this DataMapper code? - El Forum - 03-06-2010

[eluser]ntheorist[/eluser]
Quote:A profiler? That will show me actual queries? Please tell me more! I have missed that more than any other potential feature!

hehe for a sec i thought u were being sarcastic

just set $this->output->enable_profiler(TRUE); typically in your controller's constructor.

see http://ellislab.com/codeigniter/user-guide/general/profiling.html

gl,

n