2013-02-26 135 views
0

我有加盟商的租金設備 我的客戶是租掉加盟 兩個加盟商和顧客是用戶正確的模型關係,對cakephp2主鍵和外鍵查詢

我有一個用戶表,加盟商表,客戶表和預訂表。

的表是這樣的:

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' 
    ) 
); 

所以,我想查詢預訂表(其中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

確保設置了'$ recursive = -1;',然後發佈它生成的新查詢。此外,爲什麼你使用別名而不是「預訂」你的hasMany關聯? – Dave 2013-02-26 16:01:44

+0

是否有任何數據插入到相應的表中?在我看來,你的關係是好的,因爲你的find()方法獲取所需的所有數據。問題是你的表可能沒有任何數據。 – 2013-02-26 17:15:14

回答

0

我相信你的用戶ID沒有被正確調用。嘗試:

$options = array('conditions' => array('Booking.franchisee_id' => $this->Auth->user('id')));