簡體   English   中英

如果ID匹配,則CakePHP從另一個表獲取記錄

[英]CakePHP get record from another table if the id matches

我是CakePHP的新手,在我逐步解決每個問題時仍在學習。

我有兩個表:客戶和商店。 在stores表中,我有一個名為customer_id的外鍵,其中包含來自customers表的客戶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匹配時如何從另一個表獲取數據令人困惑。

任何幫助將不勝感激 :)

射線,

您在注釋中提到已解決了問題,但是,如果您在CustomersController :: view函數中使用以下代碼,則$ 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM