简体   繁体   中英

Yii join two models

I've got two mysql tables: 1. prices 2. details

Table Prices

ID  |  DATE     | CODE |  PRICE
1   |2014-01-01 | AAA1 | 90.123
2   |2014-01-01 | AAB1 | 50.113
3   |2014-01-01 | AAC1 | 48.621
4   |2014-01-02 | AAA1 | 91.123
5   |2014-01-02 | AAB1 | 51.113
6   |2014-01-02 | AAC1 | 41.621

Table Details

CODE | NAME   | DESCRIPTION
AAA1 | andria | A very good...
AAB1 | anasta | A very good...
AAC1 | simple | A very good...

Models:

Prices

public function relations(){
        return array(
            'code' => array(self::BELONGS_TO, 'details', 'code'),
        );
    }

Details

public function relations(){
        return array(
            'code' => array(self::HAS_MANY, 'prices', 'code'),
        );
    }

Here's the SQL code I want to execute:

SELECT * FROM prices a 
JOIN (SELECT * FROM details) b
WHERE a.DATE=(SELECT MAX(DATE) FROM prices) AND a.code = b.code

My controllers:

$prices=new CActiveDataProvider('prices', array(
                'criteria'=>new CDbCriteria (array(
                    'select'=>'code,date,close',
                    'condition'=>'date=(SELECT MAX(date) FROM prices)'
                )),
            ));

$this->render('index',array(
                'prices'=>$prices
            ));

index.php:

<?php 
    $this->widget('bootstrap.widgets.TbGridView', array(
    'dataProvider'=>$prices,
    'template'=>"{items}",
    'enablePagination' => false,
    'columns'=>array(
        array('name'=>'code', 'header'=>'Code'),
        array('name'=>'name', 'header'=>'Name'),
        array('name'=>'close', 'header'=>'Close'),
    ),
)); ?>

With this controller commands, I can get the data from table prices, but I can't get the data from table details.

Take a look at the with part of the criteria: http://www.yiiframework.com/doc/api/1.1/CDbCriteria#with-detail

I think your code should look something like this (untested):

$prices=new CActiveDataProvider('prices', array(
            'criteria'=>new CDbCriteria (array(
                'select'=>'code,date,close',
                'with' => 'details',
                'condition'=>'date=(SELECT MAX(date) FROM prices)'
            )),
        ));

you already have reliation in Model. So do query like this in your controller

$prices=new CActiveDataProvider('prices', array(
        'criteria'=>new CDbCriteria (array(
            'condition'=>'date=(SELECT MAX(date) FROM prices)'
        )),
    ));

In View just use

$data->code->name
$data->code->description

Here's what I change:

index.php

<?php 
    $this->widget('bootstrap.widgets.TbGridView', array(
    'dataProvider'=>$prices,
    'template'=>"{items}",
    'enablePagination' => false,
    'columns'=>array(
        array('name'=>'code', 'header'=>'Code'),
        array('name'=>'name', 'header'=>'Name', 'value'=>'$data->details->name'),
        array('name'=>'close', 'header'=>'Close'),
    ),
)); ?>

Models Prices

public function relations(){
        return array(
            'details' => array(self::BELONGS_TO, 'Stocks_details', 'code'),
        );
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM