0

I have this code, which is showing all the daily unsubscribe users..

public function getdailyUnSubs($modelName,$op){

    $currentDate = date('Ymd');
    $criteria = new CDbCriteria;
    $criteria->select = 'app_unsubs,sms_unsubs';

    $criteria->order = 'date DESC';
    $criteria->limit = 1;
    $criteria->offset = 0;
    $Subscriber = $modelName::model()->find($criteria);

    $totalunSubs = $Subscriber['app_unsubs']+$Subscriber['sms_unsubs'];

    return $totalunSubs;
}

But Now i want to get all the unsubscribed user so far. I want the sum of all the unsubscribed users.

Here is my code but it is not working.

public function getTotalUnSubs($modelName,$op){


    $criteria = new CDbCriteria;
    $criteria->select ='sum(app_unsubs) as appunsubs, sum(sms_unsubs) as smsunsubs';

    $Subscriber = $modelName::model()->find($criteria);

    $totalunSubs = $Subscriber['appunsubs']+$Subscriber['smsunsubs'];

    return $totalunSubs;
}

It is saying appunsubs and smsunsubs is not defined.

Even if i change the code to

$criteria->select ='(sum(app_unsubs)+ sum(sms_unsubs)) as totalunsubs';

and

$totalunSubs = $Subscriber['totalunsubs'];

It still says totalunsubs is undefined.

3 Answers 3

3

You have undefined appunsubs and smsunsubs because there are no fields like that in your $modelName model class. To fetch data you can create query based on criteria and execute this query using Yii::app()->db->createCommand($sql)->queryRow(). Check out this code:

public function getTotalUnSubs($modelName,$op)
{
    $criteria = new CDbCriteria;
    $criteria->select ='sum(app_unsubs) as appunsubs, sum(sms_unsubs) as smsunsubs';

    //creating proper SQL
    $sql = Yii::app()->db->commandBuilder->createFindCommand($modelName, $criteria)->getText();

    //fetching data based on created SQL stored in $sql variable
    $Subscriber = Yii::app()->db->createCommand($sql)->queryRow();

    $totalunSubs = $Subscriber['appunsubs']+$Subscriber['smsunsubs'];

    return $totalunSubs;
}
7
  • After using your code it is giving me an error and saying SubUsers doesnot exists. It is the name of my table. The error is on $sql = Yii::app()->db->commandBuilder->createFindCommand($modelName, $criteria)->getText(); May 31, 2016 at 10:28
  • Try to check what returned with this code: Yii::app()->db->getSchema()->getTable('SubUsers'). If it returns CDbTableSchema object, pass it to line where is an error instead of $modelName variable.
    – aslawin
    May 31, 2016 at 10:43
  • Where should i put this code? Yii::app()->db->getSchema()->getTable('SubUsers') May 31, 2016 at 10:50
  • Assign this to variable, for example: $subUsersTableSchema = Yii::app()->db->getSchema()->getTable('SubUsers'). Then check if it's not equals to null. If it's not equal to null, that means this table exists in your DB and you need to put this variable instead of $modelName; otherwise table doesn't exists and that's problem.
    – aslawin
    May 31, 2016 at 11:13
  • If you are 100% sure that table SubUsers exists in DB, maybe problem is that you need to clear application cache. You can do this by deleting all files from protected/runtime directory.
    – aslawin
    May 31, 2016 at 11:15
1

I think you have to set Group By into your query.

So code is now like this.

public function getTotalUnSubs($modelName,$op){


    $criteria = new CDbCriteria;
    $criteria->select ='sum(app_unsubs) as appunsubs, sum(sms_unsubs) as smsunsubs';
    $criteria->group='table primary key';

    $Subscriber = $modelName::model()->find($criteria);

    $totalunSubs = $Subscriber['appunsubs']+$Subscriber['smsunsubs'];

    return $totalunSubs;
}
1
  • My table doesnot have primary key. it doesnot need it May 31, 2016 at 10:32
1

The easy solution is to add public or protected variable names as sum into the model. I would suggest using it as I call it a "sub-model" ...

class Summary extends CActiveRecord {
   public $total;
}

class Item extends Summary {
   ...
}

and then you can freely use count or sum in criteria, they will appear as variables in model retrieved with find, findAll, etc.

$criteria = new CDbCriteria();
$criteria->select = "count(id) as total,type,lang,source";
$criteria->group = "lang,source,type"; 
$result = Item::model()->findAll($criteria);

and the result is: (as for example)

array(1) {
  [0]=>
  object(Item)#48 (12) {
    ["total"]=>
    string(2) "50"

Those sub-models are really great to keep your functions, relations and other stuff there, as if the class needs to be autogenerated from the Gii, you will just need to replace extends CActiveRecord with extends SubmodelName...

Also, during the years, I have developed Base (model) setting up everything I need in general (user_id to the logged-in user, check dates are formatted correctly, etc)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.