Welcome Guest, Not a member yet? Register   Sign In
DataMapper ORM v1.8.2

[eluser]imohammad[/eluser]
Guys,

Is there a way to instead of having the default table key as ID to change it to User_id for example?

[eluser]Maglok[/eluser]
I do not believe so. Datamapper will look for the primary key as 'id' and then the foreign keys as the name of the object or what you put under 'join_self_as' in an advanced relationship + '_id'.

Why would you want the default table key to not be ID?

[eluser]imohammad[/eluser]
Thanks for your reply!

That's because I integrated DataMapper with CIBonefire. And Some of the fields they don't use ID as a primary key.

Looks like I have to change the core code then. I hope someone can figure out a way.

[quote author="Maglok" date="1405931280"]I do not believe so. Datamapper will look for the primary key as 'id' and then the foreign keys as the name of the object or what you put under 'join_self_as' in an advanced relationship + '_id'.

Why would you want the default table key to not be ID?[/quote]

[eluser]Maglok[/eluser]
There are a lot of methods that use the id property. One of them being get_by_id() for example. You can always use get_by_FIELD() instead for that one.

Thing is that statements like '$this->db->where('id', $this->id);' are all over the code. The only way I see to change that behaviour is to define the name of the id as a variable and use that everywhere instead. But that is an aweful lot of 'work'.

An example with for example the delete() function. It immediately grabs the id of the current Object, then iterates over its relations and makes liberal use of the id field with $this->id. After which it deletes the object itself with:

Code:
// Delete the object itself
    $this->db->where('id', $this->id);
    $this->db->delete($this->table);

So it is possible, but a buncha work. Maybe editing thie CIBonfire is easier?

[eluser]imohammad[/eluser]
Convinced Smile

Modifying CiBonfire seems much more easier.

Thanks Simon!

[quote author="Maglok" date="1405938066"]There are a lot of methods that use the id property. One of them being get_by_id() for example. You can always use get_by_FIELD() instead for that one.

Thing is that statements like '$this->db->where('id', $this->id);' are all over the code. The only way I see to change that behaviour is to define the name of the id as a variable and use that everywhere instead. But that is an aweful lot of 'work'.

An example with for example the delete() function. It immediately grabs the id of the current Object, then iterates over its relations and makes liberal use of the id field with $this->id. After which it deletes the object itself with:

Code:
// Delete the object itself
    $this->db->where('id', $this->id);
    $this->db->delete($this->table);

So it is possible, but a buncha work. Maybe editing thie CIBonfire is easier?[/quote]

[eluser]Maglok[/eluser]
It is of course a matter of personal taste, but I do like 'id' as the identifier myself. SQL allows differently, but I have always seen it as sort of a default, a guideline if you will. Smile

Good luck!

[eluser]imohammad[/eluser]
Thanks man!

I ended up using another table called Groups. Will leave role for default users and admins only. I modified CIbonfire but didnt work for me. more than 300 files :S

I have another question, welcoming an answer from everyone Smile

Now If I have a table called User and another table called User_profile, how to link both together?

So that I can join both tables together whenever I want.

Please note that I don't have a table called profile nor a model called profile.

[quote author="Maglok" date="1405941016"]It is of course a matter of personal taste, but I do like 'id' as the identifier myself. SQL allows differently, but I have always seen it as sort of a default, a guideline if you will. Smile

Good luck![/quote]

[eluser]Maglok[/eluser]
You got a table called User and a table called User_profile. You make two datamapper extending classes using the template file.

Then you can create new instances of those tables with:
Code:
$user = new User();

// Get all users
$user->get();

Now you need to create a relationship between the two. If it is a 1 on 1 (which seems likely) you can add a user_id foreign key to the User_profile table. Then in the datamapper code create a simple relationship as in the manual.

You can then access your profile info like so:
Code:
$user = new User();

// Get all users
$user->get();

// Get specific data from profile
echo $user->user_profile->address;

That any help?

[eluser]imohammad[/eluser]
Thank you Simon! really appreciate your help.

I somehow asked the wrong question Smile but glad you answered again.

Anyway, I'm having a problem since yesterday with joining many to many relations.

I have the following tables:
Groups
Groups_Users
Skills
Users
Skills_Users

I want to get a list of users based on the group filter and skill filter

What I tried:

Code:
$group = new Group();
$user = new User();
$skill = new Skill();

$group->where('title', 'dev')->get();
$skill->where('title', 'php');
$skill->where('title', 'java');

$user->where_related($group)->get() // retrieves all users in dev group
$user->where_related($skill)->get(); // retrieves all users either in dev group or not with the above skills

I could do this using sql statements easily, but I'm trying to get my head around this whole ORM for my work.


[quote author="Maglok" date="1406012655"]You got a table called User and a table called User_profile. You make two datamapper extending classes using the template file.

Then you can create new instances of those tables with:
Code:
$user = new User();

// Get all users
$user->get();

Now you need to create a relationship between the two. If it is a 1 on 1 (which seems likely) you can add a user_id foreign key to the User_profile table. Then in the datamapper code create a simple relationship as in the manual.

You can then access your profile info like so:
Code:
$user = new User();

// Get all users
$user->get();

// Get specific data from profile
echo $user->user_profile->address;

That any help?[/quote]

[eluser]Maglok[/eluser]
Yeah I know how ORM needs a bit of a different approach in thinking.

So you want users that have a certain skill and/or a certain group. You can do that like this:

Code:
// Instance your objects
$group = new Group();
$user = new User();
$skill = new Skill();

// Get a bunch of groups
$group->where('title', 'dev')->get();

// Combine the get for the skills and get them
$skill->where_in('title', array('php', 'java'))->get();

// Get the set that has the related $group and the related $skill
$this->where_related($group)->where_related($skill)->get();

This is just a example, I didn't test it. Smile

And important thing is to remember that you cannot just do another get() on a instance of a datamapper object and expect it to remember the previous result.




Theme © iAndrew 2016 - Forum software by © MyBB