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-10-2009

[eluser]OverZealous[/eluser]
Neither the updated version of DM nor DM itself currently support information associated with the join table, which I have to assume is what you want.

Right now, the closest you can get is to create a dedicated "relationship object", as I think of them, that contains the two objects and the extra relationship info.

I use this to connect Contacts with other Contacts, for example (like Company/Employee):
Code:
class ContactRelationship extends DataMapper {
    has_one = array(
        'parent' => array(
            'class' => 'contact',
            'other_field' => 'related_contacts'
        ),
        'contact'
    );
    // add fields, etc
}

class Contact extends DataMapper {
    has_many = array(
        'contactrelationship',
        'related_contacts' => array(
            'class' => 'contactrelationship',
            'other_field' => 'parent'
        )
    );
    // etc
}

Table:
Code:
contactrelationship
    id serial PRIMARY KEY,
    parent_id integer,
    contact_id integer,
    other_junk character varying,
etc

Usage:
Code:
$contact = new Contact();
$contact->get_by_id($id);
$contact->related_contacts->join_related('contact', array('id', 'name'))->get();
foreach($contact->related_contacts->all as $rel) {
    echo $rel->contact_name . ' [' . $rel->contact_id . ']';
    // change contactrelationship type
    $rel->type = 'Batman';
    $rel->save();
}

// If you need to edit the related contacts, try this slight hack
$rel_contacts = new Contact();
// this is the *field name* of the parent side of the relationship.
$rel_contacts->where_related('contactrelationship', 'parent_id', $contact->id);
$rel_contacts->join_related('contactrelationship', array('id', 'type'));
$rel_contacts->get();
foreach($rel_contacts->all as $rel_contact) {
    echo $rel_contact->name . ' is a ' . $rel_contact->contactrelationship_type;
    $rel_contact->is_happy = TRUE;
    $rel_contact->save();
}

I can't provide too much more help than that. Alternatively, you might have to rethink your design. Maybe you can use a dedicated model to keep track of more general permissions (which is how I do it). It depends on your needs.

I've actually been thinking about how to implement join table parameters for a while, but it always ends up making everything really complicated.


DataMapper 1.6.0 - El Forum - 03-10-2009

[eluser]tdktank59[/eluser]
NO no no lol...

Not what i ment...

each of the word/word were the permissions...

heres the structure lol

Users -> join_roles_users -> roles -> join_permissions_roles -> permissions -> page and method

where the only one not having a join_ table is permissions having
id
page_id
method_id

otherwise the rest are joined together


DataMapper 1.6.0 - El Forum - 03-10-2009

[eluser]OverZealous[/eluser]
You are going to have to take a moment and ask a clearer question. In fact, you haven't actually asked a question yet Tongue


DataMapper 1.6.0 - El Forum - 03-10-2009

[eluser]tdktank59[/eluser]
Same question as before... How to save multiple relations of the same type...

So i have these tables:

Quote:<b>Roles</b>
id
name
description
created_on
updated_on

<b>join_permissions_roles</b>
id
permission_id
role_id

<b>permissions</b>
id
method_id
page_id
no_restrict

<b>method</b>
id
name
description

<b>page</b>
id
name
description

what im trying to do is set multipe permissions to 1 role.

So for example

Quote:p_id is permission_id just shorted and r_id is role_id
<b>join_permissions_roles<b>
id p_id r_id
1 1 1
1 2 1
1 3 1
1 4 1
1 2 2
1 4 2
1 1 3

How do I save multiple relations of the same type (permissions onto roles) in this case...

The demo covers saving multiple with different classes

heres the code
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;
    }
}



DataMapper 1.6.0 - El Forum - 03-10-2009

[eluser]OverZealous[/eluser]
Are you just asking how to save a one-to-many relationship? I mean, that's what it looks like to me. I thought that was covered. You just do a ->save($obj) on each one, or ->save($array_of_obj) to save them all at once. You can also do ->save($obj->all) if $obj contains the result of a query.

->save($object) just saves a relation to $object. If the relationship is a has_many, it just adds it if the relationship doesn't already exist. If it is has_one, it replaces any existing relationship.

My extended DM doesn't change this, except it allows for no join table on the has_one side of relationships.


DataMapper 1.6.0 - El Forum - 03-10-2009

[eluser]tdktank59[/eluser]
ok guess i didnt read the manual right then.


DataMapper 1.6.0 - El Forum - 03-10-2009

[eluser]OverZealous[/eluser]
http://stensi.com/datamapper/pages/save.html]Save: DataMapper User Guide - scroll down about halfway to Save Multiple Relations. It shows the ->all method of saving.


DataMapper 1.6.0 - El Forum - 03-11-2009

[eluser]dmyers[/eluser]
Any way to combine the active record "sql" builder into the datamapper object?

You can do this in Datamapper and "push" a raw sql query into a datamapper object

Code:
$sql = "SELECT * FROM `users` WHERE `username` = 'Fred Smith' AND `status` = 'active'";

$u->query($sql);

and you can do this to dynamically build a sql statement using the built in Active Records

Code:
$query = $this->db->get('auth_user');

but can you jam the active record "builder" into the datamapper object?


DataMapper 1.6.0 - El Forum - 03-11-2009

[eluser]OverZealous[/eluser]
I don't know what you are asking, but DataMapper just uses ActiveRecord internally. Calling $this->db->anything or $obj->db->anything modifies the ActiveRecord query just as it normally would.

Then calling $obj->get() will process the results.


DataMapper 1.6.0 - El Forum - 03-13-2009

[eluser]Roobiz[/eluser]
Hi guys,

I have a little problem with DM and count's method.

My tables:

b_messages
-id
-is_view
-created
-updated
-ip

b_topics
-id
-id_user_from
-id_user_to
-created
-updated
-title

b_messages_b_topics
-id
-b_message_id
-b_topic_id

I Want to count all the non viewed messages linked to my topic (where my id == to id_user_from or id_user_to.

I test this kind of query but without any success:

Code:
$btopic = new B_topic();
$btopic->where('id_user_from', $myId);
$btopic->where('id_user_to', $myId);
$btopic->b_message->where('is_view', 0);
$btopic->b_message->count();

Can anyone help me?