小编sha*_*ril的帖子

如何在yii2中显示相关表格中的数据?

我在使用yii2显示来自相关表的数据时遇到了问题.我使用自己的设计,而不是使用yii2设计.我有两个表的用户状态

TABLE `user`(
`user_id` int(11) NOT NULL auto_increment,
`state_id` int(11) null 

table `state`(
`state_id` int(11) NOT NULL auto_increment,
`state` varchar(225) null
Run Code Online (Sandbox Code Playgroud)

UserModel.php

public function getStates()
{
    return $this->hasOne(State::className(),['state_id' =>'state_id']);
}
Run Code Online (Sandbox Code Playgroud)

UserController.php

 public function actionView($id)
{
    return $this->render('view', [
        'model' => $this->findModel($id),

    ]);
}
Run Code Online (Sandbox Code Playgroud)

State.php

 public function attributeLabels()
{
    return [
        'state_id' => 'State ID',
        'state' => 'State',
     ];
}

public function getState()
{
    return $this->state.'';
}
Run Code Online (Sandbox Code Playgroud)

view.php

<table>..<tr>
    <td>Negeri</td>
    <td><?php echo $model->states; ?></td>
</tr>
Run Code Online (Sandbox Code Playgroud)

当我使用$ model-> states; …

php yii2

4
推荐指数
1
解决办法
4941
查看次数

如何在yii2 restful api中从两个表中将关系数据显示为json格式

我遇到的问题是将两个表中的数据显示为JSON格式并处理yii2 restful api.

这是我的结构数据库:

TABLE `volunteer`(
`volunteer_id` int(11) NOT NULL auto_increment,
`state_id` int(11) null 

TABLE `state`(
`state_id` int(11) NOT NULL auto_increment,
`state` varchar(225) null
Run Code Online (Sandbox Code Playgroud)

volunteerController.php

public $modelClass = 'app\models\Volunteer';
public function behaviors()
{
    return ArrayHelper::merge(parent::behaviors(),[
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'delete' => ['post'],
            ],
        ],
    ]);
}
Run Code Online (Sandbox Code Playgroud)

配置/ web.php

'rules' => [
        ['class' => 'yii\rest\UrlRule', 'controller' => ['volunteer','state','post']],
],
'request' => [
        // !!! insert a secret key in the following (if it is empty) …
Run Code Online (Sandbox Code Playgroud)

php rest json relation yii2

4
推荐指数
1
解决办法
3074
查看次数

标签 统计

php ×2

yii2 ×2

json ×1

relation ×1

rest ×1