Yii Framework Forum: Scopes, Group, Count - how? - Yii Framework Forum

Jump to content

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

Scopes, Group, Count - how? Rate Topic: -----

#1 User is offline   MaxLord 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 31-March 12

Posted 28 June 2012 - 05:12 AM

I have this code, which counts how many of each modification are "on the way":


        $sql = ('select count(*) as amount, modification_id
        from products_store
        where  

        products_store.archived=0
        and (received=0 OR (received=1 and place_id=0 and receive_date<(NOW() - INTERVAL 1 DAY)))
        and defected!=1 and sent_to_costumer!=1

        group by modification_id');
        $command = Yii::app()->db->createCommand($sql);
        $dataReader = $command->query();

        foreach($dataReader as $row){
            @$this->_on_the_way[$row['modification_id']]=$row['amount'];
        }


So, "on the way" is a scope, and I want to use it in other places without repeating
        archived=0
        and (received=0 OR (received=1 and place_id=0 and receive_date<(NOW() - INTERVAL 1 DAY)))
        and defected!=1 and sent_to_costumer!=1


I defined a scope in ProductOnStore model:

    public function scopes(){
        return array(
            'onTheWay'=>array(
                'condition'=>'
                  products_store.archived=0
                  and (received=0 OR (received=1 and place_id=0 and receive_date<(NOW() - INTERVAL 1 DAY)))
                  and defected!=1 and sent_to_costumer!=1
                 '
            ),
        );
    }



Now I wonder, how to remake first code, using scope.
I don't see any ways of group and count under scope.

This returns all modifications, even those which have no any productsOnStore

        $items=ProductModification::model()->findAll(
            array(
                'with'=>array(
                    'productsOnStore'=>array(
                        'scopes'=>array(
                            'onTheWay',
                        )
                    ),
                ),
            )
        );


@MaxZhuravlev
0

#2 User is offline   Iran 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 27-September 12

Posted 19 November 2012 - 02:39 PM

I think you can use the select and group of CDbCriteria =>

 public function scopes(){
        return array(
            'onTheWay'=>array(
                'select' => 'count(*)',
                'condition'=>'
                  products_store.archived=0
                  and (received=0 OR (received=1 and place_id=0 and receive_date<(NOW() - INTERVAL 1 DAY)))
                  and defected!=1 and sent_to_costumer!=1',
                'group' => 'modification_id' 
            ),
        );
    }


I don't test this...
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