Yii Framework Forum: Access query criteria from beforeFind() - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Access query criteria from beforeFind() Error in documentation? Rate Topic: -----

#1 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,991
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 12 July 2012 - 08:57 AM

In the class documentation for CActiveRecord::onBeforeFind it says:

Quote

This event is raised before an AR finder performs a find call. In this event, the CModelEvent::criteria property contains the query criteria passed as parameters to those find methods. If you want to access the query criteria specified in scopes, please use getDbCriteria(). You can modify either criteria to customize them based on needs.


This event is triggered from inside CActiveRecords with the beforeFind() method. But the criteria object is never passed along (see for example the implementation of query()). So $event->criteria is always null.

I wonder, how this is supposed to be used. I can hardly think of a situation where you would call beforeFind($criteria) manually.
0

#2 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,991
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 12 July 2012 - 09:02 AM

A little more background:

From a behavior i want to alter the query critieria on beforeFind() for the next query only. So i don't want to change the scope criteria with $this->owner->dbCriteria or something. I tried to modify $event->criteria (or set a new criteria if it's null) but found out that it does not work as expected (see above).

Any idea for a workaround?
0

#3 User is offline   zeroByte 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 39
  • Joined: 17-February 12

Posted 20 July 2012 - 11:20 AM

This is fixed in the current github version.
However, up to today this does _not_ work if the beforeFind() is applied to any relation.


Regards,
0

#4 User is offline   CeBe 

  • Standard Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 180
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 02 August 2012 - 06:04 AM

Just updated the documentation for CModelEvent::$criteria : https://github.com/y...t/yii/pull/1103

This property is not used anymore since yii 1.1.7.

Modifying $this->owner->dbCriteria is the right way since after a find the criteria will be reset to default scope. Changes are only valid for next query.
This currently does not work for relational queries:
Bug: https://github.com/y...t/yii/issues/93
Solution: https://github.com/y...t/yii/pull/1102
0

#5 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,991
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 07 September 2012 - 09:24 AM

There are still these wrong comments in CActiveRecord:


    /**                                                                                                                      
     * This event is raised before an AR finder performs a find call.                                                        
     * In this event, the {@link CModelEvent::criteria} property contains the query criteria                                 
     * passed as parameters to those find methods. If you want to access                                                     
     * the query criteria specified in scopes, please use {@link getDbCriteria()}.                                           
     * You can modify either criteria to customize them based on needs.                                                      
     * @param CModelEvent $event the event parameter                                                                         
     * @see beforeFind                                                                                                       
     */                                                                                                                      
    public function onBeforeFind($event)                                                                                     
    {                                                                                                                        
        $this->raiseEvent('onBeforeFind',$event);                                                                            
    }



Only the first sentence is still valid.

And here, findBySql and findAllBySql is missing:



    /**                                                                                                                      
     * This method is invoked before an AR finder executes a find call.                                                      
     * The find calls include {@link find}, {@link findAll}, {@link findByPk},                                               
     * {@link findAllByPk}, {@link findByAttributes} and {@link findAllByAttributes}.                                        
     * The default implementation raises the {@link onBeforeFind} event.                                                     
     * If you override this method, make sure you call the parent implementation                                             
     * so that the event is raised properly.                                                                                 
     */                                                                                                                      
    protected function beforeFind()                                                                                          
    {                                                                                                                        
        if($this->hasEventHandler('onBeforeFind'))                                                                           
        {                                                                                                                    
            $event=new CModelEvent($this);                                                                                   
            $this->onBeforeFind($event);                                                                                     
        }                                                                                                                    
    }

0

#6 User is offline   CeBe 

  • Standard Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 180
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 07 September 2012 - 03:04 PM

Thanks for reporting, just fixed the documentation.
https://github.com/y...74dbcb7ba0d7cb8
If you have anything to add, please add a comment on github.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users