使用 codeigniter 和 datamapper 的高级关系问题
我有三个表:客户、订单和用户 - 用户是登录表,客户保存地址信息等。
CREATE TABLE IF NOT EXISTS `orders` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`customer_id` int(11) NOT NULL,
`image_path` varchar(100) NOT NULL,
`sample_path` varchar(255) NOT NULL,
`status` enum('new','progress','completed','sent') NOT NULL,
`placed` date NOT NULL,
`updated_date` date NOT NULL,
`will_send` int(1) NOT NULL,
PRIMARY KEY (`id`)
)
CREATE TABLE IF NOT EXISTS `customers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`name` varchar(150) NOT NULL,
`address_1` varchar(150) NOT NULL,
`address_2` varchar(150) NOT NULL,
`address_3` varchar(150) NOT NULL,
`postcode` varchar(50) NOT NULL,
`phone` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ;
CREATE TABLE IF NOT EXISTS `users` (
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`group_id` mediumint(8) unsigned NOT NULL,
`ip_address` char(16) NOT NULL,
`username` varchar(15) NOT NULL,
`password` varchar(40) NOT NULL,
`salt` varchar(40) DEFAULT NULL,
`email` varchar(100) NOT NULL,
`activation_code` varchar(40) DEFAULT NULL,
`forgotten_password_code` varchar(40) DEFAULT NULL,
`remember_code` varchar(40) DEFAULT NULL,
`created_on` int(11) unsigned NOT NULL,
`last_login` int(11) unsigned DEFAULT NULL,
`active` tinyint(1) unsigned DEFAULT NULL,
PRIMARY KEY (`id`)
) ;
我的问题在于用户表中的 ID 是主要问题 - 它与客户相关(如 user_id),然后客户与订单相关
我想要做的是让我的订单表在订单和客户中都具有基于 customer_id 的关系
我已经尝试过这个 -
var $has_one = array(
'customer' => array('join_self_as'=>'customer','join_other_as' => 'user')
);
但这不起作用,因为它产生以下查询 - 它引用 customer.id 而不是 customer.user_id
SELECT * FROM `customers` LIMIT 1
0.0004 SELECT `customers`.*
FROM (`customers`)
LEFT OUTER JOIN `orders` orders ON `customers`.`id` = `orders`.`customer_id`
WHERE `orders`.`id` = 27
希望这解释了我正在尝试做的事情
i have three tables customer, order and user - user is the login table and customer holds address info and so on.
CREATE TABLE IF NOT EXISTS `orders` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`customer_id` int(11) NOT NULL,
`image_path` varchar(100) NOT NULL,
`sample_path` varchar(255) NOT NULL,
`status` enum('new','progress','completed','sent') NOT NULL,
`placed` date NOT NULL,
`updated_date` date NOT NULL,
`will_send` int(1) NOT NULL,
PRIMARY KEY (`id`)
)
CREATE TABLE IF NOT EXISTS `customers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`name` varchar(150) NOT NULL,
`address_1` varchar(150) NOT NULL,
`address_2` varchar(150) NOT NULL,
`address_3` varchar(150) NOT NULL,
`postcode` varchar(50) NOT NULL,
`phone` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ;
CREATE TABLE IF NOT EXISTS `users` (
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`group_id` mediumint(8) unsigned NOT NULL,
`ip_address` char(16) NOT NULL,
`username` varchar(15) NOT NULL,
`password` varchar(40) NOT NULL,
`salt` varchar(40) DEFAULT NULL,
`email` varchar(100) NOT NULL,
`activation_code` varchar(40) DEFAULT NULL,
`forgotten_password_code` varchar(40) DEFAULT NULL,
`remember_code` varchar(40) DEFAULT NULL,
`created_on` int(11) unsigned NOT NULL,
`last_login` int(11) unsigned DEFAULT NULL,
`active` tinyint(1) unsigned DEFAULT NULL,
PRIMARY KEY (`id`)
) ;
my problem lies in the the ID from the user table is the main one- it relates to customer (as user_id) then customer relates to order
what i want to do is have my order table have a relationship based on customer_id in both orders and customers
i have tried this -
var $has_one = array(
'customer' => array('join_self_as'=>'customer','join_other_as' => 'user')
);
but thats not working as it produces the following query- which references customer.id instead customer.user_id
SELECT * FROM `customers` LIMIT 1
0.0004 SELECT `customers`.*
FROM (`customers`)
LEFT OUTER JOIN `orders` orders ON `customers`.`id` = `orders`.`customer_id`
WHERE `orders`.`id` = 27
hopefully this explains what i am trying to do
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您不想作为变量加入...我认为您想要的是深层关系查询。
你可以这样做..
然后你可以像这样访问用户:
也就是说,如果我正确理解你的问题。
I don't think you want to join as a variable... I think what you want is a deep relationship query.
You can do this..
Then you can access the user like this:
That is if I am understanding your problem correctly.