Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7019239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:09:27+00:00 2026-05-27T23:09:27+00:00

When designing a relationship in Datamapper one is bound to call the relationship the

  • 0

When designing a relationship in Datamapper one is bound to call the relationship the same name as the related object, which is not too handy when you have something like Application_Model_User as a class name. For those of you who will rush to say that there is a configuration option with “class” key, I know. Been there tried that. It only works for getting a related object, not for updating them.

Here is a code snippet to reproduce the problem:

// User Model
class UserModel extends Datamapper
{
    public $table = 'users';
    public $has_many = array(
        'roles' => array(
            'class' => 'RoleModel',
            'other_field' => 'usermodel',
            'join_other_as' => 'role',
            'join_self_as' => 'user',
            'join_table' => 'users_roles' 
        ),
    );
}

class RoleModel extends DataMapper
{
    public $table = 'roles';
    public $has_many = array(
        'usermodel' => array('class'        => 'UserModel',
                         'other_field'  => 'roles',
                         'join_other_as'=> 'user',
                         'join_self_as' => 'role',
                         'join_table'   => 'users_roles' )
    );
}

// controller code. Make sure you have a role with INT id = 2, and a user with INT id = 5 in your db
$user = new UserModel(2);
$role = new RoleModel(5);
$user->save($role);

This code gives an “Unable to relate usermodel with rolemodel.” error, however it does work properly (meaning a new record is inserted in the join table user_roles) if the relation is renamed from “roles” to “rolemodel”.

So, if there are any avid users of CI’s Datamapper that could help, please let me know how to properly define relationships.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:09:27+00:00Added an answer on May 27, 2026 at 11:09 pm

    UPDATE

    You can save an object as a relation using the relationship key:

    $object->save( $related, $relationship_key ). 
    

    So you would need to use

    $user->save($role, "roles");
    

    See the bottom of this web page:
    http://datamapper.wanwizard.eu/pages/save.html


    Leaving this bit in case it helps someone else out.

    It looks like you want to have a custom name on a relationship. (That’s what I get after wading through all of the cynicism) –

    You get to name the relationship anything that you want with the key in the relationship array. So, in the following snippet, you use book <– this does or does not have to be the same name as the class – that’s what the class key is for.

    class Author extends DataMapper {
      $has_many = array(
        'book' => array(  // YOU USE THIS KEY TO NAME THE RELATIONSHIP
            'class' => 'book',
            'other_field' => 'author',
            'join_self_as' => 'author',
            'join_other_as' => 'book',
            'join_table' => 'authors_books'
         )
      );
    }
    

    If this is not working for you, my guess is you have something else wrong in the set up of your relationships.

    http://datamapper.wanwizard.eu/pages/advancedrelations.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Department entity which has a one-to-many relationship to an Employee entity.
I am struggling with designing a coreData model where I have only one type
What advantage is there to designing child-parent relationships, which allow us to do writes
When designing user table what would be the must have fields from the security/user
When designing a collection class, is there any reason not to implement locking privately
I'm designing a Core Data app which needs to keep dated records. Imagine a
I'm designing a new system, and I have need to store a pretty large
I'm designing a ruby on rails app for a pharmacy, and one of the
I have very little experience designing databases, and now I want to create a
I am designing a many-to-many relationship between two models: Cell and Isolator : CREATE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.