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 - 03-06-2009

[eluser]OverZealous[/eluser]
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:
Code:
// member and region are already loaded
$member->delete($region);
// OR this also works
$region->delete($member);

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

Code:
$members = new Members();
$members->delete_all()

and it will automagically delete all from the relationship table?


DataMapper 1.6.0 - El Forum - 03-06-2009

[eluser]OverZealous[/eluser]
Yes, except you have to get them first:
Code:
$members = new Members();
$members->get()->delete_all();

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

cheers!


DataMapper 1.6.0 - El Forum - 03-06-2009

[eluser]wolffc[/eluser]
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

Code:
$league = new League();
$league->get_by_id(1);

$league->team->get();
//this will give me all the teams in a league

$league->team->player->get()
//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)
{
$t->player->get();
}

Am I doing or thinking about this completely wrong?


DataMapper 1.6.0 - El Forum - 03-06-2009

[eluser]OverZealous[/eluser]
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:
Code:
$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?
Code:
$league->player->get();



DataMapper 1.6.0 - El Forum - 03-06-2009

[eluser]wolffc[/eluser]
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.

Thanks.


DataMapper 1.6.0 - El Forum - 03-10-2009

[eluser]tdktank59[/eluser]
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...

Code:
/**
* 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)
    {
        $this->create('role',$role);
    }

    $r = new Role();
    $r->where('id',$role['id'])->get();

    if(!is_array($permission))
    {
        // allow for single fields
        $permission['id'] = $permission;
    }

    foreach ($permission as $key => $val)
    {
        $p[$i] = new Permission();
        $p[$i]->where($key,$val)->get();
    }

    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

[eluser]OverZealous[/eluser]
There are a couple of ways to save relationships where the field is not same as the model:
Code:
// known type
$myObj->save_creator($user);

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

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

// saving many different kinds of objects at once
$myObj->save(array(
    'editor' => $user,
    'creator' => $user,
    'assignedto' => $assigned_users->all
));

Is this what you wanted?


DataMapper 1.6.0 - El Forum - 03-10-2009

[eluser]tdktank59[/eluser]
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.