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

[eluser]WanWizard[/eluser]
For set_join_field() the relationship must already exist (it is an UPDATE query), so
Code:
$person->save($dog);
$person->set_join_field($dog, 'years_together', 5);

[eluser]Maglok[/eluser]
[quote author="WanWizard" date="1340801825"]For set_join_field() the relationship must already exist (it is an UPDATE query), so
Code:
$person->save($dog);
$person->set_join_field($dog, 'years_together', 5);
[/quote]

You have got the be kidding me Smile You are entirely correct of course. Why did I not try that! Guess I figured get all data together THEN save. Thanks! Works.

[eluser]animatora[/eluser]
[quote author="WanWizard" date="1340800015"]That is a very standard table definition, so no advanced relationship definition is needed. $has_one('user') and $has_many('post') should work fine.

The definition you're proposing looks like a copy/paste from the example in the manual, and is definately not going to work, as it refers to columns that are not present in the table, like "created_post_id".

The "Attribute table" and the section on "manually defining the relationship" explain what each of the settings do. You only have to define them if they are different from the default (for example, you only use "join_table" in a many-many relation that uses an external join table, and you're not using the default naming convention for this table).[/quote]

Ok I understand this, but what if I want to use creator rather than user for relation name ? How can I mask the user behind creator ? I can't change the database, it has been setup like this? Is it possible to do this trick ?

$post->creator makes much more sense, than $post->user

[eluser]shenanigans01[/eluser]
Sorry if i'm out of the loop, Is this supported on CI's latest 2.1.1 release? If I update to 2.1.1 will it break my code?

[eluser]WanWizard[/eluser]
[quote author="mikem562" date="1340815540"]Sorry if i'm out of the loop, Is this supported on CI's latest 2.1.1 release? If I update to 2.1.1 will it break my code?[/quote]
Until now no issues have been reported, so I assume the answer is yes.

[eluser]WanWizard[/eluser]
[quote author="animatora" date="1340803299"]Ok I understand this, but what if I want to use creator rather than user for relation name ? How can I mask the user behind creator ? I can't change the database, it has been setup like this? Is it possible to do this trick ?

$post->creator makes much more sense, than $post->user [/quote]

If you dump a model object, you'll see that internally Datamapper will expand simple definitions to advanced definitions, so you can see which defaults are being used.

For example, in this case if you do user $has_one('post'), this will be generated:
Code:
array (size=1)
      'post' =>
        array (size=8)
          'class' => string 'post' (length=4)
          'other_field' => string 'user' (length=4)
          'join_self_as' => string 'user' (length=4)
          'join_other_as' => string 'post' (length=4)
          'join_table' => string '' (length=0)
          'reciprocal' => boolean false
          'auto_populate' => null
          'cascade_delete' => boolean true

This says:
- array key: my User object has a related object called 'post'
- class: this object is defined by the class 'post'
- other_field: in the the class 'post', the relation back to me is defined as 'user'
- join_self_as: foreign key for my side of the relation (_id is appended, so 'user_id')
- join_other_as: foreign key for the other side of the relation (_id is appended, so 'post_id')
- join_table: table containing the foreign keys (required in many-many, optional for others)
- reciprocal: when true and a self-referencing relation, make the relation both ways
- autopopulate: if true, fetch this relations objects when the parent is fetched
- cascade_delete: if true, delete related objects when you delete the parent

So if you just want to name the relation 'creator', change the array key, and set class to 'post'. And in the post model you set the 'other_field' to 'creator'. Leave the other fields as defined above (any foreign key will still be called 'post_id' for example).

Although the foreign keys are defined for both tables, they will only used when needed, Datamapper is smart enough to figure out which to use.

For example, if you have a one-one relation, the foreign key can be in either table. Or you can have a one-one relation that uses a join table (in which case both FK's are needed).

I hope this helps.

[eluser]animatora[/eluser]
Thanks @WanWizard, this will do the job.

[eluser]Unknown[/eluser]
can the foreign key be override to be other than xxx_id ?

I have a table called posts
id , image_1 , image_2 , content

and table images
id , img_name , etc

and i want to link table posts with image_1 and image_2 as FK . I had read countless times in the documentation but I dont think image_1 and image_2 can be use as FK .. or am I wrong ?

[eluser]WanWizard[/eluser]
It can be overridden, but has to end with "_id". It's hardcoded at the moment.

[eluser]JamieBarton[/eluser]
[quote author="WanWizard" date="1339783600"]In my previous example I has indicated the many-to-many needed a relationship table, which indeed contains the columns user_id and friend_id.

You can add extra columns to this table, and use where_join_field() to query the relation based on the contents of the 'approved' column.[/quote]

Still not got this to work. Any chance you can shoot some code my way? I think I'm trying to over complicate things!




Theme © iAndrew 2016 - Forum software by © MyBB