Hi Patrick,
I did everything you say and still have problem with relation.
Here is my new code:
Model:
class Usersnew extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'usersnew';
}
public static function primaryKey()
{
return ['Id'];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['FirstName', 'LastName', 'NickName', 'IdCity'], 'required'],
[['IdCity'], 'integer'],
[['FirstName', 'LastName', 'NickName'], 'string', 'max' => 100],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'Id' => 'ID',
'FirstName' => 'First Name',
'LastName' => 'Last Name',
'NickName' => 'Nick Name',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getIdCity()
{
return $this->hasOne(City::className(), ['Id' => 'IdCity']);
}
}
class UsersnewSearch extends Usersnew
{
/**
* @inheritdoc
*/
public function rules()
{
return [
[['Id', 'IdCity'], 'integer'],
[['FirstName', 'LastName', 'NickName'], 'safe'],
];
}
public function attributes()
{
// add related fields to searchable attributes
return array_merge(parent::attributes(), ['city.Naziv']);
}
/**
* @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 = Usersnew::find()->addSelect('usersnew.Id, usersnew.FirstName,usersnew.LastName,usersnew.NickName,usersnew.IdCity,city.Naziv');
$query = Usersnew::find();
//$query->innerJoin('city', 'usersnew.IdCity=city.Id');
$query->joinWith(['city' => function($query) { $query->from(['city' => 'usersnew']); }]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 5,
],
]);
//print_r($dataProvider);
$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,
'IdCity' => $this->IdCity,
]);
$query->andFilterWhere(['like', 'FirstName', $this->FirstName])
->andFilterWhere(['like', 'LastName', $this->LastName])
->andFilterWhere(['like', 'NickName', $this->NickName]);
return $dataProvider;
}
}
Controller:
class UsersnewController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all Usersnew models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new UsersnewSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Usersnew model.
* @param string $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Usersnew model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Usersnew();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->Id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing Usersnew model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param string $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->Id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing Usersnew model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param string $id
* @return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the Usersnew model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param string $id
* @return Usersnew the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Usersnew::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
Index view with GridView:
<?php
use yii\helpers\Html;
use yii\grid\GridView;
use app\models\City;
/* @var $this yii\web\View */
/* @var $searchModel app\models\UsersnewSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Usersnews';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="usersnew-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a('Create Usersnew', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>
And after all that it shows error:
[code]
Invalid Parameter – yii\base\InvalidParamException
app\models\Usersnew has no relation named "city".
↵
Caused by: Unknown Method – yii\base\UnknownMethodException
Calling unknown method: app\models\Usersnew::getcity()
in C:\wamp\www\ctb\vendor\yiisoft\yii2\base\Component.php at line 285
I have 2 tables: city and usersnew. They are related by fields usersnew.IdCity=city.Id in PhPMyAdmin designer. They have PK and FK. I have checked.
I don’t know what is wrong I did everything that is in docs.
Please help 
thanx in advance