Answer the question
In order to leave comments, you need to log in
How to correctly transfer data from two tables (yii, active record) to the view?
Interested in the correct approach to solving the following problem. Let's say I have two tables. In one list of customers, in the other list of customer orders. One-to-many relationship (one customer - many orders).
Let's say there is a view (view, view) in which you need to display data about the client and a list of his orders.
I usually do it like this:
$client = Client::model()->findByPk($id);
$orders = Order::model()->findAll('client_id = :client_id', array(':client_id' => $id);
$data['client'] = $client;
$data['orders'] = $orders;
$this->render('view_name', $data);
Answer the question
In order to leave comments, you need to log in
In the customer model
public function relations()
{
'orders'=>array(self::BELONGS_TO,'Order','client_id'),
}
$client = Client::model()->findByPk($id);
$this->render('view_name', array('client'=>$client, 'orders'=>$client->orders));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question