Property "cdbcommand.queryscalar" Is Not Defined.

I have this code


public function afterSave() {


        $command = Yii::app()->db->createCommand('SELECT SUM(subtotal) FROM pembelian_detail WHERE pembelian_detail.id_pembelian=:id_pembelian');

        $command->bindValue(':id_pembelian', $this->id_pembelian);

        $update=Pembelian::model()->update(array($this->total => $command->queryScalar));

        $update->save();


    }

I’m not sure if the queryScalar placement is correct, because this code makes this error :

anyone can help? thanks

What exactly you want to do?

I want to count the total from subtotals. but this total column and subtotal column are in different table in db. I never use this kind of createCommand or queryScalar code before, so that’s why it’s a little bit confusing for me. :mellow:

Is there update method? (I dont remember)

Anyway check with echo end die() php function if the $command->queryScalar returns that you want.

Secondary, I dont think the update paramater is $this->total => $command->queryScalar but somthig like that

$update=Pembelian::model()->update(array(‘total’ => $command->queryScalar));

instead

$update=Pembelian::model()->update(array($this->total => $command->queryScalar));

describe the update method for more advices

well, yes there is an update method. this $command->queryScalar supposed to contains the value of


$command = Yii::app()->db->createCommand('SELECT SUM(subtotal) FROM pembelian_detail WHERE pembelian_detail.id_pembelian=:id_pembelian');

        $command->bindValue(':id_pembelian', $this->id_pembelian);



but I think my code is wrong, so that’s why the error appears. and I’m not sure if $update=Pembelian::model()->update(array(‘total’ => $command->queryScalar)); is a right code. but I’ll give it a try. thanks :D

aw, still the same error :o

Did you try to check with echo-trace of $command->queryScalar returned value? Is the correct?

If the update method exist please ensure you pass the correct arguments or post to forum the code of method

$command->queryScalar()

can you tell me how to check it like what you said? I’m really a newbie in Yii :blink:

In simple way (without using Yii way) you can use php function

$test = $command->queryScalar;

var_dump ($test);

die();

OR

$test = $command->queryScalar;

throw new CHttpException(404, ‘my message:’.$test); //Yii tricky way

OR

the right Yii way is with log and trace, see below link

http://www.yiiframework.com/wiki/114/how-to-log-and-debug-variables-using-cweblogroute/

$command->queryScalar() and not $command->queryScalar (it’s a method and not a property)

Yes I have ommited the ()

thanks for the informations ::) I’ve tried this one :


$test = $command->queryScalar;

throw new CHttpException(404, 'my message:'.$test); //Yii tricky way

and this 404 error appears, but without the content of $test in my message. what does it means?

dyooolicious, bennouna is correct, you are missing the parenthesis.

ok, thanks :) I’ve just change my code :


public function afterSave() {


        //save total 

        $command = Yii::app()->db->createCommand('SELECT SUM(subtotal) FROM pembelian_detail WHERE pembelian_detail.id_pembelian=:id_pembelian');

        $command->bindValue(':id_pembelian', $this->id_pembelian);

        Pembelian::model()->updateByPk($this->id_pembelian, array($this->total => $command->queryScalar()));


    }

but, I got this error : No columns are being updated for table “pembelian”. :mellow:

I finally did it ! :lol: thanks for all help.

this is my newest code to update total column from the sum of subtotals:


public function afterSave() {


        //save total 

        $command = Yii::app()->db->createCommand('SELECT SUM(subtotal) FROM pembelian_detail WHERE pembelian_detail.id_pembelian=:id_pembelian');

        $command->bindValue(':id_pembelian', $this->id_pembelian);

        Pembelian::model()->updateByPk($this->id_pembelian, array('total'=>$command->queryScalar()));        


    }

best regards