機器をレンタルするフランチャイズ加盟者がいます フランチャイズ加盟者をレンタルする顧客がいます フランチャイズ加盟者と顧客の両方がユーザーです
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'
)
);
お客様
public $name = 'Customer';
public $hasMany = array(
'CustomerBooking' => array(
'className' => 'Booking',
'foreignKey' => 'customer_id'
)
);
予約
public $name = 'Booking';
public $belongsTo = array(
'Customers' => array(
'className' => 'Customer',
'foreignKey' => 'customer_id'
),
'Franchisees' => array(
'className' => 'Franchisee',
'foreignKey' => 'franchisee_id'
)
);
そのため、Booking テーブル (ここで、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を使用しています)