Why There Is No Value?

first, I’m sorry for the strange topic title because of Idk what is the good title for this kind of question.

I have this situation, I need to check if the customer’s level is distributor then they’ll be given the price of distributor for items that they bought. and the same rule for retail level. I have this code :


$customer=Customer::model()->findByPk($model->id_customer);

            if($customer->level_harga==='distributor'){

                $harganya = Harga::model()->findAllByAttributes(array('id_item'=>$_POST['Penjualan']['id_item'], 'level_harga'=>'distributor'));

                        $pendet->harga = $harganya->harga;

            }else if($customer->level_harga==='retail'){

                $harganya = Harga::model()->findAllByAttributes(array('id_item'=>$_POST['Penjualan']['id_item'], 'level_harga'=>'retail'));

                        $pendet->harga = $harganya->harga;

            }

but, that codes give me no value for $pendet->harga :( any idea?

Hi dyooolicious

Did you check if the $_POST[‘Penjualan’][‘id_item’] has the apropriate value?

also check if the ‘level_harga’ field has ‘distributor’ value and same value on id_item field with above POST value in the same record of your database

Final check if your conditions (if… else…) is ok

findAllByAttributes returns an array of models. If you are searching for a single model, use findByAttributes instead.

Edit: you can also var_dump any variable you want in your controller code to help debug it.

I agree with bennouna.

If you want to check all records you could use

foreach ($harganya as $item) { your code …$item->harga… }

thanks all :)

I’ll try soon and reply the result

wow, it’s work now. I change my code into like this :


$customer = Customer::model()->findByPk($model->id_customer);

            if ($customer->level_harga === 'distributor') {

                $harganya = Harga::model()->findByAttributes(array('id_item' => $pendet->id_item, 'level_harga' => 'distributor'));

                $pendet->harga = $harganya->harga;

            } else if ($customer->level_harga === 'retail') {

                $harganya = Harga::model()->findByAttributes(array('id_item' => $pendet->id_item, 'level_harga' => 'retail'));

                $pendet->harga = $harganya->harga;

            }

            

            foreach ($harganya as $item){

                echo $item->harga;

            }



and somehow it works ! I’m not really sure what’s make it work anyway ::) if you masters knows, please tell me what makes this code working. thanks a lot !

Something doesn’t seem right there. findByAttributes() should only be returning a single instance of your Harga model, so you shouldn’t be able to successfully iterate over it using foreach. Maybe I’m missing something obvious.

yeah, I think so. That’s why I’m so curious about that :unsure:

I would have thought that it will go through the loop only once if there is only a single instance. Is it not so?

Because findByAttributes() returns only one record the

foreach ($harganya as $item){

            echo $item->harga;

}

runs only one time

so it has the same results with $harganya->harga;

hhmm… I see. So can I structurize my codes? if I can, then how? or maybe it’s just fine to let it be like that? thanks in advance.

I’ve just tested this with the following action:




	public function actionTest()

	{

		$users = User::model()->findByAttributes(array('id'=>1));

		foreach ($users as $user)

			echo $user->id;

	}



Running this produces the PHP notice "Trying to get property of non-object" on the echo line.

If I disable error reporting, nothing is output.

Clearly iterating over a model instance is not supported in my version of PHP. I suspect that either the code that’s been posted above is not the same as the code that is actually running, or something else is happening which is masking this issue.

findByAttributes return one object instance. Looping through it gives the object properties / values ie the model’s attributes.

Okay, that makes sense. Is that what dyooolicious is intending?

In that case, no need to loop… or am I wrong?

@Keith

That’s a mystery to me.

and @Jimlam

You’re right. Who said a loop is needed? :)

Exactly. If you are not sure when the code is running correctly you could use foreach when use findAllByAttributes. In other case use directly the $harganya->harga;

As I just have posted it, you right :)

Bennouna,

as I mentioned the looping is nessesary in case there are more of one records (using findAllByAttributes) :)

But not necessary in the current scenario, as findByAttributes() is being used, so either a single record or null will be returned.