有人可以解释这段magento代码吗?

Nit*_*hin 3 php magento

有人可以解释一下loadByCustomerId()在课堂上找到的这段magento代码Mage_Sales_Model_Mysql4_Quote.

$read = $this->_getReadAdapter();
$select = $this->_getLoadSelect('customer_id', $customerId, $quote)
                ->where('is_active=1')
                ->order('updated_at desc')
                ->limit(1);
$data = $read->fetchRow($select);
Run Code Online (Sandbox Code Playgroud)

当我var_dump($data)看到它的一系列客户数据时.什么是与此$data阵列相关的模型?谢谢.

Ala*_*orm 5

Magento"模型"(意味着允许您与数据库交互的实体,而不是通用服务器/域模型)有两层.第一个是"模型"层.这包含与模型进行逻辑交互的方法.(给我一个客户的地址,下订单等).第二层是"资源模型"层.资源模型处理与数据库(或更一般地,数据存储或持久层等)的任何交互.

资源模型与数据库交互的方式是通过适配器对象.一个用于读取信息,另一个用于写入信息.

所以,你在课堂上Mage_Sales_Model_Mysql4_Quote.这是一个资源模型.它是Mage_Sales_Model_Quote对象的后端,用实例化

$model = Mage::getModel('sales/quote');
Run Code Online (Sandbox Code Playgroud)

有了这条线

$read = $this->_getReadAdapter();
Run Code Online (Sandbox Code Playgroud)

你得到了模型的读取适配器的引用.这将允许您对数据库进行查询.

有了这条线

$select = $this->_getLoadSelect('customer_id', $customerId, $quote)
                ->where('is_active=1')
                ->order('updated_at desc')
                ->limit(1);
Run Code Online (Sandbox Code Playgroud)

您将获得对此资源模型将用于加载sales/quote对象的SQL语句(也是对象)的引用.

//gets a reference
$this->_getLoadSelect('customer_id', $customerId, $quote)
Run Code Online (Sandbox Code Playgroud)

然后,您正在调用该对象上的方法以使用其他逻辑来更改它

->where('is_active=1')
->order('updated_at desc')
->limit(1);
Run Code Online (Sandbox Code Playgroud)

在伪sql中,查询通常看起来像这样

SELECT * FROM quote_table;
Run Code Online (Sandbox Code Playgroud)

但是在调用这些方法之后,查询看起来就像

SELECT * FROM quote_table
WHERE is_active = 1
ORDER BY updated_at desc
LIMIT 1;
Run Code Online (Sandbox Code Playgroud)

最后,

$data = $read->fetchRow($select);
Run Code Online (Sandbox Code Playgroud)

在这里,您使用先前提取的读取适配器来查询数据库中查询将获取的特定报价项行.