fange 2016-12-28 19:03:01 4424次浏览 1条评论 0 0 0

标题两张表

QQ图片20161228183257.png

QQ图片20161228183339.png

明显我以tbl_order表为主显示表,tbl_order表中的customer_id关联tbl_customer表中的id字段。

配置文件QQ图片20161228183804.png

用gii生成代码就不多说了

我的model层文件

<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "{{%order}}".
 *
 * @property integer $id
 * @property integer $customer_id
 * @property integer $price
 */
class Order2 extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%order}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'customer_id', 'price'], 'required'],
            [['id', 'customer_id', 'price'], 'integer'],
            [['id'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'customer_id' => 'Customer ID',
            'price' => 'Price',
        ];
    }
    /**
     * 关联顾客表Customer
     */
    public function getCustomer2(){
        // hasOne要求返回两个参数 第一个参数是关联表的类名 第二个参数是两张表的关联关系 
        return $this->hasOne(Customer2::className(),['id'=>'customer_id']);exit;
    }
}

<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Order2;

/**
 * Order2Search represents the model behind the search form about `app\models\Order2`.
 */
class Order2Search extends Order2
{
    /**
     * @inheritdoc
     */
    public $name;
    public function rules()
    {
        return [
            [['id', 'customer_id', 'price'], 'integer'],
            ['name','safe'], 
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Order2::find();
        $query->joinWith(['customer2']);
        $query->select("{{%order}}.*,{{%customer}}.name");
        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'customer_id' => $this->customer_id,
            '{{%customer}}.name' => $this->name,
            'price' => $this->price,
        ]);

        return $dataProvider;
    }
}

<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "{{%customer}}".
 *
 * @property integer $id
 * @property string $name
 */
class Customer2 extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%customer}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'name'], 'required'],
            [['id'], 'integer'],
            [['name'], 'string', 'max' => 20],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => 'name',
        ];
    }
}

表的关联,在主表tbl_order表中

QQ图片20161228184820.png

在Order2Search中
QQ图片20161228185047.png

前端显示QQ图片20161228185500.png

添加顾客姓名的搜索框

QQ图片20161228185700.png

执行搜索条件,!

QQ图片20161228185843.png
下面是我的页面效果!

QQ图片20161228190008.png

QQ图片20161228190239.png

您需要登录后才可以评论。登录 | 立即注册