Unknown Property – yii\base\UnknownPropertyException - php

I got the error in the title when trying to display a relational field in the gridview.
I have the problem with statuscode.statusname.
What do I miss?
Thanks in advance!
My Projectsearch model:
<?php namespace frontend\models;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use frontend\models\Project;
class ProjectSearch extends Project
{
/**
* {#inheritdoc}
*/
public function rules()
{
return [
[['id', 'client_id', 'status_id'], 'integer'],
[['number', 'name'], 'safe'],
[['client.name'], 'safe'],
[['statuscode.statusname'], 'safe'],
];
}
/**
* {#inheritdoc}
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
public function attributes()
{
// add related fields to searchable attributes
return array_merge(parent::attributes(), ['client.name']);
return array_merge(parent::attributes(), ['statuscode.statusname']);
}
/**
* Creates data provider instance with search query applied
*
* #param array $params
*
* #return ActiveDataProvider
*/
public function search($params)
{
$query = Project::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->sort->attributes['client.name'] = [
'asc' => ['client.name' => SORT_ASC],
'desc' => ['client.name' => SORT_DESC],
];
$dataProvider->sort->attributes['statuscode.statusname'] = [
'asc' => ['statuscode.statusname' => SORT_ASC],
'desc' => ['statuscode.statusname' => SORT_DESC],
];
$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;
}
$query->joinWith(['client' => function($query) {
$query->from(['client' => 'clients']); }]);
$query->joinWith(['statuscode' => function($query) { $query->from(['statuscode' => 'statuses']); }]);
// grid filtering conditions
$query->andFilterWhere(['like', 'project.number', $this->number])
->andFilterWhere(['like', 'project.name', $this->name])
->andFilterWhere(['LIKE', 'client.name', $this->getAttribute('client.name')])
->andFilterWhere(['LIKE', 'statuscode.statusname', $this->getAttribute('statuscode.statusname')]);
return $dataProvider;
}
}
My view:
<?php
use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
$this->title = 'Projects';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="project-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php Pjax::begin(); ?>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p><?= Html::a('Create Project', ['create'], ['class' => 'btn btn-success']) ?></p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'number',
'client.name',
'name',
[
'attribute' => 'statuscode.statusname',
'label' => 'Projectstatus',
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end(); ?>
</div>

Nevermind, i got it! :)
return array_merge(parent::attributes(), ['client.name', 'statuscode.statusname']);
instead of:
return array_merge(parent::attributes(), ['client.name']);
return array_merge(parent::attributes(), ['statuscode.statusname']);
LOL

Related

how to join table in grid view in yii2

i want to join my company table name to employee id. here my code in
employee model
public function attributeLabels()
{
return [
'id' => 'ID',
//'company_id' => 'Company ID',
'emp_name' => 'Emp Name',
'emp_email' => 'Emp Email',
'emp_salery' => 'Emp Salery',
];
}
public function getCompany()
{
return $this->hasOne(Company::className(),['id' => 'company_id']);
}
in index file i use this code
<?php PJax::begin();?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
//['class' => 'yii\grid\SerialColumn'],
//'id',
[
'attribute' => 'company_id',
'value' => 'company.name',
],
'emp_name',
'emp_email:email',
'emp_salery',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php PJax::end();?></div>
in employee search table i use this code
public function search($params)
{
$query = Employee::find();
$query->joinWith(['company']);
// add conditions that should always apply here
$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([
'id' => $this->id,
]);
$query->andFilterWhere(['like', 'company_id', $this->company_id])
->andFilterWhere(['like', 'emp_name', $this->emp_name])
->andFilterWhere(['like', 'emp_email', $this->emp_email])
->andFilterWhere(['like', 'emp_salery', $this->emp_salery])
->andFilterWhere(['like', 'Company.name', $this->company_id])
->andFilterWhere(['like', 'company.name', $this->company])
;
return $dataProvider;
}
http://localhost/test/web/employee/
is shows in company field (not set)
pls help me
you can do this in two difference way : 1 . If you want use active record you must set property in first model you want to select it and join table with that like:
public $pin;
then when you get data you can access to pin in second table
the second way is you use asArray() that return all off data in both table
$model = News::find()->leftJoin('comment', 'news.id = comment.news_id')->asArray()->all();
then you can use it in your gridview
You should add a getter eg:getCompanyname()n your base model
......
public function attributeLabels()
{
return [
'id' => 'ID',
//'company_id' => 'Company ID',
'emp_name' => 'Emp Name',
'emp_email' => 'Emp Email',
'emp_salery' => 'Emp Salery',
'companyName' => 'Company Name')
];
}
public function getCompany()
{
return $this->hasOne(Company::className(),['id' => 'company_id']);
}
You should add a getter for company name
/*
Getter for company name
*/
public function getCommpanyName() {
return $this->company->name;
}
and in your gridview you should use the new get name (companyName)
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
'companyName',
'emp_name',
'emp_email:email',
'emp_salery',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
see this for more complete samples
http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/

