如何在yii 2中使用搜索和过滤获取外键值而不是网格视图中的键?

时间:2015-08-22 12:19:38

标签: php gridview yii yii2

我有两个表staff,其中包含idnameattendance列。 staff_id用作attendance表中的外键。

我想在考勤网格视图中显示员工姓名。

出勤型号:

public function getStaff()
{
        return $this->hasOne(Staff::className(), ['id' => 'staff_id']);
}

public function getStaffName() {
          return $this->staff->name;
}

并在index.php中我使用了这段代码

     <?= GridView::widget([
            [
             'attribute'=>'staff_id',
            'value'=>'StaffName',
            ],
]); ?>

获取员工姓名的价值。通过这种方式,我成功获得了员工姓名但问题是,当我在gridview中搜索员工姓名时,它说“staff_id”应该是整数,因为我将其定义为整数,但在这里我想搜索员工的姓名而不是id 。

这怎么可能?提前致谢

3 个答案:

答案 0 :(得分:6)

在搜索模型中添加此内容

$query->joinWith(['staff(relation name)']);

在过滤查询中添加以下代码。

$query->andFilterWhere(['like', 'staff.name', $this->staff_id])

staff.name staff中的var mongoClient = new MongoClient(new Server(mongoHost, mongoPort)); //B mongoClient.open(function(err, mongoClient) { //C if (!mongoClient) { console.error("Error! Exiting... Must start MongoDB first"); process.exit(1); //D } var db = mongoClient.db("trackr"); //E collectionDriver = new CollectionDriver(db); //F }); app.use(express.static(path.join(__dirname, 'public'))); app.get('/:collection', function(req, res) { //A var params = req.params; //B collectionDriver.findAll(req.params.collection, function(error, objs) { //C if (error) { res.send(400, error); } //D else { if (req.accepts('html')) { //E res.render('data',{objects: objs, collection: req.params.collection}); //F } else { res.set('Content-Type','application/json'); //G res.send(200, objs); //H } } }); }); app.get('/:collection/:entity', function(req, res) { //I var params = req.params; var entity = params.entity; var collection = params.collection; if (entity) { collectionDriver.get(collection, entity, function(error, objs) { //J if (error) { res.send(400, error); } else { res.send(200, objs); } //K }); } else { res.send(400, {error: 'bad url', url: req.url}); } }); app.get('/:collection/iata/:value', function(req, res) { //I console.log("entrou"); var params = req.params; var value = params.value; var collection = params.collection; if (value) { collectionDriver.getByIata(collection, value, function(error, objs) { //J if (error) { res.send(400, error); } else { res.send(200, objs); } //K }); } else { res.send(400, {error: 'bad url', url: req.url}); } }); 是表名。

答案 1 :(得分:6)

您可以使用此代码

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        'staff.name',
    ],
]); ?>

或使用此代码

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        [
           'attribute' => 'staff.name',
           'header' => 'Staff title'
           'value'=> 'function ($model, $key, $index, $grid){
               return $model->staff->name;
           }'
        ],
    ],
]); ?>

您的代码中的OR可以使用此

<?= GridView::widget([
    [
      'attribute'=>'staff_id',
      'value'=>'getStaffName()',
    ],
]); ?>

对于搜索,您可以观看此视频Searching Related Table Data

答案 2 :(得分:1)

You can use this code

1. relation in models
 public function getCountry()
    {
        return $this->hasOne(Country::className(), ['id' => 'country_id']);
    }
2. in grid view
<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            'id',
            'state_name',
           // 'country_id',
         [
      'attribute'=>'country_id',
      'value'=>'country.country_name',
    ],
            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>
3. Search Models
   change country_id from integer to safe than change on search function

 public function search($params)
    {
        $query = State::find();
        $query->joinWith(['country']);
       $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
        $this->load($params);
        if (!$this->validate()) {
            return $dataProvider;
        }
        $query->andFilterWhere([
            'id' => $this->id,
        ]);
        $query->andFilterWhere(['like', 'state_name', $this->state_name])
         ->andFilterWhere(['like', 'country.country_name', $this->country_id]);
return $dataProvider;
    }