Error with scopes

Good afternoon

I created a scope, this:




public function scopes() 

{

  return array('porcentagemAtual'=>array('order'=>'id DESC limit 1'));

}



This scope read the last line of DB, and only the last.

When I call this scopes, its returns null. But in my DB there are many data. This is my call:




public function getPorcentagemPontos(){

            $porcentagem = Porcentagem::model()->porcentagemAtual()->findAll();

            return $porcentagem->porcentagem;

        }



I print for test and show me null, whether I change the code for this:




 public function getPorcentagemPontos(){

         $porcentagem = Porcentagem::model()->findbyPk(3);

           return $porcentagem->porcentagem;

        }




The code return de right value. Where I wrong on my scope?

I try this:


return array('porcentagemAtual' => array('order' => 'id'));

But return null too.

This is my model:




<?php

class Porcentagem extends CActiveRecord

{

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	public function tableName()

	{

		return 'porcentagem';

	}

	public function rules()

	{

		return array(

			array('data, porcentagem, id_usuario', 'required'),

			array('id_usuario', 'numerical', 'integerOnly'=>true),

			array('porcentagem', 'numerical'),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('id, data, porcentagem, id_usuario', 'safe', 'on'=>'search'),

		);

	}


	public function relations()

	{

		return array(

                    'usuario' => array(self::BELONGS_TO,'Usuarios','id_usuario'),

		);

	}


	public function attributeLabels()

	{

		return array(

			'id' => 'Id',

			'data' => 'Data',

			'porcentagem' => 'Porcentagem',

			'id_usuario' => 'Id Usuario',

		);

	}


	public function search()

	{


		$criteria=new CDbCriteria;


		$criteria->compare('id',$this->id);


		$criteria->compare('data',$this->data,true);


		$criteria->compare('porcentagem',$this->porcentagem);


		$criteria->compare('id_usuario',$this->id_usuario);


		return new CActiveDataProvider('Porcentagem', array(

			'criteria'=>$criteria,

		));

	}

        

        public function scopes() 

        {

            return array('porcentagemAtual'=>array('order'=>'id DESC limit 1'));

            

        }

}



Hi,

first you should define limit like you did with order:




public function scopes() 

{

  return array('porcentagemAtual'=>array('order'=>'id DESC', 'limit'=>1));

}



second findAll returns list of active records satisfying the specified condition - an empty array is returned if none is found.

So either use ‘find’ and




$porcentagem->porcentagem



or ‘findAll’ and




$porcentagem[0]->porcentagem



Thanks for help

I did the changes and fix the problem.

I have another doubt. There is something in Yii to find of last line of my database? For exemplo :

" $porcentagem = Porcentagem::model()->findLastLine;"