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

[eluser]WanWizard[/eluser]
This has been asked a few times recently.

You're not using the latest version (v1.8.2.1), please download the latest version (https://bitbucket.org/wanwizard/datamapper/get/tip.zip) from Bitbucket to solve this problem (this is not only true for error delimiters, but for every config value you want to override in the model.

[eluser]diZzyCoDeR[/eluser]
Greetings! I've been racking my brain about this for a couple days, and finding it hard to accomplish what I want using Advanced Relationships techniques.

I have a contentprovider model, and a retentionrule model. Contentprovider will have two retention rules; a CDN one, and a Local one.

contentprovider
Code:
var $has_one = array(
            'priority' => array(),
            'cdn_rule' => array(
                'class' => 'retentionrule',
                'other_field' => 'cdn_retentionrule'
            ),
            'local_rule' => array(
                'class' => 'retentionrule',
                'other_field' => 'local_retentionrule'
            )
        );

retentionrule
Code:
var $has_one = array(
            'cdn_retentionrule' => array(
                'class' => 'contentprovider',
                'other_field' => 'cdn_rule'
            ),

            'local_retentionrule' => array(
                'class' => 'contentprovider',
                'other_field' => 'local_rule'
            ),
            
        );

For the tables, they have 'id' fields and the join tables look like this:

contentprovider_retentionrule
id, cdn_retentionrule_id, local_retentionrule_id, contentprovider_id

..And I'm getting

Quote:An Error Was Encountered

Unable to relate contentprovider with retentionrule.

I just know I'm doing something wrong, but I can't figure what it is!? I'm on CI2 & DM1.8

[eluser]Unknown[/eluser]
thank you

[eluser]WanWizard[/eluser]
@diZzyCoDeR,

Since you haven't defined the 'join_self_as' and 'join_other_as' fields of the relation, Datamapper will try to determine them automatically.

For 'join_self_as', it will use the "other_field" value. So your relationship table should contain:
- cdn_retentionrule_id (check)
- local_retentionrule_id (check)

For 'join_other_as', it will use the relation name. So your relationship table should also contain:
- cdn_rule_id (missing)
- local_rule_id (missing)

For the retentionrule model, it should be the other way around.

As this is clearly not what you want, define the 'join_self_as' and 'join_other_as' values for your relations, so they point to the correct keys in your relationship table.

[eluser]diZzyCoDeR[/eluser]
Thanks Wiz, cheers. So, if I'm reading your phonebook, I've got the following now:

contentprovider
Code:
var $has_one = array(
            'priority' => array(),
            'cdn_rule' => array(
                'class' => 'retentionrule',
                'other_field' => 'cdn_retentionrule',
                'join_self_as' => 'contentprovider',
                'join_other_as' => 'cdn_retentionrule',
            ),
            'local_rule' => array(
                'class' => 'retentionrule',
                'other_field' => 'local_retentionrule',
                'join_self_as' => 'contentprovider',
                'join_other_as' => 'local_retentionrule',
            )
        );

retentionrule
Code:
var $has_one = array(
            'cdn_retentionrule' => array(
                'class' => 'contentprovider',
                'other_field' => 'cdn_rule',
                'join_self_as' => 'cdn_retentionrule',
                'join_other_as' => 'contentprovider',
            ),

            'local_retentionrule' => array(
                'class' => 'contentprovider',
                'other_field' => 'local_rule',
                'join_self_as' => 'local_retentionrule',
                'join_other_as' => 'contentprovider',
            ),
            
        );

Yet, I still get the same error... (?)

[eluser]WanWizard[/eluser]
On which statement exactly do you get the error?

[eluser]diZzyCoDeR[/eluser]
Code:
$relations = some_func_to_get_relationships_in_an_array();
$success = $contentprovider->save($relations);

where $relations is set like this previously (ie, in function)

Code:
$related_model = new $related_model_name();
$related_model->where_in('id', $val)->get();
$return_array[] = $related_model->all;

so my models look right then? maybe I should whip up a simpler test...

[eluser]WanWizard[/eluser]
You're not being clear.

What is $relations EXACTLY? An object? An array of objects? Which object(s)? Something else?

[eluser]diZzyCoDeR[/eluser]
Sorry, mate. It's really complicated, I'm trying to abstract; let me break it down.

$relations ends up being an array of objects, come ca:

Code:
Array
(
    [0] => Array
        (
            [0] => Priority Object
                (
                    [has_one] => Array
                        (
                        )

                    [has_many] => Array
                        (
                            [show] => Array
                                (
                                    [class] => show
                                    [other_field] => priority
                                    [join_self_as] => priority
                                    [join_other_as] => show
                                    [join_table] =>
                                    [reciprocal] =>
                                    [auto_populate] =>
                                    [cascade_delete] => 1
                                )

                            [contentprovider] => Array
                                (
                                    [class] => contentprovider
                                    [other_field] => priority
                                    [join_self_as] => priority
                                    [join_other_as] => contentprovider
                                    [join_table] =>
                                    [reciprocal] =>
                                    [auto_populate] =>
                                    [cascade_delete] => 1
                                )

                        )

                    [validation] => Array
                        (
...

it's actually a dynamic CrUD I built to handle basic editing of tables in the DB, and it works famously, until this scenario. All my other relationships are simple 1:1 or n:n (and combinations thereof). So when I added this advanced relationship setup it changed the game a bit.

But the test I'm doing is simply a form-submission to the controller which finds the relationships for the model being saved, and then passes those relationships to the ->save() function (as seen above).

[eluser]WanWizard[/eluser]
Ok.

The original error was "Unable to relate contentprovider with retentionrule.".

This means that one of the objects in that array is an instance of the class "retentionrule". However, you do not have a relationship defined for "retentionrule", only for "cdn_retentionrule" and "local_retentionrule".

Datamapper can't find the relation to the object "retentionrule", and reports this.

So you need to tell Datamapper which relation you mean. This is done by the array key. If it is numeric, the class name of the object is used. If it is string, that string is used.

Code:
// use the class name
$array = array($retentionrule);

// use a relation name
$array = array('cdn_retentionrule' => $retentionrule);




Theme © iAndrew 2016 - Forum software by © MyBB