CodeIgniter Forums
DataMapper 1.6.0 - Printable Version

+- CodeIgniter Forums (
+-- Forum: Archived Discussions (
+--- Forum: Archived Libraries & Helpers (
+--- Thread: DataMapper 1.6.0 (/showthread.php?tid=11358)

DataMapper 1.6.0 - El Forum - 03-06-2009

With DataMapper, you never have to worry about the relationship (join) tables! That's the beauty of ORM!

Simply delete one of the two related items, and all relationship data is automatically deleted!

Also, if you want to just delete the relationship, you call:
// member and region are already loaded
// OR this also works

What's great about something like DataMapper is that you can worry much less about the database, SQL queries, and tables. Once the structure is set up, a lot of the work is automagically handled.

DataMapper 1.6.0 - El Forum - 03-06-2009

[eluser]Billy Khan[/eluser]
So i can do

$members = new Members();

and it will automagically delete all from the relationship table?

DataMapper 1.6.0 - El Forum - 03-06-2009

Yes, except you have to get them first:
$members = new Members();

I'm pretty sure that will work.

Of course, that isn't a very efficient way to completely truncate a table. If you are doing this often, you might want to just run the queries by hand, I think you can call truncate() using normal CodeIgniter ->db methods.

DataMapper 1.6.0 - El Forum - 03-06-2009

[eluser]Billy Khan[/eluser]
Blimey... this thing is better than i thought. Smile


DataMapper 1.6.0 - El Forum - 03-06-2009

I am having an issue that I have no idea how to handle.

I'll use sports to help explain my issue. Say I have a league,team, and player models

league has many teams
teams have one league
teams have many players
players have one league
players have one team

$league = new League();

//this will give me all the teams in a league

//this will give me players for the first team, but not all teams

How can I fill all the teams with their players instead of just the first selected team?

I want to avoid doing the code below
Quote:foreach(&$league->team->all as $t)

Am I doing or thinking about this completely wrong?

DataMapper 1.6.0 - El Forum - 03-06-2009

There isn't currently a way to do what you want. In reality, that would be somewhat complex SQL code, anyway, at the very least requiring a subquery.

When you write $team->player->get(), you are asking DataMapper to give you all players that belong the team specified by $team.

In other words, the foreach loop is your best bet.

If you are feeling risky, you could try the poorly documented upgrade to DataMapper I posted a while back. It contains the ability for in-table has_one joins, meaning your Player table could have 'league_id' and 'team_id' on it, and then you wouldn't need the join tables at all. Then your query looks something like:
$player = new Player();
$player->where_in('team_id', array($team1->id, $team2->id, etc))->get();

Also, if you are including a join to leagues for players, why not just do this?

DataMapper 1.6.0 - El Forum - 03-06-2009

That makes sense. I just need to make foreign keys for the id's i need to relate back to. Basically adding the league and team id to the player table.

Probably the best solution for my situation.


DataMapper 1.6.0 - El Forum - 03-10-2009

Allright I have the need to save multiple of the same relations... So now im just trying to find the right way to do it...

* Join permissions to a role
* @param varhcar/array $permission, $key=>$value
*  defaults to 'id' if you do not pass an array with $key=>$value
* @param array $role - $key=>$value to get the role object
* @param boolean $create_role TRUE/FALSE to create the role being passed
* @return boolean TRUE on success only
public function join_permissions_role($permission, $role, $create_role=FALSE)
    if ($create_role)

    $r = new Role();

        // allow for single fields
        $permission['id'] = $permission;

    foreach ($permission as $key => $val)
        $p[$i] = new Permission();

    if ($r->save($p))
        return TRUE;

I have no clue if the $r->save($p) would even do what I want it to do in this case...
Otherwise how would I save the same relationships over and over again with no limitations per say.

BTW The demo covers multiple relations but not have to save multiple relations of the same type

DataMapper 1.6.0 - El Forum - 03-10-2009

There are a couple of ways to save relationships where the field is not same as the model:
// known type

// unknown type
$type = 'creator';
$myObj->save($user, $type);

// saving a whole bunch:
$myObj->save($user->all, 'assignedto');

// saving many different kinds of objects at once
    'editor' => $user,
    'creator' => $user,
    'assignedto' => $assigned_users->all

Is this what you wanted?

DataMapper 1.6.0 - El Forum - 03-10-2009

not exactly...

So ive got a Role and many permissions that are attached to a role.

For example

Administrator: (role)
- Auth/Manage (permission)
- Auth/Delete_user (permission)
- Auth/Edit_user (permission)

just for some examples.