Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Model 可以在Yii2中定义通过另一个表关联的两个表之间的关系吗?_Model_Yii2_Relationship - Fatal编程技术网

Model 可以在Yii2中定义通过另一个表关联的两个表之间的关系吗?

Model 可以在Yii2中定义通过另一个表关联的两个表之间的关系吗?,model,yii2,relationship,Model,Yii2,Relationship,我有三张相互关联的桌子。这些表是预订、车辆时间表和车辆 表预订(预订id、日程id、其他字段) 表计划(计划id、车辆id、其他字段) 表车辆(车辆id、注册号、型号等) 现在,有了这三个表,我想显示保留表GridView中车辆表的值,即能够对车辆进行排序,搜索等。显示表的值不是问题,但现在尝试操作保留搜索模型,以便在搜索和排序期间能够获得该值,这证明是一个问题,因为表保留与表车辆没有直接关系,而仅通过车辆时间表表格 有人知道怎么做吗 Reservations view.php <?php

我有三张相互关联的桌子。这些表是
预订
车辆时间表
车辆

预订(预订id、日程id、其他字段)
计划(计划id、车辆id、其他字段)
车辆(车辆id、注册号、型号等)

现在,有了这三个表,我想显示
保留
GridView
车辆
表的值,即能够对
车辆进行
排序
搜索
等。显示表的值不是问题,但现在尝试操作
保留搜索
模型,以便在搜索和排序期间能够获得该值,这证明是一个问题,因为表
保留
与表
车辆
没有直接关系,而仅通过
车辆时间表
表格

有人知道怎么做吗

Reservations view.php

<?php Pjax::begin(); ?>    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            'reservation_id:integer:Ticket #',
            'schedule.vehicle.reg_number',
            [
            'attribute' => 'Customer Name',
            'value' => 'customer.name'
            ],
            [
            'attribute' => 'Officer Name',
            'value' => 'officer.first_name'
            ],
            'schedule.route.displayName:text:Route',
            'amount_paid',
            // 'datetime',

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>

预订模式

<?php

namespace app\models;

use Yii;
use app\models\CDbExpression;

/**
 * This is the model class for table "reservations".
 *
 * @property integer $reservation_id
 * @property integer $schedule_id
 * @property integer $customer_id
 * @property integer $officer_id
 * @property integer $seat_number
 * @property string $amount_paid
 * @property string $datetime
 *
 * @property Customers $customer
 * @property User $officer
 * @property VehicleSchedules $schedule
 */
class Reservations extends \yii\db\ActiveRecord
{
    public $mobile_number;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'reservations';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['schedule_id', 'customer_id', 'officer_id', 'seat_number'], 'required'],
            [['schedule_id', 'customer_id', 'officer_id', 'seat_number'], 'integer'],
            [['amount_paid'], 'number'],
            [['datetime', 'mobile_number'], 'safe'],
            [['customer_id'], 'exist', 'skipOnError' => true, 'targetClass' => Customers::className(), 'targetAttribute' => ['customer_id' => 'customer_id']],
            [['officer_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['officer_id' => 'id']],
            [['schedule_id'], 'exist', 'skipOnError' => true, 'targetClass' => VehicleSchedules::className(), 'targetAttribute' => ['schedule_id' => 'schedule_id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'reservation_id' => 'Ticket Number',
            'schedule_id' => 'Schedule ID',
            'customer_id' => 'Customer Name',
            'officer_id' => 'Officer',
            'seat_number' => 'Seat Number',
            'amount_paid' => 'Amount Paid',
            'datetime' => 'Date & Time',
            'mobile_number' => 'Mobile Number',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCustomer()
    {
        return $this->hasOne(Customers::className(), ['customer_id' => 'customer_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getOfficer()
    {
        return $this->hasOne(Staff::className(), ['id' => 'officer_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getSchedule()
    {
        return $this->hasOne(VehicleSchedules::className(), ['schedule_id' => 'schedule_id']);
    }

可能对您有所帮助。看看这里:嗨@vijay,我认为一个适用于两个直接相关的表。在我的例子中,这些表通过另一个表进行关联。事实证明有些棘手,但我知道有一种方法。请阅读-总结是,这不是一个理想的方式来解决志愿者,可能会适得其反获得答案。请不要将此添加到您的问题中。比兹利也提到了这一点。您正在搜索“连接表”的用法,这将对您有所帮助
    <?php

namespace app\models;

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

/**
 * ReservationsSearch represents the model behind the search form about `app\models\Reservations`.
 */
class ReservationsSearch extends Reservations
{
    public $customer;
    public $vehicle;
    public $officer;
    public $route;
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['reservation_id', 'schedule_id', 'customer_id', 'officer_id', 'seat_number'], 'integer'],
            [['amount_paid'], 'number'],
            [['datetime', 'customer', 'vehicle', 'officer', 'route'], '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 = Reservations::find();

        // add conditions that should always apply here

        // $query->joinWith(['customers', 'vehicles', 'staff', 'stations']);

        $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([
            'reservation_id' => $this->reservation_id,
            'schedule_id' => $this->schedule_id,
            'customer_id' => $this->customer_id,
            'officer_id' => $this->officer_id,
            'seat_number' => $this->seat_number,
            'amount_paid' => $this->amount_paid,
            'datetime' => $this->datetime,
        ]);

        return $dataProvider;
    }
}