SQL对Magento模型的理解 [英] SQL to Magento model understanding

查看:69
本文介绍了SQL对Magento模型的理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过SQL理解Magento模型:

Understanding Magento Models by reference of SQL:

  1. select * from user_devices where user_id = 1
  2. select * from user_devices where device_id = 3
  1. select * from user_devices where user_id = 1
  2. select * from user_devices where device_id = 3

如何使用magento模型执行相同的操作? getModel("module/userdevice")

How could I perform the same using my magento models? getModel("module/userdevice")

此外,如何找到每个查询的行数

Also, how can I find the number of rows for each query

以下问题已在此主题中得到解答.

Following questions have been answered in this thread.

How to perform a where clause ?
How to retrieve the size of the result set ?
How to retrieve the first item in the result set ?
How to paginate the result set ? (limit)
How to name the model ?

推荐答案

您所指的是收藏集

一些参考资料给您

  1. http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-5-magento-models-and-orm-b​​asics
  2. http://alanstorm.com/magento_collections
  3. http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/using_collections_in_magento
  4. lib/varien/data/collection/db.php和lib/varien/data/collection.php
  1. http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-5-magento-models-and-orm-basics
  2. http://alanstorm.com/magento_collections
  3. http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/using_collections_in_magento
  4. lib/varien/data/collection/db.php and lib/varien/data/collection.php

因此,假设您的模块设置正确,您将使用一个集合来检索您的模型类型的多个对象.

So, assuming your module is set up correctly, you would use a collection to retrieve multiple objects of your model type.

此语法为:

$yourCollection = Mage::getModel('module/userdevice')->getCollection()

Magento为开发人员提供了一些很棒的功能,可以与集合一起使用.因此,您上面的示例很容易实现:

Magento has provided some great features for developers to use with collections. So your example above is very simple to achieve:

$yourCollection = Mage::getModel('module/userdevice')->getCollection()
    ->addFieldToFilter('user_id', 1)
    ->addFieldToFilter('device_id', 3);

您可以获得返回的对象数:

You can get the number of objects returned:

$ yourCollection-> count()或简单地计数($ yourCollection)

$yourCollection->count() or simply count($yourCollection)

编辑

要回答评论中提出的问题:"如果我不需要集合而是一个特定对象,该怎么办"

To answer the question posed in the comment: "what If I do not require a collection but rather just a particular object"

这取决于您是否仍然需要满足原始问题中的两个条件,或者是否知道要加载的对象的ID.

This depends if you still require both conditions in the original question to be satisfied or if you know the id of the object you wish to load.

如果您知道对象的ID,则只需:

If you know the id of the object then simply:

Mage::getModel('module/userdevice')->load($objectId);

但是如果您仍然希望基于以下两个属性进行加载:

but if you wish to still load based on the two attributes:

user_id = 1
device_id = 3

然后,您仍将使用集合,而只是返回第一个对象(假设只有一个对象只能满足两个条件).

then you would still use a collection but simply return the first object (assuming that only one object could only ever satisfy both conditions).

要重用,请将此逻辑包装在方法中并放入模型中:

For reuse, wrap this logic in a method and place in your model:

public function loadByUserDevice($userId, $deviceId)
{
    $collection = $this->getResourceCollection()
        ->addFieldToFilter('user_id', $userId)
        ->addFieldToFilter('device_id', $deviceId)
        ->setCurPage(1)
        ->setPageSize(1)
    ;

    foreach ($collection as $obj) {
        return $obj;
    }
    return false;
}

您可以这样称呼它:

$userId = 1;
$deviceId = 3;
Mage::getModel('module/userdevice')->loadByUserDevice($userId, $deviceId);

注意:

您可以将loadByUserDevice缩短为以下内容,尽管在找不到对象的情况下也无法获得错误的返回值:

You could shorten the loadByUserDevice to the following, though you would not get the benefit of the false return value should no object be found:

public function loadByUserDevice($userId, $deviceId)
{
    $collection = $this->getResourceCollection()
        ->addFieldToFilter('user_id', $userId)
        ->addFieldToFilter('device_id', $deviceId)
    ;

    return $collection->getFirstItem();
}

这篇关于SQL对Magento模型的理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