我有租用设备的特许经营商 我有客户租用特许经营商 特许经营商和客户都是用户
我有一个User表,一个Franchisee表,一个Customer表和一个Booking表。
表格如下:
CREATE TABLE IF NOT EXISTS `franchisees` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`group_id` int(11) DEFAULT NULL,
`firstname` varchar(20) DEFAULT NULL,
`lastname` varchar(20) DEFAULT NULL,
`address1` varchar(150) DEFAULT NULL,
`address2` varchar(150) DEFAULT NULL,
`city` varchar(20) DEFAULT NULL,
`postcode` varchar(10) DEFAULT NULL,
`phone` varchar(15) DEFAULT NULL,
`alt_phone` varchar(15) DEFAULT NULL,
`lat` float DEFAULT NULL,
`lng` float DEFAULT NULL,
`created` timestamp NULL DEFAULT NULL,
`modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ;
CREATE TABLE IF NOT EXISTS `customers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`group_id` int(11) DEFAULT NULL,
`firstname` varchar(20) DEFAULT NULL,
`lastname` varchar(20) DEFAULT NULL,
`address1` varchar(150) DEFAULT NULL,
`address2` varchar(150) DEFAULT NULL,
`city` varchar(20) DEFAULT NULL,
`postcode` varchar(10) DEFAULT NULL,
`phone` varchar(15) DEFAULT NULL,
`alt_phone` varchar(15) DEFAULT NULL,
`lat` float DEFAULT NULL,
`lng` float DEFAULT NULL,
`created` timestamp NULL DEFAULT NULL,
`modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ;
CREATE TABLE IF NOT EXISTS `bookings` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`customer_id` int(11) DEFAULT NULL,
`franchisee_id` int(11) DEFAULT NULL,
`booking_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`start_date` datetime DEFAULT NULL,
`end_date` datetime NOT NULL,
`event_location` text,
`price` float DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ;
我的模型关系定义如下:
特许
public $name = 'Franchisee';
public $hasMany = array(
'FranchiseeBooking' => array(
'className' => 'Booking',
'foreignKey' => 'franchisee_id'
)
);
CUSTOMER
public $name = 'Customer';
public $hasMany = array(
'CustomerBooking' => array(
'className' => 'Booking',
'foreignKey' => 'customer_id'
)
);
BOOKING
public $name = 'Booking';
public $belongsTo = array(
'Customers' => array(
'className' => 'Customer',
'foreignKey' => 'customer_id'
),
'Franchisees' => array(
'className' => 'Franchisee',
'foreignKey' => 'franchisee_id'
)
);
所以,我正在尝试查询预订表(其中franchisee_id是登录用户)并获取特许经营商的所有预订,以及相关的客户数据。
$options = array('conditions' => array('Booking.franchisee_id' => $this->_authUser));
$bookings = $this->Booking->find('all');
以下查询中的结果:
SELECT `Booking`.`id`, `Booking`.`customer_id`, `Booking`.`franchisee_id`, `Booking`.`booking_date`, `Booking`.`start_date`, `Booking`.`end_date`, `Booking`.`event_location`, `Booking`.`price`, `BookingCustomers`.`id`, `BookingCustomers`.`user_id`, `BookingCustomers`.`group_id`, `BookingCustomers`.`firstname`, `BookingCustomers`.`lastname`, `BookingCustomers`.`address1`, `BookingCustomers`.`address2`, `BookingCustomers`.`city`, `BookingCustomers`.`postcode`, `BookingCustomers`.`phone`, `BookingCustomers`.`alt_phone`, `BookingCustomers`.`lat`, `BookingCustomers`.`lng`, `BookingCustomers`.`created`, `BookingCustomers`.`modified`, `BookingFranchisees`.`id`, `BookingFranchisees`.`user_id`, `BookingFranchisees`.`group_id`, `BookingFranchisees`.`firstname`, `BookingFranchisees`.`lastname`, `BookingFranchisees`.`address1`, `BookingFranchisees`.`address2`, `BookingFranchisees`.`city`, `BookingFranchisees`.`postcode`, `BookingFranchisees`.`phone`, `BookingFranchisees`.`alt_phone`, `BookingFranchisees`.`lat`, `BookingFranchisees`.`lng`, `BookingFranchisees`.`created`, `BookingFranchisees`.`modified` FROM `trailblazer`.`bookings` AS `Booking` LEFT JOIN `trailblazer`.`customers` AS `BookingCustomers` ON (`Booking`.`customer_id` = `BookingCustomers`.`id`) LEFT JOIN `trailblazer`.`franchisees` AS `BookingFranchisees` ON (`Booking`.`franchisee_id` = `BookingFranchisees`.`id`) WHERE 1 = 1
打印数组时看起来像这样:
Array
(
[0] => Array
(
[Booking] => Array
(
[id] => 1
[customer_id] => 8
[franchisee_id] => 7
[booking_date] => 2013-02-25 11:44:29
[start_date] => 2013-03-06 11:45:00
[end_date] => 2013-03-08 11:45:00
[event_location] => Belfast
[price] => 900
)
[BookingCustomers] => Array
(
[id] =>
[user_id] =>
[group_id] =>
[firstname] =>
[lastname] =>
[address1] =>
[address2] =>
[city] =>
[postcode] =>
[phone] =>
[alt_phone] =>
[lat] =>
[lng] =>
[created] =>
[modified] =>
)
[BookingFranchisees] => Array
(
[id] =>
[user_id] =>
[group_id] =>
[firstname] =>
[lastname] =>
[address1] =>
[address2] =>
[city] =>
[postcode] =>
[phone] =>
[alt_phone] =>
[lat] =>
[lng] =>
[created] =>
[modified] =>
)
)
[1] => Array
(
[Booking] => Array
(
[id] => 2
[customer_id] => 14
[franchisee_id] => 7
[booking_date] => 2013-02-25 16:18:44
[start_date] => 2013-02-08 16:15:00
[end_date] => 2013-02-23 16:15:00
[event_location] =>
[price] => 6750
)
[BookingCustomers] => Array
(
[id] =>
[user_id] =>
[group_id] =>
[firstname] =>
[lastname] =>
[address1] =>
[address2] =>
[city] =>
[postcode] =>
[phone] =>
[alt_phone] =>
[lat] =>
[lng] =>
[created] =>
[modified] =>
)
[BookingFranchisees] => Array
(
[id] =>
[user_id] =>
[group_id] =>
[firstname] =>
[lastname] =>
[address1] =>
[address2] =>
[city] =>
[postcode] =>
[phone] =>
[alt_phone] =>
[lat] =>
[lng] =>
[created] =>
[modified] =>
)
)
)
如您所见,我没有收到相关数据。我觉得问题与主键和外键有关,但我不知道我哪里出错了。有人能搞清楚吗? (我正在使用cakephp 2.3)
答案 0 :(得分:0)
我认为您的User-Id未正确调用。尝试:
$options = array('conditions' => array('Booking.franchisee_id' => $this->Auth->user('id')));