Welcome Guest, Not a member yet? Register   Sign In
[datamapper ORM] How do I add my own columns to the autogenerated m:m tables
#1

[eluser]macron[/eluser]
In order to do proper normalization to my data I need to add a column to the table made by datamapper ORM.
As you can see from the models below I use the images table in two relations. One for news and one for teachers. So I cant add info to the images table that relates to the teachers relation only.

What I want to do is add an imagetype to the relation between teacher and image. As shown in the code below a teacher has many images. I want a way to select a specific image related to teacher. One with a specific imagetype. In essens NF 2 stuff.

So, how do I do this?

Code:
class Image_model Extends DataMapper
{
    var $table       = 'images';
    var $has_one = array('news_model', 'teachers_model');
...
}
class Teachers_model Extends DataMapper
{
    var $table         = 'teachers';
    var $has_many = array('image_model');
    var $has_one    = array('teachers_text');
...
}
class News_model extends DataMapper
{
    var $table                = 'news';
    var $has_many       = array('image_model', 'news_text_model');
    var $created_field  = 'date';
...
}

This is my db:
Code:
CREATE TABLE `images` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` char(50) DEFAULT NULL,
  `height` int(11) DEFAULT NULL,
  `width` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=innoDB DEFAULT CHARSET=utf8;

CREATE TABLE `images_news` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `news_model_id` int(11) unsigned DEFAULT NULL,
  `image_model_id` int(11) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=innoDB DEFAULT CHARSET=utf8;

CREATE TABLE `news` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `date` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `user_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=innoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

CREATE TABLE `teachers` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=innoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

CREATE TABLE `teachers_text` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `headline` varchar(200) DEFAULT NULL,
  `content` text,
  `language` enum('dk','uk') NOT NULL DEFAULT 'dk',
  `teachers_model_id` int(11) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=innoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
#2

[eluser]WanWizard[/eluser]
Read the documentation on many to many relationships, join tables and join fields: http://datamapper.exitecms.org/pages/joinfields.html
#3

[eluser]macron[/eluser]
Thank you. I guess I have to print the docs one day and read it from a-z. It seems that reading onscreen I miss a lot of info.
#4

[eluser]WanWizard[/eluser]
I still don't know all ins and outs. And I maintain it... Wink




Theme © iAndrew 2016 - Forum software by © MyBB