Magento 对象中的 Load 是什么意思?

发布于 2024-10-17 10:50:07 字数 439 浏览 1 评论 0原文

我正在尝试通过 Magento 学习一些编码,我必须承认我对其中的对象链接概念有点困惑。

事实上我不明白什么时候该做负载以及什么时候可以避免它。例如:

$product = Mage::getModel('catalog/product')->load($item->getProductId());

在本例中我想从产品 ID 中获取产品信息;为什么我需要加载它? ($item 是一个订单的所有产品的循环)

在这里我不需要做任何加载:

$customer = $payment->getOrder()->getCustomer();

我提前对我的愚蠢问题表示抱歉:与我的第二个例子?非常感谢,祝你有愉快的一天,

Anselme

I m trying to learn coding a bit through Magento, and I have to admit that I'm a bit confused about this notion of object chaining in it.

In fact I don't understand when to do a load and when I can avoid it. For exemple:

$product = Mage::getModel('catalog/product')->load($item->getProductId());

I would like to get the info of product from a product ID in this case; why do I need to load it? ($item is the loop of all the products of an order)

And here I don't need to do any load:

$customer = $payment->getOrder()->getCustomer();

I'm sorry in advance for my stupid question: What does load do comparing to my second example? Thanks a lot and have a nice day,

Anselme

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无所谓啦 2024-10-24 10:50:08
$customer = $payment->getOrder()->getCustomer();

这意味着客户的 id 已经存在于会话中,因此您不需要明确告诉 magento 加载客户。

在产品案例中,您必须告诉magento您想要获取详细信息的产品的产品ID。

$customer = $payment->getOrder()->getCustomer();

It means the id of the customer is already present in the session, so you don't need to explicitly tell magento to load the customer.

In the products case, you have to tell magento the product id of the product you want to get details of.

︶葆Ⅱㄣ 2024-10-24 10:50:07

在幕后,像 $ payment->getOrder() 这样的方法可以有效地(在检查它是否已经加载之后)执行以下操作:

return Mage::getModel('sales/order')->load($this->getOrderId());
// $this in this context is $payment

因此仍然需要加载来从数据库检索相关数据,getOrder() 方法只是为了方便。 load() 方法本身返回它的类实例,这就是为什么您可以在第一个示例中将其分配给 $productgetOrder()getCustomer() 方法不返回自身,它们返回不同的对象,这就是为什么 $ payment 在第二个示例中,未分配给 $customer

Mage::getModel() 方法仅负责确定正确的类并创建它的空白实例。您可以使用 setData() 调用来设置其数据,并传递一个键值数组,而不是加载。所有 setter 都会返回其对象,就像 load() 一样。

Behind the scenes a method like $payment->getOrder() is effectively (after checking to see if it's already loaded) doing this:

return Mage::getModel('sales/order')->load($this->getOrderId());
// $this in this context is $payment

So a load is still needed to retrieve the relevant data from the database, the getOrder() method is just a convenience. The load() method itself returns it's class instance, which is why you can assign it to $product in your first example. The getOrder() and getCustomer() methods don't return themselves, they return a different object, which is why $payment is not assigned to $customer in your second example.

The Mage::getModel() method is only responsible for determining the correct class and creating a blank instance of it. Instead of a load you could instead set it's data with a setData() call, passing a keyed array of values. All of the setters return their object, just like load() does.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文