Relacion tres tablas

Buenos días,

tengo una duda que no sé como solucionar. Básicamente tengo tres tablas que podrían ser

ACTIVIDADES

id

Nombre

Horas

CiudadId

CIUDAD

id

Nombre

PaisId

PAIS

id

Nombre

Si desde el model de Actividades quiero mostrar los datos de una actividad, con los datos de la tabla ACTIVIDADES, Nombre Ciudad y Nombre País. Que relación tengo que hacer para poder mostrar el nombre del País? Para mostrar el nombre de la Ciudad ya entiendo que tendré la siguiente relación.


 public function relations()

	{

		return array('relCiudad' => array(self::BELONGS_TO, 'CIUDAD', 'CiudadId'),

        }



Muchas gracias por la ayuda.

Guillem

Creo que buscas esto, un poco tarde…

http://www.yiiframew…ive-record.html

Declaring Relations

To work with relational data using Active Record, you first need to declare relations in Active Record classes. The task is as simple as declaring arelation method for every interested relation, like the following,





class Customer extends ActiveRecord

{

    public function getOrders()

    {

        return $this->hasMany(Order::className(), ['customer_id' => 'id']);

    }

}


class Order extends ActiveRecord

{

    public function getCustomer()

    {

        return $this->hasOne(Customer::className(), ['id' => 'customer_id']);

    }

}



Relations via a Junction Table

In database modelling, when the multiplicity between two related tables is many-to-many, a junction table is usually introduced. For example, theorder table and the item table may be related via a junction table named order_item. One order will then correspond to multiple order items, while one product item will also correspond to multiple order items.

When declaring such relations, you would call either via() or viaTable() to specify the junction table. The difference between via() and viaTable()is that the former specifies the junction table in terms of an existing relation name while the latter directly the junction table. For example,





class Order extends ActiveRecord

{

    public function getItems()

    {

        return $this->hasMany(Item::className(), ['id' => 'item_id'])

            ->viaTable('order_item', ['order_id' => 'id']);

    }

}



or alternatively,





class Order extends ActiveRecord

{

    public function getOrderItems()

    {

        return $this->hasMany(OrderItem::className(), ['order_id' => 'id']);

    }


    public function getItems()

    {

        return $this->hasMany(Item::className(), ['id' => 'item_id'])

            ->via('orderItems');

    }

}



The usage of relations declared with a junction table is the same as that of normal relations. For example,





// SELECT * FROM `order` WHERE `id` = 100

$order = Order::findOne(100);


// SELECT * FROM `order_item` WHERE `order_id` = 100

// SELECT * FROM `item` WHERE `item_id` IN (...)

// returns an array of Item objects

$items = $order->items;



Hola CSnet,

no estoy usando Yii 2.0, esto me va a funcionar?

Gracias!

Guillem

Buenas.

En tu modelo de actividades tienes esto:




 public function relations()

        {

                return array('relCiudad' => array(self::BELONGS_TO, 'CIUDAD', 'CiudadId'),

        }



En tu modelo de ciudades tendrás algo como esto:




 public function relations()

        {

                return array('relPais' => array(self::BELONGS_TO, 'PAIS', 'PaisId'),

        }



Pues ya está, ya lo tienes todo:




$model = $this->loadModel($id);// Esto lo cmabia por lo que quieras, aqu cargarías el modelo.


$model->relCiudad->Nombre; // Nombre de la ciudad.

$model->relCiudad->relPais->Nombre; // Nombre del país.



Así de sencillo.

Un saludo.