Yii Framework Forum: ActiveRecord access - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

ActiveRecord access Access related models from main model Rate Topic: -----

#1 User is offline   Jorge Teles 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 26-May 09
  • Location:Sâo Paulo

Posted 22 April 2010 - 08:58 PM

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?
Jorge Teles
Systems Analist
0

#2 User is offline   Vince. 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 432
  • Joined: 29-December 08
  • Location:Los Angeles,CA

Posted 23 April 2010 - 12:50 AM

I assume 'idpedido' is a relation in the $model AR class? If it is then how do you initiate $model object?
Vince Gabriel, Lead Developer.
Posted ImagePosted ImagePosted Image
0

#3 User is offline   Jorge Teles 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 26-May 09
  • Location:Sâo Paulo

Posted 23 April 2010 - 07:26 AM

View PostVince., on 23 April 2010 - 12:50 AM, said:

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,
Jorge Teles
Systems Analist
0

#4 User is offline   Vince. 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 432
  • Joined: 29-December 08
  • Location:Los Angeles,CA

Posted 23 April 2010 - 07:56 AM

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
}

Vince Gabriel, Lead Developer.
Posted ImagePosted ImagePosted Image
0

#5 User is offline   Jorge Teles 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 26-May 09
  • Location:Sâo Paulo

Posted 23 April 2010 - 09:19 AM

View PostVince., on 23 April 2010 - 07:56 AM, said:

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.
Jorge Teles
Systems Analist
0

#6 User is offline   Jorge Teles 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 26-May 09
  • Location:Sâo Paulo

Posted 23 April 2010 - 09:34 AM

View Postjorteles, on 23 April 2010 - 09:19 AM, said:

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.
Jorge Teles
Systems Analist
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users