最新消息:觉得本站不错的话 记得收藏哦 博客内某些功能仅供测试 讨论群:135931704 快养不起小站了 各位有闲钱就打赏下把 My Email weicots#gmail.com Please replace # with @

Magento数据操作&数据收集器 学习2

Magento 资料整理 ajiang-tuzi 4786浏览
<?php
/**
 * Created by PhpStorm.
 * User:ajiang-tuzhi
 * QQ:1050653098@qq.com
 * WebSite:www.weicot.com
 * Date: 2015/11/12
 * Time: 11:54
 */
class Aps_Weicot_productsController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        /*模型数据收集器(Model Collections)
前面我们有提到,
        所有Magento的模型数据收集器都继承Varien_Data_Collectionm,
        所以理论上我们可以使用之前的所有方法。下面让我们以product模型实战下。*/

        $COP = Mage::getModel('catalog/product')->getCollection();
        $COP->getFirstitem()->getData();
        //array(11) { ["entity_id"]=> string(1) "1"
        // ["entity_type_id"]=> string(1) "4"
        // ["attribute_set_id"]=> string(1) "4"
        // ["type_id"]=> string(6) "simple"
        // ["sku"]=> s
        /*基本所有的Magento模型都有个方法叫getCollection默认情况下,
        它会返回系统中所有的数据。
Magento的数据收集器Collection包含很多复杂的逻辑来处理数据,
        无论是否使用索引或缓存、EAV表等。
上面的产品数据收集器,它里面还有Varien_Data_Collection_Db类。
        这个类给你很多有用的方法,例如如果你向看sql的select语句。*/
        $COP->getSelect();//object(Varien_Db_Select)#40 (4)
        // { ["_bind":protected]=> array ...Zend_Cache_Backend_File
        // Zend_Cache_Core...
        //var_dump((string)$COP->getSelect());
        //上面的方法将输出
        /*"SELECT `e`.*, `price_index`.`price`,
        `price_index`.`tax_class_id`, `price_index`.
        `final_price`, IF(price_index.tier_price IS NOT NULL,
        LEAST(price_index.min_price, price_index.tier_price),
         price_index.min_price) AS `minimal_price`, `price_index`.`min_price`, `price_index`.`max_price`, `price_index`.`tier_price`
        FROM `catalog_product_entity` AS `e`
        INNER JOIN `catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND price_index.website_id = '1'
         AND price_index.customer_group_id = 0"
        这个差异取决于你选择的字段,同样也涉及到索引和缓存。
        如果你看过之前的文章,那么你应该知道很多Magento表是使用Eav表结构的
        ,默认情况下一个eav的数据收集器将不会包含所有的对象字段
        ,你可以通过addAttributeToSelect来添加它们。让我们看看例子。
         */
        $COFS = Mage::getModel('catalog/product')
            ->getCollection()
            // ->addAttributeToSelect('*');//the asterisk is like a sql select
            /*SELECT `e`.*, `price_index`.
            `price`, `price_index`.`tax_class_id`,
            `price_index`.`final_price`,
             IF(price_index.tier_price IS NOT NULL, LEAST(price_index.min_price,
            price_index.tier_price), price_index.min_price) AS `minimal_price`,
            `price_index`.`min_price`, `price_index`.`max_price`, `price_index`.
            `tier_price` FROM `catalog_product_entity` AS `e`
            INNER JOIN `catalog_product_index_price` AS `price_index`
             ON price_index.entity_id = e.entity_id AND
             price_index.website_id = '1' AND price_index.customer_group_id = 0
            或者你也可以只选某一个字  or just one*/
            ->addAttributeToSelect('meta_title')
        /*

        SELECT `e`.*, `price_index`.`price`, `price_index`.
        `tax_class_id`, `price_index`.`final_price`,
         IF(price_index.tier_price IS NOT NULL,
        LEAST(price_index.min_price, price_index.tier_price),
         price_index.min_price) AS `minimal_price`, `price_index`.`min_price`,
         `price_index`.`max_price`, `price_index`.`tier_price` FROM
        `catalog_product_entity` AS `e` INNER JOIN `catalog_product_index_price` AS `
        price_index` ON price_index.entity_id = e.entity_id AND price_index.website_id = '1'
         AND price_index.customer_group_id = 0
*************************** 1. row ***************************
       entity_id: 1
  entity_type_id: 4
attribute_set_id: 4
         type_id: simple
             sku: puma 2012080705
     has_options: 1
required_options: 0
      created_at: 2015-11-12 08:55:04
      updated_at: 2015-11-12 08:55:04
           price: 64.9900
    tax_class_id: 0
     final_price: 58.4900
   minimal_price: 58.4900
       min_price: 58.4900
       max_price: 58.4900
      tier_price: NULL
*************************** 2. row ***************************
       entity_id: 2
  entity_type_id: 4
attribute_set_id: 4
         type_id: simple
             sku: puma 2012080706
     has_options: 1
required_options: 0
      created_at: 2015-11-12 08:55:13
      updated_at: 2015-11-12 08:55:13
           price: 199.9900
    tax_class_id: 0
     final_price: 179.9900
   minimal_price: 179.9900
       min_price: 179.9900
       max_price: 179.9900
      tier_price: NULL
*************************** 3. row ***************************
       entity_id: 3

        //或者更多
*/
       ->addAttributeToSelect('price');

    }
    public function getAction(){
        //http://127.0.0.1/Blck/MAps/mg_1/weicot/products/get
        //过滤数据(Filtering Database Collections)
        //最重要的一个方法是addFieldToFilter。通过这个方法可以添加我们sql中的WHERE语句。
        $COP = Mage::getModel('catalog/product')->getCollection();
        $COP->addFieldToFilter('sku','10086');
        //实践者注:这个将会获得 所有有关这个的数据
        //addFieldToFilter方法中的第一个参数是你想过滤的字段名称,
        ////第二个是你想过滤的值。例如刚刚sku是字段名称,10086是值
        //第二个参数也可以被用来指定某一类型的数据。
       $COP->getFirstitem()->getData();//end
        /*array(11) { ["entity_id"]=> string(2) "12"
        ["entity_type_id"]=> string(1) "4" ["attribute_set_id"]=> string(1) "4"
        ["type_id"]=> string(6) "simple" ["sku"]=> string(5) "10086" ["has_options"]=> string(1) "1"
         ["required_options"]=> string(1) "1" ["created_at"]=> string(19) "2015-11-13 01:43:53"
         ["updated_at"]=> string(19) "2015-11-13 02:21:14" ["is_salable"]=> string(1) "1"
         ["stock_item"]=> object(Varien_Object)#32 (7) { ["_data":protected]=> array(1) {
        ["is_in_stock"]=> string(1) "1" } ["_hasDataChanges":protected]=> bool(false)
        ["_origData":protected]=> NULL ["_idFieldName":protected]=> NULL
         ["_isDeleted":protected]=> bool(false)
        ["_oldFieldsMap":protected]=> array(0) { }
        ["_syncFieldsMap":protected]=> array(0) { } } }*/
        $T1=(string)Mage::getModel('catalog/product')
            ->getCollection()
            ->addFieldToFilter('sku','10086')
            ->getSelect();
            //tring(77) "SELECT `e`.* FROM `catalog_product_entity` AS `e` WHERE (`e`.`sku` = '10086')"
        //但是这个很快会变得很复杂。试着做下面的练习。
        var_dump((string)Mage::getModel('catalog/product')
            ->getCollection()
            ->addAttributeToSelect('*')
            ->addFieldToFilter('meta_title','my titlt')//过滤字段
           // ->getSelect()
        );
        /*SELECT `e`.*, IF(at_meta_title.value_id > 0, at_meta_title.value, at_meta_title_default.value)
        AS `meta_title` FROM `catalog_product_entity` AS `e` INNER JOIN `catalog_product_entity_varchar`
        AS `at_meta_title_default` ON (`at_meta_title_default`.`entity_id` = `e`.`entity_id`)
        AND (`at_meta_title_default`.`attribute_id` = '82') AND `at_meta_title_default`.`store_id` = 0
        LEFT JOIN `catalog_product_entity_varchar` AS `at_meta_title` ON (`at_meta_title`.`entity_id` = `e`.`entity_id`)
        AND (`at_meta_title`.`attribute_id` = '82')
        AND (`at_meta_title`.`store_id` = 1)
        WHERE (IF(at_meta_title.value_id > 0, at_meta_title.value, at_meta_title_default.value) = 'my titlt')
         *在你有空的时候可以好好研究下上面的sql语句,
         * 我们先不转移焦点,继续我们下面的讲解。
        其它比较运算符
        我确定在刚刚的练习中,你想知道如何实现一个不是“=”的where条件句,例如不等于、大于、
        小于。刚刚我们有讲过addFieldToFilter的第二个参数允许传入不同“类型”。
        其实很简单,只要将一个简单的数组作为第二个参数传入addFieldToFilter方法就可以变换条件句。
        数组的键就是“类型”,关联的值就是你想过滤的值。我们改写下上面的代码。

         *  */
        var_dump((string)Mage::getModel('catalog/product')
            ->getCollection()
            ->addFieldToFilter('sku',array('eq'=>'10086'))//过滤字段
                //string(0) "" string(77) "SELECT `e`.* FROM `catalog_product_entity` AS `e` WHERE (`e`.`sku` = '10086')"
            ->getSelect()
            /*看上面的过滤器
            addFieldToFilter('sku',array('eq'=>'n2610'))
             * 看上面的过滤器
             addFieldToFilter('sku',array('eq'=>'n2610'))
             正如你看到的,第二个参数是一个php的数组。它的键是“eq”,代表等于的意思。
              到参考--资料察看 在这里我将Magento所有的条件判断符号列出来供大家参考。
                */
        /*
         * array("eq"=>'n2610')
WHERE (e.sku = 'n2610')

array("neq"=>'n2610')
WHERE (e.sku != 'n2610')

array("like"=>'n2610')
WHERE (e.sku like 'n2610')

array("nlike"=>'n2610')
WHERE (e.sku not like 'n2610')

array("is"=>'n2610')
WHERE (e.sku is 'n2610')

array("in"=>array('n2610'))
WHERE (e.sku in ('n2610'))

array("nin"=>array('n2610'))
WHERE (e.sku not in ('n2610'))

array("notnull"=>'n2610')
WHERE (e.sku is NOT NULL)

array("null"=>'n2610')
WHERE (e.sku is NULL)

array("gt"=>'n2610')
WHERE (e.sku > 'n2610')

array("lt"=>'n2610')
WHERE (e.sku < 'n2610')

array("gteq"=>'n2610')
WHERE (e.sku >= 'n2610')

array("moreq"=>'n2610') //a weird, second way to do greater than equal
WHERE (e.sku >= 'n2610')

array("lteq"=>'n2610')
WHERE (e.sku <= 'n2610')

array("finset"=>array('n2610'))
WHERE (find_in_set('n2610',e.sku))

array('from'=>'10','to'=>'20')
WHERE e.sku >= '10' and e.sku <= '20'
其中大多数是自我的理解,但有几个得特别注意。
in, nin, find_in_set
in and nin 条件句中,语序你传入一个数组作为值。例如:
array("in"=>array('n2610','ABC123')
WHERE (e.sku in ('n2610','ABC123'))
notnull, null
关键字NULL是最特殊的sql句,它将忽略你传入的值。
array("notnull"=>'n2610')
WHERE (e.sku is NOT NULL)
from – to 过滤
这是另一种过滤方式,在传入的数组中,允许你传入两个键,是从哪里到哪里的意思,一个数值区间。

         *
         * */
        );
    }

    public function  CopAction(){
        var_dump((string)Mage::getModel('catalog/product')
            ->getCollection()
            ->addFieldToFilter('price',array('from'=>'40','to'=>'60'))//过滤字段
            //string(0) "" string(77) "SELECT `e`.* FROM `catalog_product_entity` AS `e` WHERE (`e`.`sku` = '10086')"
            ->getSelect());
        /*mysql> SELECT `e`.*, `at_price`.`value` AS `price` FROM `catalog_product_entity` AS `e` INNER JOIN `
        catalog_product_entity_decimal` AS `at_price` ON (`at_price`.`entity_id` = `e`.`entity_id`) AND
        (`at_price`.`attribute_id` = '75') AND (`at_price`.`store_id` = 0) WHERE (at_price.value >= '40' AND at_price.value <= '60');
+-----------+----------------+------------------+---------+-----------------+-------------+------------------+---------------------+---------------------+---------+
| entity_id | entity_type_id | attribute_set_id | type_id | sku             | has_options | required_options | created_at          | updated_at          | price   |
+-----------+----------------+------------------+---------+-----------------+-------------+------------------+---------------------+---------------------+---------+
|         4 |              4 |                4 | simple  | puma 2012080711 |           1 |                0 | 2015-11-12 08:55:18 | 2015-11-12 08:55:18 | 59.9900 |
|         5 |              4 |                4 | simple  | puma 2012080714 |           1 |                0 | 2015-11-12 08:55:20 | 2015-11-12 08:55:20 | 59.9900 |
|        10 |              4 |                4 | simple  | puma 2012080725 |           1 |                0 | 2015-11-12 08:55:31 | 2015-11-12 08:55:31 | 49.9000 |
|        11 |              4 |                4 | simple  | puma 2012080726 |           1 |                0 | 2015-11-12 08:55:33 | 2015-11-12 09:45:29 | 49.9000 |
|        12 |              4 |                4 | simple  | 10086           |           1 |                1 | 2015-11-13 01:43:53 | 2015-11-13 02:21:14 | 49.9000 |
+-----------+----------------+------------------+---------+-----------------+-------------+------------------+---------------------+---------------------+---------+
5 rows in set (0.03 sec)

        面等同于
WHERE (_table_price.value >= '10' AND _table_price.value <= '20')
AND 或者 OR
根据刚才讲的内容,你可以知道,通过多个 addFieldToFilter方法可以获得一个”AND”的条件句。
        */
    }
   public function Mcop()
    {
        echo(
        (string)
        Mage::getModel('catalog/product')
            ->getCollection()
            ->addFieldToFilter('sku',array('like'=>'a%'))
            ->addFieldToFilter('sku',array('like'=>'b%'))
            ->getSelect()
        );
    }
    /*等同于下面的子句WHERE (e.sku LIKE 'a%') AND (e.sku LIKE 'b%')*/
