首頁>Magento>source

我有一个名為 flagged_products的自定義表   列出了這樣的標記产品:

+-----------+------------+---------------------+
| entity_id | product_id |     flagged_on      |
+-----------+------------+---------------------+
|         1 |     356534 | 2019-03-01 12:45:20 |
|         2 |     654657 | 2019-01-07 05:52:09 |
|         3 |     345477 | 2019-03-23 09:34:07 |
|         4 |     114673 | 2019-04-11 15:45:17 |
+-----------+------------+---------------------+

我希望將其与目錄/产品模型結合起来,列出已標記為這樣的产品的所有名稱:

+-----------+--------------+---------------------+
| entity_id | product_name |     flagged_on      |
+-----------+--------------+---------------------+
|    356534 | Product 1    | 2019-03-01 12:45:20 |
|    654657 | Product 2    | 2019-01-07 05:52:09 |
|    345477 | Product 3    | 2019-03-23 09:34:07 |
|    114673 | Product 4    | 2019-04-11 15:45:17 |
+-----------+--------------+---------------------+

但是,由於产品名稱是EAV表的一部分,我不確定如何获取此資訊。

非常感谢Magento 1如何實現這一目標

最新回復
  • 2019-12-5
    1 #

    如果您想尝試"Magento方式",您可以尝試以下方式:

    $flaggedProducts = Mage::getModel('your_module/your_model')->getCollection();
    // Join with product table
    $flaggedProducts->getSelect()->joinLeft(
            array('cp' => $flaggedProducts->getTable('catalog/product'),
            'cp.entity_id=main_table.product_id',
            array(
                'product_entity_id' => new Zend_Db_Expr('cp.entity_id')
            )
        );
    // Join to get the name
        $nameAttribute = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'name');
        $nameTable = $nameAttribute->getBackendTable();
        $nameId = $nameAttribute->getAttributeId();
        // Get default store name value
        $flaggedProducts->getSelect()->joinLeft(
            array('nt' => $nameTable),
            'nt.attribute_id=' . $nameId . ' AND nt.entity_id = cp.entity_id AND nt.store_id = 0',
            array(
                'product_name' => new Zend_Db_Expr('nt.value')
            )
        );
    

    如果你只是想做一些sql查詢,可能是這樣的:

    SELECT fp.*, cpev_name.value FROM  flagged_products AS fp
    LEFT JOIN catalog_product_entity_varchar AS cpev_name
                 ON fp.product_id = cpev_name.entity_id AND cpev_name.attribute_id = 'put-here-the-id-of-the-attribute-name' 
    AND cpev_name.store_id = 0
    

  • 2019-12-5
    2 #

    請尝試此查詢,它会為您提供預期的輸出。

    select fp.*, cpev.value as product_name from flagged_products as fp
    left JOIN catalog_product_entity_varchar as cpev on cpev.entity_id = fp.product_id
    WHERE cpev.attribute_id=71
    

    如果您有任何疑問,請告诉我。

  • 2019-12-5
    3 #

    <?php
    $collection = Mage::getModel('catalog/product')
        ->getCollection()
        ->addAttributeToSelect('name')
        ->getSelect()
        ->joinLeft(
            array('flagged_products' => $this->getTable('flagged_products')),
            'e.entity_id = flagged_products.product_id'
        );
    

  • 如何在wySiwyG編輯器magento中添加新選項