phpsqlmysqldatabasemagento

Magento SQL Queries


I am new to Magento and I want to know the SQL query that is generated while running the following code

   public function ws_getAllManagers($cust_id, $group_id)
   {
    $campusmanager = $ManagersData = $arr = array();
    array_push($arr, $cust_id);
    $collection = Mage::getModel("manager/campus")->getCollection()
                                                  ->addFieldToFilter('regionalmanager',array('finset' => $arr)) 
                                                  ->addFieldToFilter('status', array('eq' => 1));
    // rest of the code
   }

Solution

  • It appears that this is from a custom module, so I cannot give you the actual query, but I can show you how to get it. All collections have a getSelect() method that will return the actual query being used in it's current state. Once you get that, you can use Magento's Logging to put it into a file:

       public function ws_getAllManagers($cust_id, $group_id)
       {
        $campusmanager = $ManagersData = $arr = array();
        array_push($arr, $cust_id);
        $collection = Mage::getModel("manager/campus")->getCollection()
                                                      ->addFieldToFilter('regionalmanager',array('finset' => $arr)) 
                                                      ->addFieldToFilter('status', array('eq' => 1));
    
        // by default, this will add an entry to [magento-root]/var/log/system.log
        Mage::log($collection->getSelect());
    
        // rest of the code
       }
    

    Note: The output will be different if you were to log this before ->addFieldToFilter( ... and it will be different if the collection is altered later in your code, so make sure that you are doing this at the right time to get the information that you want.