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

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):
class ContactRelationship extends DataMapper {
    has_one = array(
        'parent' => array(
            'class' => 'contact',
            'other_field' => 'related_contacts'
    // add fields, etc

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

    id serial PRIMARY KEY,
    parent_id integer,
    contact_id integer,
    other_junk character varying,

$contact = new Contact();
$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';

// 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'));
foreach($rel_contacts->all as $rel_contact) {
    echo $rel_contact->name . ' is a ' . $rel_contact->contactrelationship_type;
    $rel_contact->is_happy = TRUE;

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

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

otherwise the rest are joined together

DataMapper 1.6.0 - El Forum - 03-10-2009

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

Same question as before... How to save multiple relations of the same type...

So i have these tables:






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
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
* 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;

DataMapper 1.6.0 - El Forum - 03-10-2009

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

ok guess i didnt read the manual right then.

DataMapper 1.6.0 - El Forum - 03-10-2009

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

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

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


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

$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

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

Hi guys,

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

My tables:




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:

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

Can anyone help me?