Welcome Guest, Not a member yet? Register   Sign In
DataMapper 1.6.0

[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.

[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?

[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.

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

cheers!

[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?

[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();

[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.

[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

[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?

[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.




Theme © iAndrew 2016 - Forum software by © MyBB