[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