Know Magento
Available conditionals for ...

Available conditionals for addAttributeToFilter()

Category: Uncategorized – Posted Wednesday, May 27th, 2009.

I got a little tired of always referencing a code file in order to remember the conditionals for addAttributeToFilter(). I’ll come back to this post to clean it up and make it an easier reference, but for now it’s just a code reference in an easy place to find it ;)

    /**
     * Build SQL statement for condition
     *
     * If $condition integer or string - exact value will be filtered
     *
     * If $condition is array is - one of the following structures is expected:
     * - array("from"=>$fromValue, "to"=>$toValue)
     * - array("like"=>$likeValue)
     * - array("neq"=>$notEqualValue)
     * - array("in"=>array($inValues))
     * - array("nin"=>array($notInValues))
     *
     * If non matched - sequential array is expected and OR conditions
     * will be built using above mentioned structure
     *
     * @param string $fieldName
     * @param integer|string|array $condition
     * @return string
     */
    protected function _getConditionSql($fieldName, $condition) {
        if (is_array($fieldName)) {
            foreach ($fieldName as $f) {
                $orSql = array();
                foreach ($condition as $orCondition) {
                    $orSql[] = "(".$this->_getConditionSql($f[0], $f[1]).")";
                }
                $sql = "(".join(" or ", $orSql).")";
            }
            return $sql;
        }

        $sql = '';
        $fieldName = $this->_getConditionFieldName($fieldName);
        if (is_array($condition)) {
            if (isset($condition['from']) || isset($condition['to'])) {
                if (isset($condition['from'])) {
                    if (empty($condition['date'])) {
                        if ( empty($condition['datetime'])) {
                            $from = $condition['from'];
                        }
                        else {
                            $from = $this->getConnection()->convertDateTime($condition['from']);
                        }
                    }
                    else {
                        $from = $this->getConnection()->convertDate($condition['from']);
                    }
                    $sql.= $this->getConnection()->quoteInto("$fieldName >= ?", $from);
                }
                if (isset($condition['to'])) {
                    $sql.= empty($sql) ? '' : ' and ';

                    if (empty($condition['date'])) {
                        if ( empty($condition['datetime'])) {
                            $to = $condition['to'];
                        }
                        else {
                            $to = $this->getConnection()->convertDateTime($condition['to']);
                        }
                    }
                    else {
                        $to = $this->getConnection()->convertDate($condition['to']);
                    }

                    $sql.= $this->getConnection()->quoteInto("$fieldName <= ?", $to);
                }
            }
            elseif (isset($condition['eq'])) {
                $sql = $this->getConnection()->quoteInto("$fieldName = ?", $condition['eq']);
            }
            elseif (isset($condition['neq'])) {
                $sql = $this->getConnection()->quoteInto("$fieldName != ?", $condition['neq']);
            }
            elseif (isset($condition['like'])) {
                $sql = $this->getConnection()->quoteInto("$fieldName like ?", $condition['like']);
            }
            elseif (isset($condition['nlike'])) {
                $sql = $this->getConnection()->quoteInto("$fieldName not like ?", $condition['nlike']);
            }
            elseif (isset($condition['in'])) {
                $sql = $this->getConnection()->quoteInto("$fieldName in (?)", $condition['in']);
            }
            elseif (isset($condition['nin'])) {
                $sql = $this->getConnection()->quoteInto("$fieldName not in (?)", $condition['nin']);
            }
            elseif (isset($condition['is'])) {
                $sql = $this->getConnection()->quoteInto("$fieldName is ?", $condition['is']);
            }
            elseif (isset($condition['notnull'])) {
                $sql = "$fieldName is NOT NULL";
            }
            elseif (isset($condition['null'])) {
                $sql = "$fieldName is NULL";
            }
            elseif (isset($condition['moreq'])) {
                $sql = $this->getConnection()->quoteInto("$fieldName >= ?", $condition['moreq']);
            }
            elseif (isset($condition['gt'])) {
                $sql = $this->getConnection()->quoteInto("$fieldName > ?", $condition['gt']);
            }
            elseif (isset($condition['lt'])) {
                $sql = $this->getConnection()->quoteInto("$fieldName < ?", $condition['lt']);
            }
            elseif (isset($condition['gteq'])) {
                $sql = $this->getConnection()->quoteInto("$fieldName >= ?", $condition['gteq']);
            }
            elseif (isset($condition['lteq'])) {
                $sql = $this->getConnection()->quoteInto("$fieldName <= ?", $condition['lteq']);
            }
            elseif (isset($condition['finset'])) {
                $sql = $this->getConnection()->quoteInto("find_in_set(?,$fieldName)", $condition['finset']);
            }
            else {
                $orSql = array();
                foreach ($condition as $orCondition) {
                    $orSql[] = "(".$this->_getConditionSql($fieldName, $orCondition).")";
                }
                $sql = "(".join(" or ", $orSql).")";
            }
        } else {
            $sql = $this->getConnection()->quoteInto("$fieldName = ?", (string)$condition);
        }
        return $sql;
    }

  • KeakDaSneak

    they should change the name from magento to monstrocity!

  • Jeff

    The more I learn, the more I like it. It’s just a shame there’s such a huge lack of documentation. I could code projects two to three times faster if I had a better idea of what certain methods (functions) did and how they respond to given arguments. But overall though it gets a huge thumbs up from me so far.

  • Dennis

    @Jeff
    For sure one could develop things at least 10 times faster without that lack of documentation — its horrible.

    Thanks for that piece of code – easy to find at google ;)

    Best

    Dennis

  • http://alandubs.nfshost.com alandubs

    I’m only just starting out with it, for sure there is a steep learning curve but I’m getting more comfortable with it everyday. Thanks for this article, saved me a headache for sure :)

  • http://www.sharpdotinc.com Mike D

    Nice Post! I have referenced this quite a few times now.

  • Ben DeMott

    The amount of calls that must occur, and memory that must be consumed to perform 1 SQL Query is staggering.
    If this much code is required to abstract the internals to the concept of a ‘collection’ something is wrong.

    This type of ‘elegance’ will eventually become a prison, in which the overhead to make any changes is so massive its impractical.

    Some fun facts:
    To create 1 simple product in Magento and save it requires over 33,000 class, and method calls.
    78% of Simple product creation time is spent refreshing indexes (introduced in 1.4)
    To import a store with 80,000 products 10% of which are configurable will take around 24 hours.
    (Restoring the same data-store with direct sql queries takes around 2 minutes)

    A very nice product, poorly conceived from a developers perspective – with no regard to efficiencies.

  • http://www.netismine.com/magento/magento-sql-conditionals Magento SQL conditionals | Magento custom development and freelance programming

    [...] time ago, I found a helpful post that lists available Magento SQL conditionals for managing collections. It maybe shows that Magento doesn’t have a broad support for SQL [...]

Buy Magento Books and learn more tips and guides

Copyright © 2011 KnowMagento.com. All rights reserved. Designed by . Privacy Policy