Yii Framework Forum: Relations Problem - Yii Framework Forum

Jump to content

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

Relations Problem can't get related info Rate Topic: -----

#1 User is offline   Led 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 200
  • Joined: 16-January 12

Posted 08 December 2012 - 10:32 AM

dont know whats happen , but i have 2 models:

Reserva

ReservaState

in Reserva i have
'states' => array(self::HAS_ONE, 'ReservaState', 'id'),

in ReservaState
'reservas' => array(self::HAS_MANY,'Reserva','reserva_state')


$model = Reserva::model()->with('states', 'cliente','precos')->findByPk($id);

But always get null. on $model->states

Reserva have one reservaState

need help
0

#2 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 966
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 08 December 2012 - 11:37 AM

I see you have to have fk in relationship

'states' => array(self::HAS_ONE, 'ReservaState', 'id'), // use your fk 

'reservas' => array(self::HAS_MANY,'Reserva','reserva_state') // same here use your fk 


if you can upload your database schema then i might be able to tell you what exactly to change
0

#3 User is offline   Led 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 200
  • Joined: 16-January 12

Posted 08 December 2012 - 12:16 PM

View Postalirz23, on 08 December 2012 - 11:37 AM, said:

I see you have to have fk in relationship

'states' => array(self::HAS_ONE, 'ReservaState', 'id'), // use your fk 

'reservas' => array(self::HAS_MANY,'Reserva','reserva_state') // same here use your fk 


if you can upload your database schema then i might be able to tell you what exactly to change


thanks , here schema of tables

CREATE TABLE reserva_state (
id int(11) NOT NULL AUTO_INCREMENT,
state varchar(25) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE reserva (
id int(11) NOT NULL AUTO_INCREMENT,
n_prereserva int(11) DEFAULT NULL,
reservado bit(1) DEFAULT NULL,
valor varchar(45) DEFAULT NULL,
n_cheque varchar(45) DEFAULT NULL,
valorSinal varchar(45) DEFAULT NULL,
idcliente int(11) NOT NULL,
idpreco int(11) NOT NULL,
tipopagamento int(11) DEFAULT NULL,
n_pagamento int(11) DEFAULT NULL,
reserva_state int(11) NOT NULL,
`data` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`user` varchar(50) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

just want to show the state description and not the state id
0

#4 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 966
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 08 December 2012 - 03:51 PM

change this
'states' => array(self::HAS_ONE, 'ReservaState', 'id')

to
'states' => array(self::HAS_ONE, 'ReservaState', 'reserva_state')
0

#5 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,082
  • Joined: 16-February 11
  • Location:Japan

Posted 08 December 2012 - 08:18 PM

Umm, it should be a simple 1:N relation ... 1 for ReservaState and N for Reserva ...
"A ReservaState has many Reservas" and "A Reserva belongs to ReservaState".
It is a pair of "HAS_MANY" and "BELONGS_TO" sharing the same FK.

in Reserva
'states' => array(self::BELONGS_TO, 'ReservaState', 'reserva_state'),


in ReservaState
'reservas' => array(self::HAS_MANY, 'Reserva','reserva_state')


Don't be confused by the name of "HAS_ONE".
http://www.yiiframew...-versus-has_one
0

#6 User is offline   Led 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 200
  • Joined: 16-January 12

Posted 09 December 2012 - 04:30 PM

View Postsoftark, on 08 December 2012 - 08:18 PM, said:

Umm, it should be a simple 1:N relation ... 1 for ReservaState and N for Reserva ...
"A ReservaState has many Reservas" and "A Reserva belongs to ReservaState".
It is a pair of "HAS_MANY" and "BELONGS_TO" sharing the same FK.

in Reserva
'states' => array(self::BELONGS_TO, 'ReservaState', 'reserva_state'),


in ReservaState
'reservas' => array(self::HAS_MANY, 'Reserva','reserva_state')


Don't be confused by the name of "HAS_ONE".
http://www.yiiframew...-versus-has_one


Samdark. 1k thanks.
By the way, lets say if i want to relate to another model/table that is not directly related. ex: relate to owner that have a relation with house that have a relation to reserva.

Can i relate directly to owner in reserva.
Thanks again and hope i explain clearly.
0

#7 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,082
  • Joined: 16-February 11
  • Location:Japan

Posted 09 December 2012 - 07:48 PM

I'm not samdark, but yes, you can use a relation's relation.
Let's say, if a->b and b->c, then we can use a->b->c.
0

#8 User is offline   Led 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 200
  • Joined: 16-January 12

Posted 10 December 2012 - 07:52 AM

View Postsoftark, on 09 December 2012 - 07:48 PM, said:

I'm not samdark, but yes, you can use a relation's relation.
Let's say, if a->b and b->c, then we can use a->b->c.


ok softark. thanks again
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