public function Mcop1tAction()
{
    /*WHERE (e.sku LIKE 'a%') AND (e.sku LIKE 'b%')
但是,聪明的你可以发现,上面的例子不可能返回任何结果,因为一个sku不可能以a开头,同时也以b开头。
我们希望用的应该是”OR”,那么如何实现呢?这又使我们将焦点集中到了addFieldToFilter方法的第二个参数上。
如果你希望构造一个or的语句,首先我们构造两个参数。*/
        $filter_a = array('like'=>'a%');
        $filter_b = array('like'=>'b%');
        echo(
        (string)
        Mage::getModel('catalog/product')
        ->getCollection()
        ->addFieldToFilter('sku',array($filter_a,$filter_b))
        ->getSelect()
        );
}
/*你可以看到这样的一个子句。
WHERE (((e.sku LIKE 'a%') OR (e.sku LIKE 'b%')))
总结
恭喜你,你现在已经是一个很不错的Magento开发者了!因为你不需要写任何sql语句,就可以获取几乎所有模型的所有你想要的数据。

*/

    //好了来咱们来动手练看看
    public function getinfoAction(){
        $products=Mage::getModel('catalog/product')
            ->getCollection()
            ->addAttributeToFilter('sku','10086')
            ->addAttributeToSelect('*')
            ->load();
        foreach($products as $_product){
            print_r($_product->getData());
        }
    }
    /*
     * rray ( [entity_id] => 12
     * [entity_type_id] => 4 [attribute_set_id] => 4 [type_id] => simple
     * [sku] => 10086 [has_options] => 1 [required_options] => 1 [created_at] => 2015-11-13 01:43:53 [updated_at] => 2015-11-13 02:21:14 [manufacturer] => [color] => [status] => 1 [is_recurring] => 0 [visibility] => 4 [tax_class_id] => 0 [ac] => 5 [price] => 49.9000 [special_price] => 49.9000 [weight] => 1.0000 [msrp] => [name] => Puma Youth PowerCat 3.12 Gravity FG - Fluo Blue with New Navy and Orange [meta_title] => [meta_description] => [image] => /P/u/Puma_Youth_PowerCat_3.12_Gravity_FG_-_Fluo_Blue_with_New_Navy_and_Orange_1.jpg [small_image] => /P/u/Puma_Youth_PowerCat_3.12_Gravity_FG_-_Fluo_Blue_with_New_Navy_and_Orange_1.jpg [thumbnail] => /P/u/Puma_Youth_PowerCat_3.12_Gravity_FG_-_Fluo_Blue_with_New_Navy_and_Orange_1.jpg  => /Puma Youth PowerCat 3.12 Gravity FG - Fluo Blue with New Navy and Orange_138.jpg;/soccercleatsus87/Puma Youth PowerCat 3.12 Gravity FG - Fluo Blue with New Navy and Orange_139.jpg;
     * /soccercleatsus87/Puma Youth PowerCat 3.12 Gravity FG - Fluo Blue with New [url_key] => puma-youth-powercat-3-12-gravity*/
}

链接以及参考
Magento数据操作&数据收集器 学习
表达式中的运算符EQ NE GT GE LT LE…..

转载请注明:(●--●) Hello.My Weicot » Magento数据操作&数据收集器 学习2

蜀ICP备15020253号-1