2015-09-08 120 views
0

我是CakePHP的新手,仍然在學習解決每個問題。CakePHP從另一個表中獲取記錄(如果ID匹配)

我有兩個表:客戶和商店。在商店表中,我有一個名爲customer_id的外鍵,它持有客戶表中的客戶ID。

在CakePHP中,我爲上面的表創建了控制器,模型和視圖。從視圖操作中的CustomerController.php中,我試圖獲得與客戶ID匹配的商店。

CustomerController.php頁:

class CustomersController extends AppController 
{ 

    public function index() 
    { 
     $customers = $this->Customers->find('all'); // Find all the records from the database. 
     $this->set('customers', $customers); 
     $stores = $this->Customers->Stores->find('all'); 
     $this->set('stores', $stores);   
    } 

    public function view($id = NULL) 
    { 
     $customer = $this->Customers->get($id); // Find a record for individual record. 
     $this->set('customer', $customer); 

     // $stores = $this->Customers->Stores->find('all'); 
     $stores = $this->Customers->Stores->find('list', [ 
       'keyField' => 'id', 
       'valueField' => 'store_name' 
      ]);   
     $this->set('store', $stores); 
    } 
} 

SQL表:

CREATE TABLE IF NOT EXISTS `customers` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `first_name` varchar(50) DEFAULT NULL, 
    `last_name` varchar(50) DEFAULT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ; 

-- 
-- Dumping data for table `customers` 
-- 

INSERT INTO `customers` (`id`, `first_name`, `last_name`) VALUES 
(1, 'Ray', 'Mak'), 
(2, 'John', 'Smith'), 
(3, 'Mike', 'Gorge'); 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `states` 
-- 

CREATE TABLE IF NOT EXISTS `states` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `state_name` varchar(100) DEFAULT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ; 

-- 
-- Dumping data for table `states` 
-- 

INSERT INTO `states` (`id`, `state_name`) VALUES 
(1, 'TX'), 
(2, 'VA'), 
(3, 'WI'), 
(4, 'AZ'), 
(5, 'FL'); 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `stores` 
-- 

CREATE TABLE IF NOT EXISTS `stores` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `customer_id` int(11) DEFAULT NULL, 
    `store_name` varchar(50) DEFAULT NULL, 
    `corp_name` varchar(200) DEFAULT NULL, 
    `street_address` varchar(200) DEFAULT NULL, 
    `city` varchar(50) DEFAULT NULL, 
    `state_id` int(11) DEFAULT NULL, 
    `zipcode` int(11) DEFAULT NULL, 
    PRIMARY KEY (`id`), 
    KEY `customers_idx` (`customer_id`), 
    KEY `states_idx` (`state_id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ; 

查看網頁:

<pre> 
<?php 
print_r(json_encode($store)); 
print_r(h($customer)); 
?> 
</pre> 

我可以使用自定義的SQL查詢與左連接得到的結果,但在cakephp中令人困惑的是,如何在id匹配時從另一個表中獲取數據。

任何幫助將不勝感激:)

+0

我想我解決我自己的問題。我能夠使用需要一段時間才能理解的條件,因爲cakephp上的文檔在解釋事物時不太清楚。 '$ stores = $ this-> Customers-> Stores-> find('all',['conditions'=> array('Stores.id'=> $ id)]);' – Ray

+0

並嘗試$ this- >客戶 - >商店 - > findById($ ID); –

+1

並閱讀[關聯]手冊部分(http://book.cakephp.org/3.0/en/orm/associations.html)。如果你正確地設置了你的模型(如果你使用烘焙工具,這會自動完成),你在客戶視圖方法中的查詢應該只是'$ this-> Customers-> get($ id,['contains' => ['商店']]);' –

回答

2

雷,

您在您解決您的問題的意見已經提到,但如果您使用的是下面的代碼在CustomersController ::取景功能,那麼$ ID不代表商店ID。

$stores = $this->Customers->Stores->find('all',[ 'conditions' => array('Stores.id' => $id)]); 

爲了獲得與客戶相關的店鋪,您必須參考下面的代碼

$stores = $this->Customers->Stores->findByCustomerId($id); 
//where $id represents customer_id from stores table 
+0

嗨Manish,非常感謝你的迴應。您的解決方案與我作爲我自己的解決方案所回答的相同。但是當我運行Greg的解決方案時,我能夠在一個關聯數組中打印商店和客戶數據。但是用你的解決方案和我的,我必須單獨打印$ store和$ customer變量。作爲格雷格的解決方案,我可以在一個變量中獲得一切。 – Ray

相關問題