I try to have a functionnal gridview with a collection that have an integer as _id field instead of an objectid. In gridview, I can filter my columns with all fields except the _id. And I can’t have a detailview because it seems that it expect an objectid as the primary key. I then have the message: " Please specify the “model” property." in reference to the view line: echo DetailView::widget([ ‘model’ => $model
In my controller, I have this usual code:
public function actionView($id)
{
$model = Products::findOne(['_id' => Yii::$app->request->get('_id')]);
return $this->render('view', [
'model' => $model,
]);
}
It’s the first time I have to use a MongoDb database and I’m a little bit lost here. How can I define in the model, that the primary key of my Mongodb collection is an integer?
Thanks a lot!
Thanks InsaneSkull. I have the same error message with your code. And the message, as we can see in detailview code is
if ($this->model === null) {throw new InvalidConfigException('Please specify the "model" property.');
Which means that the request doesn’t return anything.
My code in controller for gridview (which shows all the data in my collection and which I can filter with any fields except _id) is the default one generated by gii:
public function actionIndex()
{
$searchModel = new ProductsSearch;
$dataProvider = $searchModel->search(Yii::$app->request->getQueryParams());
return $this->render('index', [
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
]);
}
I must specify that I have other collections with an obectid as primary key and I don’t have any problem with those.
The output, with vardump is an empty array. I have find a way to make it work by selecting an alternative primarykey, which also have unique values. And I have set it in the model like this::
public static function primaryKey()
{
// return ['_id'];
return ['product'];
}
And then, in my queries:
query->select(['product', 'name'])
->from('products')
->where(['product' => $id]) //this work
->where(['_id' => $id]) //this does not work
Thanks for your help! Now, I have something working, not the ideal solution, but it works. Still curious to know why I can’t have a result with the true primarykey.
The problem is how the view url is generated. Url may be generated using different column instead of _id. Check the code of the link and change the view url in grid if needed.