ActiveRecord access

Hi everybody,

I have used new yii 1.1.1 and I have had problem when I try access models by models like the old version. For example:

I have

           $criteria->join = "INNER JOIN PRODUTO ON produto.IDPRODUTO = t.IDPRODUTO";


            $criteria->condition = "t.IDPEDIDO = $model->idpedido";


            


            $produtopedido = produtopedido::model()->findAll($criteria);

When I try access it in my view $produtopedido->idpedido is returning null. In old versions of yii I could access using this way.

In case a have used foreach for tracking my array, foreach($produtospedido as $n=>$prod) and I use $prod->idpedido, but nothing too.

Could somebody help me?

I assume ‘idpedido’ is a relation in the $model AR class? If it is then how do you initiate $model object?

Hi Vince,

Yes I assumed idpedido like a relation. I initialize my $model in my controller and I pass $model in render’s array and create/update array. The variable is filling because I have try “var_dump($model)”.

Regards,

if the idepedido is a relation in the $model then make sure you initiate the relation when initiating the $model.

So, If you do:




$model = ModelName::model()->with(array('ipdedido'))->findAll();


foreach($model as $data)

{

// now call $data->ipedido

}



Thanks Vince,

But when I want access the related table, for example, $model->pedido->produtopedido->idproduto. I have already define my relations on model pedido with produtopedido.

Does method above work or would work? I have been tried and at now, nothing!

Thanks.

Hi,

I found the solution/problem. I have defined wrong relation(m:n). I have 3 tables: pedido, pedidoproduto and produto. I was defining relation between pedidoproduto and produto like HAS_MANY, where I would define BELONGS_TO. Now it works.

Thanks.