使用 codeigniter 和 datamapper 的高级关系问题

发布于 2024-12-01 23:29:22 字数 2000 浏览 0 评论 0原文

我有三个表:客户、订单和用户 - 用户是登录表,客户保存地址信息等。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心在旅行 2024-12-08 23:29:23

我认为您不想作为变量加入...我认为您想要的是深层关系查询。

你可以这样做..

$order = new Order();
$order->include_related('customer/user', *, true, true)->get();

然后你可以像这样访问用户:

$order->customer->user->name;

也就是说,如果我正确理解你的问题。

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..

$order = new Order();
$order->include_related('customer/user', *, true, true)->get();

Then you can access the user like this:

$order->customer->user->name;

That is if I am understanding your problem correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文