Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The Yii way would be to define a Relation between the two model classes (and enforce it with a Foreign Key). Then Yii would know how the two classes are related and you would have an easier time loading the related data.</p> <p>In the <code>TaxiOrders</code> model class, a relation would look like this:</p> <pre><code>/** * @property TaxiDrivers $Driver */ class TaxiOrders extends CActiveRecord { // ... public function relations() { return array( 'Driver' =&gt; array(self::BELONGS_TO, 'TaxiDrivers', 'driver_id'), ); } // ... } </code></pre> <p>In the controller, when you load the order data, you can prefetch the associated driver data like this:</p> <pre><code>public function actionOrderInfo($orderID) { $order = TaxiOrders::model()-&gt;with('Driver')-&gt;findByPk($orderID); // render it } </code></pre> <p><code>with('Driver')</code> will make sure that each returned order has its driver info already loaded, no matter if you need to find one record or a lot of records. It is a lot more efficient than trying to load that related data yourself.</p> <p>In the view, you can output the driver info like this:</p> <pre><code>echo CHtml::encode($order-&gt;Driver-&gt;Name); </code></pre> <p>Unless you have a foreign key to ensure data integrity, it is possible that the Driver has been deleted without clearing his existing orders... In that case <code>$order-&gt;Driver</code> will be <code>NULL</code> and the line above will cause an error. Figuring out how to avoid it should be obvious.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload