Welcome Guest, Not a member yet? Register   Sign In
DMZ 1.7.1 (DataMapper OverZealous Edition)

[eluser]Dennis Rasmussen[/eluser]
You only have to set $model if there are complications with the plural version of the model name. (unless I'm completely mistaken?)

[eluser]WanWizard[/eluser]
No, the other way around.

$this->model is by default set to singular(classname). You have to set it manually if this fails to determine the proper singular name. And it has to be lowercase if you manually set it.

[eluser]modano[/eluser]
Gi guys,

I got a little problem with relationships that Ive been stuck on for a good few hours now and have run out of ideas, perhaps someone can help me out here. Im trying to get multiple Relationships to the Same Model to work:

Tables:
POSTS(id,source_id,courtesy_id)
SOURCES(id,name)

A post (blog post) has a source where the actual content might come from, but it also has an image in it, that comes from somewhere, and i have called it "courtesy_id".
For example, you write an article about foo and reference the source to be foo.com and in the article you put an image as well, and the image was found on site bar.com
Now i can at the end say: Source: foo.com and image courtesy of: bar.com

This is how I have set up the relationships:
In POST model:
Code:
var $has_one = array(
   'courtesy' => array(
      'class' => 'source',
      'other_field' => 'courtesy'
   ),
   'source' => array(
      'class' => 'source',
      'other_field' => 'source'
   )
);
and in SOURCE mode:
Code:
var $has_many = array(
   'courtesy' => array(
      'class' => 'post',
      'other_field' => 'courtesy'
   ),
   'source' => array(
      'class' => 'post',
      'other_field' => 'source'
   )
);

And in my view, in the form I have called two single select drop down menus "source_id" and "courtesy_id"

I just cant get it to work, and when it actually outputs something at all, it is this:
Quote:A Database Error Occurred
Error Number: 1146

Table 'blog.posts_sources' doesn't exist

SELECT `sources`.* FROM (`sources`) LEFT OUTER JOIN `posts_sources` post_posts_sources ON `sources`.`id` = `post_posts_sources`.`_id` WHERE `post_posts_sources`.`_id` = 4

Which seem wrong to me, because its a 1:M relationship, with the foreign keys in the table, not in a join table.

Any idea what im doing wrong?
Regards,
modano

[eluser]WanWizard[/eluser]
You've defined both sides of the relationship the same way, that's not going to work. The definition should be mirrored, like it is explained in the manual: http://datamapper.wanwizard.eu/pages/adv...tions.html

In the post model, try
Code:
var $has_one = array(
   'courtesy' => array(
      'class' => 'source',
      'other_field' => 'courtesy_post'
   ),
   'source' => array(
      'class' => 'source',
      'other_field' => 'source_post'
   )
);

In the source model, try
Code:
var $has_many = array(
   'courtesy_post' => array(
      'class' => 'post',
      'other_field' => 'courtesy'
   ),
   'source_post' => array(
      'class' => 'post',
      'other_field' => 'source'
   )
);

[eluser]modano[/eluser]
thanks for your reply WanWizard.
It works when i try to save, but not when i try to get the relationships for output. still same error.

Here is a code snippet:
Code:
$post=new Post();
$post->get_where(array('id'=>$variable_holding_id));
$source=new Source();
$post->where_related($source)->get();

Grateful for any help
modano

[eluser]WanWizard[/eluser]
You're trying to get something related on an empty object? Assuming you want the source records, you should have reversed your question.

I'm not sure (actually, I don't think so) that you can get both source objects in one go. When using relationships, you have to specify (in case multple exist), which relation you want to query.

Maybe you should explain what it is that you want to get out of this query?

[eluser]modano[/eluser]
sorry if im confusing. just took the dog for a walk and thought about this and it hit me, the code i just posted where i try to retrieve data im not specifying what! it is i want, source or courtesy.
is that the problem?

the object is not empty, as i said, saving works fine so there is a post and there is sources.

[eluser]modano[/eluser]
need to head to bed, will give it another go in the morning. thanks again for your help!
( "where_related" is where my problem lies)

[eluser]WanWizard[/eluser]
In your example your source object is empty, so there's nothing to relate to.

If you want the related objects, you should not use the where_related(), because that generates a where clause on the related object (and that needs a valid source (object).

You need
Code:
// get the post record
$post=new Post( $variable_holding_id );

// get the related records
$post->source->get();
$post->courtesy->get();

[eluser]modano[/eluser]
i had been starring at the code far too long.
thanks WanWizard, i got it working. Your help and some good night sleep was what was needed Smile




Theme © iAndrew 2016 - Forum software by © MyBB