CodeIgniter Forums
DataMapper 1.6.0 - 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: DataMapper 1.6.0 (/showthread.php?tid=11358)



DataMapper 1.6.0 - El Forum - 02-18-2009

[eluser]DavidZB[/eluser]
Hi!

Well, I've tried to read all of the post just to find an answer... but there are 56 pages!!. I couldn't find the answer, so here it goes:

I'm making a Bug Tracker module for a CI app... I'm using DataMapper. I have my "bugs" table and my "priorities" table. The idea is that a bug can be updated whenever I want, just changing the priority, but, of course, keeping track of the priorities it had all along. There is, of course, the bugs_priorities table. there's an id, bug_id and priority_id.... but I need to add a date field, so I can know when happened that update....

bugs
- id
- name
- details

priorities
- id
- name (i.e: Normal, Critical, Trivial...).

bugs_priorities
- id
- bug_id
- priority_id
- date <- Here is my problem

I need to know, in example, the priorities that a bug had between two dates...

How can I do that? is there another way?

Thanks...

PD: Sorry about my english!


DataMapper 1.6.0 - El Forum - 02-19-2009

[eluser]OverZealous[/eluser]
@DavidZB
There is no way to get information from the join tables in DataMapper, currently. You need to use a third Object to store the information, like this:

bugs, priorities stay the same.

prioritydates
fields: id, date
has_one: priority, bug

bugs_prioritydates
id, bug_id, prioritydate_id

priorities_prioritydates
id, priority_id, prioritydate_id

Code:
$bug = ...
$pds = $bug->prioritydate->where("date >=", $date1)->where("date <=", $date2)->get();
foreach($pds->all as $pd) {
    $date = $pd->date;
    $priority = $pd->priority->get();
}

Now, in the current implementation, you have to use the extra join tables, which is rather ugly, and makes certain complex queries difficult. I have been working on a version that (among other things) allows has_ones to use traditional, in-table fields. You can try it out a few pages back, it is almost a drop-in replacement for the official version. If you use that, then you can drop the join tables, and just make your prioritydate table:

id, date, priority_id, bug_id

And with the testing version, you can run a query like this:
Code:
$bug = ...
$pds = new PriorityDate();
$pds->where_related($bug)
$pds->join_related('priority', array("id","name",etc...));
$pds->get();
foreach($pds->all as $pd) {
    echo("On {$pd->date}, the priority was {$pd->priority_name}");
}

The testing code is definitely experimental, however, so you might not want to use it in a production system yet.


DataMapper 1.6.0 - El Forum - 02-19-2009

[eluser]macigniter[/eluser]
[quote author="Murodese" date="1233341217"]
Code:
class Creator extends DataMapper
{
  var $table = 'users';

  var $has_many = array('file');
  
  // ...
}

class User extends DataMapper
{
  var $table = 'users';

  var $has_many = array('file');

  // ...
}

class File extends DataMapper
{
  var $table = 'files';

  var $has_one = array('creator');
  var $has_many = array('user');

  // ...
}

And join tables would become join_files_users and join_files_creators.[/quote]

I am just now implementing this and it is not working for me Sad

I get the following error:

Code:
Error Number: 1054

Unknown column 'creator_id' in 'where clause'

SELECT * FROM (`join_files_users`) WHERE `file_id` = 9 AND `creator_id` = '2'

Why doesn't it use 'join_files_creators'?

This is my query:

Code:
$c = new Creator();
$c->get_by_id($user_id);
    
$f = new File();
$f->name = $this->input->post('file_name');
...
$f->save($c);



DataMapper 1.6.0 - El Forum - 02-19-2009

[eluser]tdktank59[/eluser]
Check your mysql driver

In the manual go to troubleshooting and should be the first thin there


DataMapper 1.6.0 - El Forum - 02-19-2009

[eluser]macigniter[/eluser]
I already did... that's not it Sad


DataMapper 1.6.0 - El Forum - 02-19-2009

[eluser]tdktank59[/eluser]
I figured it out lol

Because the table in the Creator model is set to users so it will join a users table not the creator table...

Change $table = "creator"; and it should join the right table that you want.


DataMapper 1.6.0 - El Forum - 02-19-2009

[eluser]macigniter[/eluser]
Actually there is no "creators" table. The situation is as follows:

There's a "users" table and a "files" table. When a new file is added via the admin panel both the "creator" (which is a user) and the relation of the file to the users (managing rights) is stored.

So I need to save a relation to:
1. the user who created the file
2. the users who are eligible to view the file


DataMapper 1.6.0 - El Forum - 02-19-2009

[eluser]tdktank59[/eluser]
ok then... creator then has to extend users instead of datamapper

Check this page it might solve your problem

http://stensi.com/datamapper/pages/relationtypes.html

This is a self referencing type issue.


DataMapper 1.6.0 - El Forum - 02-19-2009

[eluser]macigniter[/eluser]
I read that, too. Tried exactly what was explained, but just can't get it to work Sad Can anyone help with a code example?

EDIT: I am also not so sure if this is a self-referencing issue. Since I really want to reference Users <> Files and Users (as Creator) <> Files. I just can't use the join_files_users table twice...

It's not that a User has_one or has_many Creators. A file has both a relation to users and a creator.

I'm lost Sad


DataMapper 1.6.0 - El Forum - 02-19-2009

[eluser]macigniter[/eluser]
Maybe the only solution to this is adding another CREATORS table?

USERS
id
...

FILES
id
...

CREATORS
id

join_files_users
join_creators_files
join_creators_users

It's kind of redundant since creators are in fact users, but I have no clue how to solve it otherwise...