1
先進的關係,我有三個表客戶,訂單和用戶 - 用戶的登錄表和客戶端的地址信息等。問題用笨和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')
);
但那不是工作,因爲它會產生以下查詢 - WH ICH引用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
希望這解釋了什麼,我試圖做
它不是,它如何定義的關係 - 在訂單表中的字段CUSTOMER_ID涉及在客戶表 - –
在客戶表中的user_id是在user表的id字段的外鍵user_id說明正確?你試過$ has_one = array('customer'=> array('class'=>'user'))? – Josh