yii2里的hasOne怎么搞?

2022-03-25 20:34:07 浏览数 (1)

hasOne一对多关系,比如是吧。一个人有很多文章.

第二步:user:gii自动生成 models:

代码语言:javascript复制
<?php
namespace frontendmodels;
use yiidbActiveRecord;
use frontendmodelsUser;
class Article extends ActiveRecord
{
	public function getUserinfo()
	{
		return $this->hasOne(User::className(),['aid'=>'id']);
	}
}

控制器:

代码语言:javascript复制
<?php
namespace frontendcontrollers;
use frontendmodelsArticle;
use frontendmodelsUser;
class IndexController extends yiiwebController
{
	
	public function actionIndex()
	{
		$article=Article::findOne(['id'=>2]);
		var_dump($article->userinfo);
}}

0 人点赞