Buy Now
Sign in

FAQ search

Adding 1 more belongsTo model to our connection

Published: Tuesday, 08 April 2014 15:13
What if we want to load data from other db tables which are associated with our main table ? we can do this by adding more models to our connection.
In this tutorial we will connect our articles model to the users table to load the data of the article's author.
Let's go to the "Models" tab, then click the "Add new model" button.
Enter the model title as "User" (2 times if needed), then select the table's name from the tables list, we should select the ___users table.
The relation selection can be determined easily, first, both the article and user models are related, so this can't be left empty, 2nd, each article is "related to" only 1 user, so we can select here "belongsTo", since the "Article" belongs to a "User".
The "Associated model" field can be left empty, since its our "Article" model which belongs to the "User" model.
Since the "Article" model belongs to the "User" model, the articles table should have the "foreign key", and by default, CC will try to find a key named "user_id" (the model id in lower case + the primary field name).
but since this is not the case, and the foreign key field in our table is actually named "created_by" (Joomla default), then we will have to enter "created_by" here.
Save the connection, and now you can use "User.username" in your listing fields to display the username of the article's author.