Sometimes you'll want to do something more complex than displaying a single record from a database table. You will want to join in records that are from another database table as well as from a primary one. To do this, you want to avoid JTable and do a custom query in your model. So let's do that to show at single mall.
So first, go to the models folder in your com_noidacity component which have created earlier and create a file mall.php and write the following lines of code.
we again have a getItem function that is just like the one for place which we created in earlier tutorial, but in this case, instead of pulling in a JTable object, we are doing a custom database query.
We are pulling in the id and using getInt again, but this time, we are storing it as mall_id
Then we are getting a database object from the model. We just call this getDbo and that pulls in the database object, and then we are getting a fresh query by calling getQuery and passing in true. If we don't pass in true, we get the last query that was executed, and that's not what we want.
Next, we are building our query incrementally. We have select from, join in where and all those come together to pull in the data from the malls table, as well as some associated data from the place(table). So in this case, we are getting the mall that matches the mall_id that's coming in from the request.
We are setting the query in the database using the setQuery function, and then we are calling the loadObject function on the database object. This returns a single object that's not in an array. And then once we have that row we can return it.
So with this model in place, now we have to do is add a view for the mall. So go to the views folder in your com_noidacity component, create a mall folder and create the view.html.php and tmpl (default.php) our layout file.
So let's take a look at mall and let's add the following code in view.html.php file
You will notice again we are just getting this getItem just like we did for place (in our previous tutorial).
Then let's create the layout file, default.php in our tmpl folder and add the following line of code.
In this case we are getting the mall_name and the place_name that we joined in our database query in model. We are getting the mall_description, along with some details about the mall.
So if we go to the front end and go to index.php, option=com_noidacity&view=mall&id=1, it pulls in the name of the mall and it pulls in the name of the place near that mall. And then it pulls in all the information about the mall, including the details. So whenever you want to get more than just a single row from a single table, you can use a model to do a custom query and then that way you can get more complex data to display it in your view.