如果id匹配,CakePHP从另一个表获取记录

时间:2015-09-08 20:45:56

标签: php cakephp cakephp-3.0

我是CakePHP的新手,在我解决每个问题时仍在学习。

我有两张桌子:顾客和商店。在商店表中,我有一个名为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匹配时,我如何从另一个表中获取数据会让人感到困惑。

任何帮助将不胜感激:)

1 个答案:

答案 0 :(得分:2)

您在评论中提到您解决了问题,但如果您在CustomersController :: view函数中使用以下代码,则$ 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