FindAll() and beforeFind()

Hi,

Do beforeFind code apply to the findAll() method?

I got this situation




<?php

class MyUser extends CActiveRecord {


 protected function beforeFind() {

    $criteria = new CDbCriteria;

    $criteria->condition = "username <> ''";

    $criteria->order = "username ASC";

    $this->dbCriteria->mergeWith($criteria);

    parent::beforeFind();

  }


}

?>



and when i call MyUser::model()->findAll() it returns all the users and not those that username <> ‘’

According to the documentation, beforeFind() should apply to , find(), findAll(), findByPk(), findAllByPk(), findByAttributes(), findAllByAtributes().

Maybe you should call the parent implementation as the first thing…

I have already done this.

I also added a behavior to my model but it didn’t help too.

If I use this code




<?php 

protected function beforeFind() {

   parent::beforeFind();

    $criteria = new CDbCriteria;

    $criteria->condition = "username <> ''";

    $criteria->order = "username ASC";

    $this->dbCriteria->mergeWith($criteria);

    CVarDumper::dump($this->dbCriteria,2,true);

    

  }

?>



I get




<?php

CDbCriteria#1

(

    [CDbCriteria:_paramCount] => 0

    [select] => '*'

    [distinct] => false

    [condition] => 'username <> '''

    [params] => array()

    [limit] => -1

    [offset] => -1

    [order] => 'username ASC'

    [group] => ''

    [join] => ''

    [having] => ''

    [with] => null

    [alias] => null

) 

?>



The conditions are in the models criteria but are not applied

You should not use beforeFind() in this situation.

Use defaultScope instead:




public function defaultScope()

{

    return array(

        'condition'=>"username <> ''",

        'order'=>"username ASC",

    );

}



Thanks, that worked :rolleyes: