I’ve got a grid view set up with searching (using a search model) and I’m using joinWith and getting a very odd exception (listed below). Does anyone have an idea why? I’ve the code for both ActiveRecord classes and the search model class as well.
Thanks.
Unknown Property – yii\base\UnknownPropertyException
Getting unknown property: common\models\Contact::
1. in /Applications/MAMP/htdocs/iperform/vendor/yiisoft/yii2/base/Component.php at line 143
134135136137138139140141142143144145146147148149150151152 foreach ($this->_behaviors as $behavior) {
if ($behavior->canGetProperty($name)) {
return $behavior->$name;
}
}
}
if (method_exists($this, 'set' . $name)) {
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
} else {
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}
}
/**
* Sets the value of a component property.
* This method will check in the following order and act accordingly:
*
* - a property defined by a setter: set the property value
* - an event in the format of "on xyz": attach the handler to the event "xyz"
2. in /Applications/MAMP/htdocs/iperform/vendor/yiisoft/yii2/db/BaseActiveRecord.php – yii\base\Component::__get('') at line 247
3. in /Applications/MAMP/htdocs/iperform/vendor/yiisoft/yii2/base/Model.php – yii\db\BaseActiveRecord::__get('') at line 926
4. in /Applications/MAMP/htdocs/iperform/vendor/yiisoft/yii2/db/ActiveQuery.php – yii\base\Model::offsetGet(false) at line 263
5. in /Applications/MAMP/htdocs/iperform/vendor/yiisoft/yii2/db/ActiveQuery.php – yii\db\ActiveQuery::removeDuplicatedModels([common\models\Contact, common\models\Contact, common\models\Contact, common\models\Contact, ...]) at line 220
6. in /Applications/MAMP/htdocs/iperform/vendor/yiisoft/yii2/db/Query.php – yii\db\ActiveQuery::populate([['id' => '3', 'user_id' => '1', 'company_id' => '3', 'lastname' => 'Abrams', ...], ['id' => '260', 'user_id' => '1', 'company_id' => '76', 'lastname' => 'Adams', ...], ['id' => '313', 'user_id' => '1', 'company_id' => '90', 'lastname' => 'Adelman', ...], ['id' => '1236', 'user_id' => '1', 'company_id' => '44', 'lastname' => 'Agosta', ...], ...]) at line 207
7. in /Applications/MAMP/htdocs/iperform/vendor/yiisoft/yii2/db/ActiveQuery.php – yii\db\Query::all(null) at line 130
8. in /Applications/MAMP/htdocs/iperform/vendor/yiisoft/yii2/data/ActiveDataProvider.php – yii\db\ActiveQuery::all(null) at line 112
9. in /Applications/MAMP/htdocs/iperform/vendor/yiisoft/yii2/data/BaseDataProvider.php – yii\data\ActiveDataProvider::prepareModels() at line 79
10. in /Applications/MAMP/htdocs/iperform/vendor/yiisoft/yii2/data/BaseDataProvider.php – yii\data\BaseDataProvider::prepare() at line 92
11. in /Applications/MAMP/htdocs/iperform/vendor/yiisoft/yii2/data/BaseDataProvider.php – yii\data\BaseDataProvider::getModels() at line 133
12. in /Applications/MAMP/htdocs/iperform/vendor/yiisoft/yii2/widgets/BaseListView.php – yii\data\BaseDataProvider::getCount() at line 124
13. in /Applications/MAMP/htdocs/iperform/vendor/yiisoft/yii2/grid/GridView.php – yii\widgets\BaseListView::run() at line 239
14. in /Applications/MAMP/htdocs/iperform/vendor/kartik-v/yii2-grid/GridView.php – yii\grid\GridView::run() at line 702
696697698699700701702703704705706707708 $this->containerOptions['id'] = $this->options['id'] . '-container';
Html::addCssClass($this->containerOptions, 'kv-grid-container');
$this->registerAssets();
$this->renderPanel();
$this->initLayout();
$this->beginPjax();
parent::run();
$this->endPjax();
}
/**
* Renders the table page summary.
*
15. in /Applications/MAMP/htdocs/iperform/vendor/yiisoft/yii2/base/Widget.php – kartik\grid\GridView::run() at line 96
16. in /Applications/MAMP/htdocs/iperform/frontend/views/contact/index.php – yii\base\Widget::widget(['id' => 'contact-grid', 'pjax' => true, 'dataProvider' => yii\data\ActiveDataProvider, 'filterPosition' => 'header', ...]) at line 129
123124125126127128129130131132133134 'header'=> 'Info',
'content' => function ($data) {
return '';
},
],
],
]);
?>
<?php Pjax::end() ?>
</div>
</div>
17. in /Applications/MAMP/htdocs/iperform/vendor/yiisoft/yii2/base/View.php – require('/Applications/MAMP/htdocs/iperfo...') at line 325
18. in /Applications/MAMP/htdocs/iperform/vendor/yiisoft/yii2/base/View.php – yii\base\View::renderPhpFile('/Applications/MAMP/htdocs/iperfo...', ['search' => common\models\ContactSearch, 'dataProvider' => yii\data\ActiveDataProvider]) at line 247
19. in /Applications/MAMP/htdocs/iperform/vendor/yiisoft/yii2/base/View.php – yii\base\View::renderFile('/Applications/MAMP/htdocs/iperfo...', ['search' => common\models\ContactSearch, 'dataProvider' => yii\data\ActiveDataProvider], frontend\controllers\ContactController) at line 149
20. in /Applications/MAMP/htdocs/iperform/vendor/yiisoft/yii2/base/Controller.php – yii\base\View::render('index', ['search' => common\models\ContactSearch, 'dataProvider' => yii\data\ActiveDataProvider], frontend\controllers\ContactController) at line 367
21. in /Applications/MAMP/htdocs/iperform/frontend/controllers/ContactController.php – yii\base\Controller::render('index', ['search' => common\models\ContactSearch, 'dataProvider' => yii\data\ActiveDataProvider]) at line 68
62636465666768697071727374 ],
]);
}
return $this->render('index',
array('search'=>$search,
'dataProvider'=>$dataProvider));
}
public function actionUpdate()
{
return $this->render('update');
}
22. frontend\controllers\ContactController::actionIndex()
23. in /Applications/MAMP/htdocs/iperform/vendor/yiisoft/yii2/base/InlineAction.php – call_user_func_array([frontend\controllers\ContactController, 'actionIndex'], []) at line 55
24. in /Applications/MAMP/htdocs/iperform/vendor/yiisoft/yii2/base/Controller.php – yii\base\InlineAction::runWithParams(['ContactSearch' => ['lastname' => ''], '_pjax' => '#contact-grid-pjax']) at line 151
25. in /Applications/MAMP/htdocs/iperform/vendor/yiisoft/yii2/base/Module.php – yii\base\Controller::runAction('', ['ContactSearch' => ['lastname' => ''], '_pjax' => '#contact-grid-pjax']) at line 455
26. in /Applications/MAMP/htdocs/iperform/vendor/yiisoft/yii2/web/Application.php – yii\base\Module::runAction('contact', ['ContactSearch' => ['lastname' => ''], '_pjax' => '#contact-grid-pjax']) at line 83
27. in /Applications/MAMP/htdocs/iperform/vendor/yiisoft/yii2/base/Application.php – yii\web\Application::handleRequest(yii\web\Request) at line 375
28. in /Applications/MAMP/htdocs/iperform/frontend/web/index.php – yii\base\Application::run() at line 18
12131415161718 require(__DIR__ . '/../../common/config/main-local.php'),
require(__DIR__ . '/../config/main.php'),
require(__DIR__ . '/../config/main-local.php')
);
$application = new yii\web\Application($config);
$application->run();
<?php
namespace common\models;
use Yii;
/**
* This is the model class for table "contact".
*
* @property string $id
* @property string $user_id
* @property integer $company_id
* @property string $lastname
* @property string $firstname
* @property string $title
* @property string $email
* @property string $website
* @property string $notes
* @property string $picture
* @property string $picture_type
* @property integer $accepts_postcards
* @property integer $attends_showcases
* @property string $type
* @property string $met_date
* @property string $birthday
* @property integer $birthday_cal_id
* @property string $anniversary
* @property integer $ann_cal_id
* @property string $screen_name
* @property integer $screen_name_type
* @property integer $source
* @property integer $status
* @property integer $external_id
*
* @property ContactAddress[] $contactAddresses
*/
class Contact extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'contact';
}
public function init()
{
parent::init();
$this->user_id = Yii::$app->user->id;
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['user_id', 'accepts_postcards', 'attends_showcases', 'type', 'birthday_cal_id', 'ann_cal_id', 'source'], 'required'],
[['id','user_id', 'company_id', 'accepts_postcards', 'attends_showcases', 'type', 'birthday_cal_id', 'ann_cal_id', 'screen_name_type', 'source', 'status', 'external_id'], 'integer'],
[['notes', 'picture_type', 'screen_name'], 'string'],
[['met_date', 'birthday', 'anniversary'], 'safe'],
[['lastname', 'firstname', 'title', 'website'], 'string', 'max' => 64],
[['email', 'picture'], 'string', 'max' => 32]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'user_id' => 'User ID',
'company_id' => 'Company ID',
'lastname' => 'Lastname',
'firstname' => 'Firstname',
'title' => 'Title',
'email' => 'Email',
'website' => 'Website',
'notes' => 'Notes',
'picture' => 'Picture',
'picture_type' => 'Picture Type',
'accepts_postcards' => 'Accepts Postcards',
'attends_showcases' => 'Attends Showcases',
'type' => 'Type',
'met_date' => 'Met Date',
'birthday' => 'Birthday',
'birthday_cal_id' => 'Birthday Cal ID',
'anniversary' => 'Anniversary',
'ann_cal_id' => 'Ann Cal ID',
'screen_name' => 'Screen Name',
'screen_name_type' => 'Screen Name Type',
'source' => 'Source',
'status' => 'Status',
'external_id' => 'External ID',
];
}
public static function find()
{
return parent::find()->where(['contact.user_id' => Yii::$app->user->id]);
}
/**
* @return \yii\db\ActiveQuery
*/
// public function getAddresses()
// {
// return $this->hasMany(ContactAddress::className(), ['contact_id' => 'id']);
// }
public function getCompany()
{
return $this->hasOne(Company::className(), ['id' => 'company_id']);
}
}
<?php
namespace common\models;
use Yii;
/**
* This is the model class for table "company".
*
* @property string $user_id
* @property integer $id
* @property string $name
* @property string $website
* @property string $email
* @property string $notes
* @property integer $acceptsPostcards
* @property integer $attendsShowcases
* @property string $type
* @property integer $source
* @property integer $status
*/
class Company extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'company';
}
public function init()
{
parent::init();
$this->user_id = Yii::$app->user->id;
}
public static function find()
{
return parent::find()->where(['company.user_id' => Yii::$app->user->id]);
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['user_id', 'id', 'acceptsPostcards', 'attendsShowcases', 'type', 'source'], 'required'],
[['user_id', 'id', 'acceptsPostcards', 'attendsShowcases', 'type', 'source', 'status'], 'integer'],
[['name', 'website', 'email', 'notes'], 'string'],
[['user_id', 'id'], 'unique', 'targetAttribute' => ['user_id', 'id'], 'message' => 'The combination of User ID and ID has already been taken.']
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'user_id' => 'User ID',
'id' => 'ID',
'name' => 'Name',
'website' => 'Website',
'email' => 'Email',
'notes' => 'Notes',
'acceptsPostcards' => 'Accepts Postcards',
'attendsShowcases' => 'Attends Showcases',
'type' => 'Type',
'source' => 'Source',
'status' => 'Status',
];
}
}
<?php
namespace common\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Contact;
/**
* MileageSearch represents the model behind the search form about `app\models\Contact`.
*/
class ContactSearch extends Contact
{
/**
* @inheritdoc
*/
public function rules()
{
return [
[['user_id', 'accepts_postcards', 'attends_showcases', 'type', 'birthday_cal_id', 'ann_cal_id', 'source'], 'required'],
[['user_id', 'id','company_id', 'accepts_postcards', 'attends_showcases', 'type', 'birthday_cal_id', 'ann_cal_id', 'screen_name_type', 'source', 'status', 'external_id'], 'integer'],
[['notes', 'picture_type', 'screen_name'], 'string'],
[['met_date', 'birthday', 'anniversary'], 'safe'],
[['lastname', 'firstname', 'title', 'website'], 'string', 'max' => 64],
[['email', 'picture'], 'string', 'max' => 32]
];
}
/**
* @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)
{
if(isset($params['ContactSearch'])){
if(is_array($params['ContactSearch']['lastname']))
$search_criteria = implode(" ",$params['ContactSearch']['lastname']);
else
$search_criteria = $params['ContactSearch']['lastname'];
}else{
$search_criteria=NULL;
}
$query = Contact::find()
->joinWith('company',true,'LEFT JOIN')
->filterWhere(['like', 'lastname', $search_criteria])
->orFilterWhere(['like','firstname', $search_criteria])
->orFilterWhere(['like','contact.email', $search_criteria])
->orFilterWhere(['like','title', $search_criteria])
->andWhere(['contact.user_id' => Yii::$app->user->id])
->orderBy('lastname asc');
// if (!$this->validate()) {
// // uncomment the following line if you do not want to any records when validation fails
// // $query->where('0=1');
// return $dataProvider;
// }
return new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 20,
],
]);
}
}