Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8123375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:09:45+00:00 2026-06-06T06:09:45+00:00

Understanding Magento Models by reference of SQL: select * from user_devices where user_id =

  • 0

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

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T06:09:47+00:00Added an answer on June 6, 2026 at 6:09 am

    You are referring to Collections

    Some references for you:

    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.

    Syntax for this is:

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

    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() or simply count($yourCollection)

    EDIT

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

    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.

    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;
    }
    

    You would call this as follows:

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

    NOTE:

    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();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My understanding of Magento is that you can only have 2 themes loaded at
My understanding is that the LinqToSql pseudolanguage describes a set using a syntax very
My understanding is that width: 100% lets the element's width be the same as
My understanding is that count and length should return the same number for Ruby
My understanding of Hibernate is that as objects are loaded from the DB they
I am attempting to load products using the Magento Core API product.create method. Magento
I am using Magento 1.6 to build an e-commerce store. I want to show
My understanding of Linq to Sql is it will take my Linq statement and
My understanding is that GREATEST() and LEAST() are not part of the SQL standard,
I am using a modified version of blank_seo theme for Magento with the ES_ES

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.