• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Datamapper Relationships : Database Error

#1
[eluser]Nguyen Trung[/eluser]
Hi everybody !
I'm using lastest version of datamapper
I have two model is 'NewsModel' and 'CommentModel', have one-to-many

NewsModel
Code:
class NewsModel extends DataMapper {

var $table = "news";
var $has_many = array(
   'Comment' => array(
     'class' => 'CommentModel',
     'other_field' => 'news'
   )
);

CommentModel
Code:
class CommentModel extends DataMapper {
var $table = "comment";
var $has_one = array(
   'News' => array(
     'class' => 'NewsModel',
     'other_field' => 'comment'
   )
);
}


==========================================
This My Table

Code:
-- ----------------------------
-- Table structure for `news`
-- ----------------------------
CREATE TABLE `news` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `id_alias` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `content` text COLLATE utf8_unicode_ci NOT NULL,
  `description` text COLLATE utf8_unicode_ci,
  `image` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `status` char(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `categories` int(11) DEFAULT NULL,
  `city` int(2) DEFAULT NULL,
  `author` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `source` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `user_id` int(11) DEFAULT NULL,
  `music` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Code:
-- ----------------------------
-- Table structure for `comment`
-- ----------------------------
DROP TABLE IF EXISTS `comment`;
CREATE TABLE `comment` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `news_id` int(11) DEFAULT NULL,
  `content` text COLLATE utf8_unicode_ci,
  `user_id` int(11) DEFAULT NULL,
  `comment_date` datetime DEFAULT NULL,
  PRIMARY KEY (`comment_id`),
  KEY `comment_news` (`news_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

When i run in my controller

Code:
$news = new NewsModel();
$news->get_by_id($id);
$news->Comment->get();

it show this error

Error Number: 1146

Table 'vnlive.comment_news' doesn't exist

SELECT `comment`.* FROM (`comment`) LEFT OUTER JOIN `comment_news` news_comment_news ON `comment`.`id` = `news_comment_news`.`_id` WHERE `news_comment_news`.`_id` = 1

Thanks you for reading
Sorry because I write English not well



#2
[eluser]WanWizard[/eluser]
First of all, I'm suprised you get this far.

Relation names are case sensitive, to be able to find the mapping between the two models the case should be the same.

The same is true for the mapping between the relation names and the foreign keys. If you use 'News' as relation name, Datamapper expects a foreign key called 'News_id', and not 'news_id'.

Because Datamapper can't find the foreign key, it starts looking for a relationship table. And gives you this error because it doesn't exist too.

Code:
class NewsModel extends DataMapper {

var $table = "news";

var $has_many = array(
  'comment' => array(
   'class' => 'CommentModel',
   'other_field' => 'news'
  )
);
}

class CommentModel extends DataMapper {

var $table = "comment";

var $has_one = array(
  'news' => array(
   'class' => 'NewsModel',
   'other_field' => 'comment'
  )
);
}

Now this works fine.

Code:
$news = new NewsModel();
$news->get_by_id(1);
$news->comment->get();

Note that you have an error in your comment table definition too. There is no column called 'comment_id', so the create fails on the primary key.

#3
[eluser]Nguyen Trung[/eluser]
Thanks for WanWizard's help, I have edit same your code but it's show error
"Fatal error: Call to a member function get() on a non-object in C:\xampp\htdocs\pro2012\application\controllers\News.php on line 62",
I try but ... i need your help.thanks you!

#4
[eluser]Nguyen Trung[/eluser]
Ok I'm so sorry , i have found my bug. Your code is very good!!
My bug is "news" table have a column name is "comment". when i call : $new->comment->get()


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.