Welcome Guest, Not a member yet? Register   Sign In
1:m relation question
#1

[eluser]macron[/eluser]
Hi. Questions:
Got 2 models:

class M_news extends DataMapper
{
var $table = 'news';
var $has_many = array('m_image');
var $created_field = 'creation_date';
...

and

class M_image Extends DataMapper
{
var $table = 'images';
var $has_one = array('m_news');


db looking like this:


CREATE TABLE images (
id int(11) NOT NULL AUTO_INCREMENT,
name char(50) DEFAULT NULL,
html_size char(30) DEFAULT NULL,
new_id int(11) DEFAULT NULL,
PRIMARY KEY (id)
) DEFAULT CHARSET=utf8;


CREATE TABLE news (
id int(11) NOT NULL AUTO_INCREMENT,
date timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
headline char(50) DEFAULT NULL,
dk mediumtext,
uk mediumtext,
user_id int(11) DEFAULT NULL,
PRIMARY KEY (id)
) DEFAULT CHARSET=utf8;

Problem is this: How do I make the 1:M relation so I dont have to make table images_news with m_new_id and m_image_id
And how do I make the m_new_id into m_news_id. I am aware of the plural/singular rule, but m_new_id is downright wrong.

I want to do this:

$n = new M_news();
...
$i = new M_image();
...

$n->save($i); <---- ERROR Table 'dotinfo.images_news' doesn't exist
#2

[eluser]WanWizard[/eluser]
In-Table-Foreign-Keys are named with the name of the related model, so in this case M_news_id.
#3

[eluser]macron[/eluser]
Hi
Yes thats what I thought too, but the ORM is looking for the relationtable and looking for m_new_id. I do know that my model and table should be named the same but since I want my controllers to have the news and image names I'm forced to name my models m_news and m_image.
I think maybee that news is both plural and singular I might have stumpled upon a "bug" here since the ORM is looking for m_new_id:

Error :

Code:
Table 'dotinfo.images_news' doesn't exist

SELECT * FROM (`images_news`) WHERE `m_new_id` = 9 AND `m_image_id` = 2
#4

[eluser]WanWizard[/eluser]
Datamapper defaults to a relationship table when the ITFK can't be found, so this behaviour isn't suprising.

You could try (I'm not in a positition to test this at the moment):
Code:
class M_news extends DataMapper
{
  var $table       = ‘news’;
  // requires M_news.images_id and M_images.news_id
  $has_many = array(
        'images' => array(
            'class' => 'M_image',
            'other_field' => 'news'
        )
  );
  var $created_field = ‘creation_date’;
}

and

class M_image Extends DataMapper
{
  var $table = ‘images’;
  $has_one = array(
        'news' => array(
            'class' => 'M_news',
            'other_field' => 'images'
        )
  );
}

Datamapper is designed to work with standard names for models, tables and key fields.

If you have a namespace problem, you might want to rename your controllers to Controller_news and Controller_image, and use a custom route to route http://website/news/method to http://website/controller_news/method.
#5

[eluser]macron[/eluser]
Got it!

Did a consistency check and renamed my models 'news_model' and 'image_model'.

Renamed the FKID in images table to 'news_model_id' and BOOM it worked.

Here I was thinking that the $table var vas the controlling namevar for the PKID and FKID but its the model name!! Maybee this should change in the future. !

By the way GREAT lib - makes CRUD so much easier.
#6

[eluser]WanWizard[/eluser]
That's why "advanced relationships" exist, so you can override all of these parameters. The table name is not relevant, and not used anywhere, other than to overrule to mechanism of mapping model name to table name.

Great you've got it solved.




Theme © iAndrew 2016 - Forum software by © MyBB