Yii Framework Forum: Override defaultScope - Yii Framework Forum

Jump to content

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

Override defaultScope Rate Topic: -----

#1 User is offline   mintao 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 02-December 09
  • Location:Munich, Germany

Posted 08 May 2010 - 11:53 AM

My defaultScope looks like this:

    public function defaultScope()
    {
        return array(
            'condition' => '`is_active` = 1, `is_verified` = 1, `is_banned` = 0',
            'order' => 'position ASC',
        );
    }


This defaultScope thing is really great for the frontend of my web project, but backstage I'd like to see ALL entries. Is there a possibility to override the defaultScope? Something like:
Product::model()->findAll(array('defaultScope' => false));


I know there are named scopes, so please don't explain my how to use them ;) As there are many relations between tables and every table has it's own defaultScopes I don't want remember all those scope names :)
0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,781
  • Joined: 17-January 09
  • Location:Russia

Posted 08 May 2010 - 12:33 PM

Since 1.1.2 you can do the following:

Product::model()->resetScope()->findAll();

Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#3 User is offline   mintao 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 02-December 09
  • Location:Munich, Germany

Posted 08 May 2010 - 12:36 PM

Grrreat! Thanks again, samdark! .... but it doesn't work :-(

Controller:
$this->_model = ListType::model()->resetScope()->findbyPk($typeId);


SQL-Log:
SELECT * FROM `ListType` `ListType` WHERE (`ListType`.`is_active` = 1) AND (`ListType`.`id`='32') LIMIT 1


Model:
public function defaultScope()
    {
        return array(
            'alias' => $this->tableName(),
            'condition' => sprintf('`%s`.`is_active` = 1', $this->tableName())
        );
    }

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