Change Function Sig on findByCondition

Why would you change a function signature on findByCondition(

  • Chg #7130: Changed the signature of ActiveRecord::findByCondition() to simplify the implementation and usage (Faryshta)

This is now returning an instance of activeQuery. So everywhere I have used this, I now need to go and turn this into an activerecord.

Old Function


protected static function findByCondition($condition, $one)

    {

        $query = static::find();


        if (!ArrayHelper::isAssociative($condition)) {

            // query by primary key

            $primaryKey = static::primaryKey();

            if (isset($primaryKey[0])) {

                $pk = $primaryKey[0];

                if (!empty($query->join) || !empty($query->joinWith)) {

                    $pk = static::tableName() . '.' . $pk;

                }

                $condition = [$pk => $condition];

            } else {

                throw new InvalidConfigException('"' . get_called_class() . '" must have a primary key.');

            }

        }


        return $one ? $query->andWhere($condition)->one() : $query->andWhere($condition)->all();

    }

New Function


protected static function findByCondition($condition)

    {

        $query = static::find();


        if (!ArrayHelper::isAssociative($condition)) {

            // query by primary key

            $primaryKey = static::primaryKey();

            if (isset($primaryKey[0])) {

                $pk = $primaryKey[0];

                if (!empty($query->join) || !empty($query->joinWith)) {

                    $pk = static::tableName() . '.' . $pk;

                }

                $condition = [$pk => $condition];

            } else {

                throw new InvalidConfigException('"' . get_called_class() . '" must have a primary key.');

            }

        }


        return $query->andWhere($condition);

    }

Why would you do this? Its causing me all sorts of headaches.

I have been thinking about this. And still dont see why this happened this way. First of all, this function extends ActiveRecord. Secondly all the other findX function return an instance of ActiveRecord, e.g. find(, findOne, findAll( etc…

If you wanted to allow the user to get the Active Query (Which is a perfectly reasonable request) why dont we give the function a different name, and leave the old one there. Or even make the old one return $one?static::newFuncitonName($condition)->one():static::newFunctionName($condition)->all();

Anyway I have fixe my code using the below which is not really ideal. :(


public static function findByConditionLayer2($condition, $one=false) 

    {

        $matches = [];

        if(preg_match('/([0-9]*)\.([0-9]*)\.([0-9]*)\-/', Yii::getVersion(), $matches))

        {

            if($matches[2] >= 3 && $matches[0]>=2) //less than 2.0.3

            {

                return parent::findByCondition($condition, $one); //Has To Return An Active Record.

            } 

            else

            {

                $activeQuery = parent::findByCondition($condition);

                return $one?$activeQuery->one():$activeQuery->all(); //Has to return an ActiveDBQuery

            }

        }

    }