Magento加入查询结果

时间:2015-02-04 06:17:56

标签: php mysql magento join

我在Magento的两个表上运行连接查询,这是查询:

    $collection = Mage::getModel('sales/flat_shipment_track')->getCollection();
    $collection->getSelect()->join( array('shipment'=>
                 $this->getTable('sales/flat_shipment')), 'main_table.order_id = 
                 shipment.order_id', array('shipment.*'), 'schema_name_if_different');

我原本希望看到一些来自连接的行,但是当我var_dump($collection)时,我得到的是Mage_Sales_Model_Resource_Flat_Shipment_Track_Collection的对象,这是一个很大的对象,但是我没有看到实际的行,一些起始内容在这里:

object(Mage_Sales_Model_Resource_Flat_Shipment_Track_Collection)#35 (36) {
  ["_eventPrefix":protected]=>
  string(36) "sales_flat_shipment_track_collection"
  ["_eventObject":protected]=>
  string(30) "flat_shipment_track_collection"
  ["_orderField":protected]=>
  string(8) "order_id"
  ["_salesOrder":protected]=>
  NULL
  ["_model":protected]=>
  string(25) "sales/flat_shipment_track"
  ["_resourceModel":protected]=>
  string(25) "sales/flat_shipment_track"
  ["_resource":protected]=>
  object(Mage_Sales_Model_Resource_Flat_Shipment_Track)#56 (20) {
    ["_eventPrefix":protected]=>
    string(34) "sales_flat_shipment_track_resource"
    ["_grid":protected]=>
    bool(false)
    ["_useIsObjectNew":protected]=>
    bool(true)
    ["_useIncrementId":protected]=>
    bool(false)
    ["_entityTypeForIncrementId":protected]=>
    string(0) ""
    ["_virtualGridColumns":protected]=>
    NULL
    ["_gridColumns":protected]=>
    NULL
    ["_eventObject":protected]=>
    string(8) "resource"
    ["_resources":protected]=>
    object(Mage_Core_Model_Resource)#134 (5) {
      ["_connectionTypes":protected]=>
      array(0) {
      }
      ["_connections":protected]=>
      array(5) {
        ["core_write"]=>
        object(Varien_Db_Adapter_Pdo_Mysql)#104 (30) {
          ["_defaultStmtClass":protected]=>
          string(29) "Varien_Db_Statement_Pdo_Mysql"
          ["_transactionLevel":protected]=>
          int(0)
          ["_connectionFlagsSet":protected]=>
          bool(true)
          ["_ddlCache":protected]=>
          array(0) {
          }
          ["_bindParams":protected]=>
          array(0) {
          }
          ["_bindIncrement":protected]=>
          int(0)
          ["_debug":protected]=>
          bool(false)
          ["_logQueryTime":protected]=>
          float(0.05)
          ["_logAllQueries":protected]=>
          bool(false)
          ["_logCallStack":protected]=>
          bool(false)
          ["_debugFile":protected]=>
          string(23) "var/debug/pdo_mysql.log"
          ["_debugIoAdapter":protected]=>
          NULL
          ["_debugTimer":protected]=>
          int(0)
          ["_cacheAdapter":protected]=>
          object(Varien_Cache_Core)#14 (6) {
            ["_backend":protected]=>
            object(Zend_Cache_Backend_File)#19 (3) {
              ["_options":protected]=>
              array(9) {
                ["cache_dir"]=>
                string(28) "/var/www/magento/var/cache/"
                ["file_locking"]=>
                bool(true)
                ["read_control"]=>
                bool(true)
                ["read_control_type"]=>
                string(5) "crc32"
                ["hashed_directory_level"]=>
                int(1)
                ["hashed_directory_umask"]=>
                int(511)
                ["file_name_prefix"]=>
                string(4) "mage"
                ["cache_file_umask"]=>
                int(384)
                ["metadatas_array_max_size"]=>
                int(100)
              }
              ["_metadatasArray":protected]=>
              array(1) {
                ["176_CORE_CACHE_OPTIONS"]=>

Mage_Sales_Model_Resource_Flat_Shipment_Track_Collection由我创建,这是此类的内容:

class Mage_Sales_Model_Resource_Flat_Shipment_Track_Collection
    extends Mage_Sales_Model_Resource_Order_Collection_Abstract
{

    protected $_eventPrefix    = 'sales_flat_shipment_track_collection';

    protected $_eventObject    = 'flat_shipment_track_collection';

    protected $_orderField     = 'order_id';

    protected function _construct()
    {
        $this->_init('sales/flat_shipment_track');
    }

    public function setShipmentFilter($shipmentId)
    {
        $this->addFieldToFilter('parent_id', $shipmentId);
        return $this;
    }
}

如何获得加入两个表的结果,此查询应该返回15,000行。感谢

编辑:

我正在运行此查询:

SELECT count(DISTINCT sfs.`order_id`) 
            FROM livedb.`sales_flat_shipment_track` sfst
            JOIN livedb.`sales_flat_shipment` sfs ON sfs.`order_id` = sfst.`order_id`

这来自控制台返回15,000。

0 个答案:

没有答案