【问题标题】:CakePHP get record from another table if the id matches如果 id 匹配,CakePHP 从另一个表获取记录
【发布时间】:2015-12-04 16:18:03
【问题描述】:

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

我有两个表:customers 和 stores。在 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 匹配时如何从另一个表中获取数据令人困惑。

任何帮助将不胜感激:)

【问题讨论】:

  • 我想我解决了自己的问题。我能够使用需要一段时间才能理解的条件,因为 cakephp 上的文档在解释时不是很清楚。 $stores = $this-&gt;Customers-&gt;Stores-&gt;find('all',[ 'conditions' =&gt; array('Stores.id' =&gt; $id)]);
  • 试试 $this->Customers->Stores->findById($id);
  • 并阅读Associations 上的手册部分。如果您正确设置模型(如果您使用 Bake 工具,这将自动为您完成),您在客户视图方法中的查询应该只是 $this-&gt;Customers-&gt;get($id, ['contain' =&gt; ['Stores']]);
  • 嗨,格雷格,非常感谢您的解决方案,效果很好。 :) 但是,我仍然对何时使用包含感到困惑。如果我理解正确,您的解决方案将从 Stores 模型表 col customer_id 中获取与 id 匹配的所有数据。

标签: php cakephp cakephp-3.0


【解决方案1】:

雷,

您在 cmets 中提到您解决了您的问题,但如果您在 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

【讨论】:

  • 嗨,Manish,非常感谢您的回复。您的解决方案与我作为自己的解决方案回答的相同。但是当我运行 Greg 的解决方案时,我能够在一个关联数组中打印商店和客户数据。但是使用您和我的解决方案,我必须分别打印 $store 和 $customer 变量。作为 Greg 的解决方案,我可以将所有内容放在一个变量中。
猜你喜欢
  • 2021-11-26
  • 2013-09-23
  • 1970-01-01
  • 2017-08-28
  • 2019-12-30
  • 1970-01-01
  • 2020-12-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多