yii2 grid filtering is not working

my grid view filter is not filtering categories, I have given category_id to nesserray places but yet it is not filtering my posts according to their category_id, please help me.
here are my source codes:
post-->index.php
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'title',
[
'attribute'=>'category_id',
'value'=>function($model) {
return $model->category->name;
},
'filter'=>$category
],
[
'attribute'=>'user_id',
'value'=>function($model) {
return $model->user->fullname;
},
'filter'=>$user,
'label'=>'user'
],
'category.name',
'user.fullname',
// 'user_id',
// 'description',
// 'content:html',
'count_view',
'status',
'created_at',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
and here is model/postSearch.php
<?php
namespace app\models\search;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\post;
/**
* PostSearch represents the model behind the search form about `app\models\post`.
*/
class PostSearch extends post
{
/**
* #inheritdoc
*/
public function rules()
{
return [
[['id', 'user_id','category_id', 'count_view'], 'integer'],
[['title', 'description', 'content', 'status', 'created_at'], '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 = post::find();
// add conditions that should always apply here
$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([
'id' => $this->id,
'category_id' => $this->category_id,
'user_id' => $this->user_id,
'count_view' => $this->count_view,
'created_at' => $this->created_at,
]);
$query->andFilterWhere(['like', 'title', $this->title])
->andFilterWhere(['like', 'description', $this->description])
->andFilterWhere(['like', 'content', $this->content])
->andFilterWhere(['like', 'status', $this->status]);
return $dataProvider;
}
}
it worked, I had to removle jQuery files

modelsearch not working in yii2

I have some problems in making modelsearch in yii2.
Here's is my table's relation
I want to display Jurusan from table aitambah to ais3 and I want to display NamaMahasiswa from table ai to ais3. I made table s3penghubung so that table aitambah can be related to ais3. I was able to display them. But, when I try to type "71501" in "NRP" field, the searching not working properly. it is refreshing the page. there is no any change to the rows displayed. so, what should I do to fix that?
here is my modelsearch codes:
<?php
namespace app\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Ais3;
/**
* Ais3Search represents the model behind the search form about `app\models\Ais3`.
*/
class Ais3Search extends Ais3
{
/**
* #inheritdoc
*/
public function rules()
{
return [
[['id', 'kode'], 'integer'],
[['NRP', 'namaMahasiswaText', 'ProgramStudi', 'TanggalMasuk', 'TanggalKeluar'], 'safe'],
];
}
public $NamaMahasiswa;
public $namaMahasiswaText;
/**
* #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 = Ais3::find();
$query->joinWith('ai');
// add conditions that should always apply here
$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([
'id' => $this->id,
'kode' => $this->kode,
]);
$query->andFilterWhere(['like', 'ais3.NRP', $this->NRP])
->andFilterWhere(['like', 'ai.NamaMahasiswa', $this->namaMahasiswaText])
->andFilterWhere(['like', 'ais3.ProgramStudi', $this->ProgramStudi])
->andFilterWhere(['like', 'ai.TanggalMasuk', $this->TanggalMasuk])
->andFilterWhere(['like', 'ais3.TanggalKeluar', $this->TanggalKeluar]);
return $dataProvider;
}
}
here is mygridview codes
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
//'id',
'NRP',
'namaMahasiswaText',
'ProgramStudi',
//'TanggalMasuk',
[
'attribute' => 'TanggalMasuk',
'value' => function($data) {
return $data->ai->TanggalMasuk;
},],
'TanggalKeluar',
//YANG DITAMBAH
[
'attribute'=>'NRP',
'value'=>'s3penghubung.aitambah.JURUSAN',
'label' => 'Jurusan',
],
[
'attribute'=>'NRP',
'value'=>'s3penghubung.aitambah.NAMKANTOR',
'label' => 'Nama Kantor',
],
[
'attribute'=>'NRP',
'value'=>'s3penghubung.aitambah.ANGKATAN',
'label' => 'Angkatan',
],
[
'attribute'=>'NRP',
'value'=>'s3penghubung.aitambah.TELP',
'label' => 'Nomor HP/Telp',
],
[
'attribute'=>'NRP',
'value'=>'s3penghubung.aitambah.PEKERJAAN',
'label' => 'Pekejaan',
],
[
'attribute'=>'NRP',
'value'=>'s3penghubung.aitambah.EMAIL',
'label' => 'Email',
],
[
'attribute'=>'NRP',
'value'=>'s3penghubung.aitambah.KODEPROP',
'label' => 'KodeProp',
],
// 'kode',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
I thought there must be something wrong in my query filter
$query->andFilterWhere(['like', 'ais3.NRP', $this->NRP])
->andFilterWhere(['like', 'ai.NamaMahasiswa', $this->namaMahasiswaText])
->andFilterWhere(['like', 'ais3.ProgramStudi', $this->ProgramStudi])
->andFilterWhere(['like', 'ai.TanggalMasuk', $this->TanggalMasuk])
->andFilterWhere(['like', 'ais3.TanggalKeluar', $this->TanggalKeluar]);
but I dont know how to fix those codes. Could you please help me to solve this? Thankyou
The right answer is here:
here's gridview in index
and here's in modelsearch
thankyou ck_anjuk for solving my problem.

Getting unknown property: app\models\AlumnyS2::alumniintegrasi

I want to display data relation by making a relation between table alumniintegrasi with alumnys2, but when I made it with gridview, there was an error.
it said '
getting unknown property: app\models\AlumnyS2::alumniintegrasi.
here is the code
<?php
use yii\helpers\Html;
use yii\grid\GridView;
use app\models\AlumniIntegrasi;
/* #var $this yii\web\View */
/* #var $searchModel app\models\AlumnyS2Search */
/* #var $dataProvider yii\data\ActiveDataProvider */
$this->title = Yii::t('app', 'Alumny S2s');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="alumny-s2-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a(Yii::t('app', 'Create Alumny S2'), ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'NRP',
//'NamaMahasiswa',
[
'attribute' => 'NamaMahasiswa',
'value' => function($data) {
return $data->alumniintegrasi->NamaMahasiswa;
},
/*'filter' => Html::activeDropDownList($searchModel, 'orang_ID', ArrayHelper::map(Orang::find()->asArray()->all(), 'ID', 'Nama'),['class'=>'form-control','prompt' => 'Select Category']),
*/
],
'ProgramStudi',
'Tanggal_Masuk',
'Tanggal_Lulus',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>
and here is the model
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "alumnys2".
*
* #property string $NRP
* #property string $NamaMahasiswa
* #property string $ProgramStudi
* #property string $Tanggal_Masuk
* #property string $Tanggal_Lulus
*
* #property AlumniIntegrasi $nRP
*/
class AlumnyS2 extends \yii\db\ActiveRecord
{
/**
* #inheritdoc
*/
public static function tableName()
{
return 'alumnys2';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['NRP', 'NamaMahasiswa'], 'required'],
[['NRP'], 'string', 'max' => 15],
[['NamaMahasiswa'], 'string', 'max' => 50],
[['ProgramStudi'], 'string', 'max' => 5],
[['Tanggal_Masuk', 'Tanggal_Lulus'], 'string', 'max' => 30],
[['NRP'], 'exist', 'skipOnError' => true, 'targetClass' => AlumniIntegrasi::className(), 'targetAttribute' => ['NRP' => 'NRP']],
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'NRP' => Yii::t('app', 'Nrp'),
'NamaMahasiswa' => Yii::t('app', 'Nama Mahasiswa'),
'ProgramStudi' => Yii::t('app', 'Program Studi'),
'Tanggal_Masuk' => Yii::t('app', 'Tanggal Masuk'),
'Tanggal_Lulus' => Yii::t('app', 'Tanggal Lulus'),
];
}
/**
* #return \yii\db\ActiveQuery
*/
public function getNRP()
{
return $this->hasOne(AlumniIntegrasi::className(), ['NRP' => 'NRP']);
}
}
I want to display NamaMahasiswa in table alumniintegrasi. how can I fix that?
You should use a proper getter for the field NamaMahasiswa
in your model
change the name of model getter (aviding case convention problem for name
/**
* #return \yii\db\ActiveQuery
*/
public function getNrp()
{
return $this->hasOne(AlumniIntegrasi::className(), ['NRP' => 'NRP']);
}
and add the getter simply use the attribute name (based on getter)
/* Getter for NamaMahasiswa */
public function getNamaMahasiswa() {
return $this->Nrp->NamaMahasiswa;
}
in the view
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute' => 'namaMahasiswa',
],
'ProgramStudi',
'Tanggal_Masuk',
'Tanggal_Lulus',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
NB using UpperCamleCase ( Pascal Notation) for database name is not the best choice .. for database is better use underscore notattion ..

Call to a member function saveAs() on a non-object[Yii 2]

I am new to yii and php.
I want to upload a file and save its path to database but while doing so I got an error.
My controller class is:
public function actionCreate()
{
$model = new Quiz();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$fileName = $model->name;
$model->file =UploadedFile::getInstance($model,'file');
$model->file->saveAs('uploadQuiz/'.$fileName.'.'.$model->file->extension );
$model->filePath = 'uploadQuiz/'.$fileName.'.'.$model->file->extension ;
$model->save();
return $this->redirect(['view', 'id' => $model->idQuiz]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
My database column name where I save my file path is "filePath".
My view file is:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* #var $this yii\web\View */
/* #var $model app\models\Quiz */
/* #var $form yii\widgets\ActiveForm */
?>
<div class="quiz-form">
<?php $form = ActiveForm::begin(['option' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($model, 'Course_idCourse')->textInput(['maxlength' => 100]) ?>
<?= $form->field($model, 'name')->textInput(['maxlength' => 100]) ?>
<?= $form->field($model, 'description')->textInput(['maxlength' => 255]) ?>
<?= $form->field($model, 'duration')->textInput(['maxlength' => 100]) ?>
<?= $form->field($model, 'time')->textInput() ?>
<?= $form->field($model, 'file')->fileInput(); ?>
<?= $form->field($model, 'totalMarks')->textInput(['maxlength' => 100]) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
My rules and attributes are:
public function rules()
{
return [
[['Course_idCourse', 'duration', 'time'], 'required'],
[['Course_idCourse', 'duration', 'totalMarks'], 'integer'],
[['time'], 'safe'],
[['file'],'file'],
[['name', 'filePath'], 'string', 'max' => 200],
[['description'], 'string', 'max' => 255]
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'idQuiz' => 'Id Quiz',
'Course_idCourse' => 'Course Id Course',
'name' => 'Name',
'description' => 'Description',
'duration' => 'Duration',
'time' => 'Time',
'file' => 'Quiz ',
'totalMarks' => 'Total Marks',
];
}
Now I already refer this site for the same question but I find it for update not for create . kINDLY HELP ME.
When I run try to create I got an error
Call to a member function saveAs() on a non-object
I don't figure where am I going wrong.
No file is being uploaded. The option parameter in the ActiveForm initialization should be options
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>

Categories