Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
Phalcon 3
  • Overview
  • Transcript

3.1 Fetching Contacts

In this lesson, we’ll start by creating a controller and view for the contact data index. Next we’ll create a new model that can connect our view to the database back end. Finally, we’ll go deeper with views and see how to fetch and display an array of records.

Related Links

3.1 Fetching Contacts

Hello everyone. Welcome back. This is chapter number three of our course and we have come a long way. As everyone know from the last lesson, this chapter is all about models, views, and controllers. In particularly, we will be implementing curb functionalities into our application in this chapter. So, with starting from this lesson which I have divided into 3 phases. In phase 1, we will repeat the process of creating a controller. And view like we did in the previous chapter. But I do have something new saved up for you in this one. Then, in next phase, we will create our first model and learn how to use them. We will talk more about that in a bit. The last phase is for playing with views on a bit deeper level. And displaying the records we will fetch. Great so let's get started then. So phase one will consist of four simple steps. Step one is creating a new controller. We can do it by creating a new file, I'm going to name it Contacts Controller. Step two is creating a new class we can start with a simple .php tag. And then creating a new php class with the same name as the file. Then lastly extending it with the Phalcon Controller class along with declaring the class with the user statement above, like this. Then comes to step three which is creating our first action. We can start with defining the index action like we did in our previous controller. Last but not least is step four, where we will create a view. And I hope all of you remember the Phalcon, by default will look for a directory named after the controller which is contact in this case. And a view file named after the action which is index, so index.PHTML. Now comes the best part of this phase. We know how Phalcon finds the view for the corresponding action and we also know how to define some a setting message in the view. But, we haven't passed any data from the action to the view yet. So this time we are going to echo a variable instead of just echoing the static title. Note that this view doesn't have this title variable yet. If you try to quickly test it in your browser, you must get an error. Now let's go to the corresponding action in the controller. These are a couple of different ways you can pass some data to your view. From all these options this first one set perimeter view is an alias for set var method. Both of them work similarly so we can eliminate this from the list. Now either you can use set variable and set variables method depending on the situation, if you have single or multiple variables. Set variables takes an area as a parameter and set variable on the other hand takes just a name. And the value of your parameter you want the send to the view. Or you can also set a variable name like this directly to the view object. And then assign whichever type of value you have, be it a string, number, area, or an object. I personally like this last approach. So let's test this in the browser. You can request this action by the base url, followed by the name of the controller and then the action. In case of index, the action name is optional. And there you go with your title message. Now go back to your controller, and one thing that you need to notice here is the view object. This comes from the service we registered in our DI container earlier. The view class offers lots of other methods for customization other than the setval and setvars. You can check them all here in the Phalcon documentation. Well, getting back to our text editor we are done with phase one. Now the phase two is for creating models. In the Phalcon application there is no major difference in creating a model or a controller class. I have divided this phase into four simple steps as well. Three of them will follow for the creation of model and last will be on how to use it. So starting with step one by creating a file in models directory. Which by the way doesn't carry a suffix in its name like a controller does. So just naming it after the table name in your database would be all. Second step is creating a class, just like the controller class but in this case it must extend to Phalcon MVC model class. Also don't forget to declare the model class you are extending up here. Phalcon MVC model class is the base for all models in Phalcon application. It provides us a lot of rich features like database independence, ORM, basic road functionalities, advanced finding capabilities and among many other services. After that third step to finish your model is declaring of all the columns in your table like this. A quick tip from your Phalcon documentation which says it's recommended that if you are using PHP 5.4, 5.5. You should declare each column that makes part of the model in order to save memory and reduce the memory allocation. If not you are free to skip this step. With this we are done with the models, now before we move to our last step and I show you how to use them. Some quick notes and tips I would like to share. Like, by default, this model contacts will get associated with the table name contacts in your database. Hence, a model should always be named after the table name. But there can be many situations where you want a different name like where you are using a legacy database with a replication, let's say. Then in such cases we can change this default behavior with a quick fix, using a public method getSource like this in your model, and then manually returning the name of your mapping table. But since we don't need it in our case, I will comment it for your reference. The next is this CamelCased file names for this model. Which is not forced by the framework. In fact you can keep it as you please, though I think it's good practice to keep controllers and models with the same naming convention. Now moving forward to the last step. This is how we can use models in any controller to find all the records from the table. There is this fine method which will fetch all the records in the table for us. Phalcon MVC model class provides lots of [INAUDIBLE] similar to find for fetching and inserting records in your DB very easily. If you look into this documentation, this line method can also be used in many other ways like this. It can take a parameter which is equal to adding a [INAUDIBLE] clause in your SQL query, and then there is an array for different options like orderby and limit. Also if you want just one record in your resource it offers the findfirst method which is similar to putting a limit one in your SQL query. Other than these, there are many useful options like find by, then a column name like find by id and find by name. So I highly encourage you to go through this documentation for exploring all the options. But for now coming back to the code and following my lead will get you familiar with most of them. Once we have the results, we can send them to the view like this. Name your variable and assign the results area to it. Now we can find all the records in our view in this variable named Contacts. Great, so with this we are finished with this phase. We have a model now, and we know how to use it. We will keep exploring it with other functionalities in coming lessons. But for this lesson, it is time for our last entered phase the views. So we already have a view for index action. And now we can access all the records in a variable named contacts. To display the contacts in our view I have already written a basic table structure for our data which is pretty self-explanatory. We have three columns name, email and phone. And here we have an emptied row which we will use to display a record from the database by simply echoing the variable we will receive from the controller and followed by the name of the column like this. And since the variable contact is an array and can contain multiple records, let's use the index of the first row to test the scenario. Lastly, let's copy for all the columns. Great. Now, if you turn over to your browser and hit the same URL, you must see something like this. The values of your first row from the database table. We can quickly confirm this from the database as well. Here we go, the first row with the same name. Now let's display all the rows from this table in the view. For this, we know that this context variable is an array and it contains all the records. So to display all, we just need to iterate through this array with a for each loop like this. And also, don't forget to close the curly bracket for for each loop down here. This tr tag represents a row in the table. So we are looping through this to duplicate the HTML code with each record and create required number of table rows. Last thing is just change this variable name. Since now each row due to this loop can be accessed in this contact variable. And that's it, refreshing the page in your browser should now display all the rows in your window and all that looks great. They are displayed exactly the way they are saved in the database. But wouldn't it be better if we could get our contacts in alphabetical order, based on their names? That's how general phone books look like and that's what my Google contacts does it at least. You can do it very easily in our controller action by just adding a parameter in the find method. An area with an element order with the name of the column we want to start, exactly the way we have seen in the documentation. By default it orders in ascending order. But you can change this behavior by placing your option ascending or descending after the name like this. Though in case of ascending It's totally optional and that's it. Saving the changes here and then refreshing the same page in your browser there should now display all the rows in your window. But in ascending order at this time and with this we have successfully finished this lesson. I hope you enjoyed this lesson seeing something on the screen is always very exciting this lesson was quite a basic. But I really hope with this you have got familiar enough with the MVC in Phalcon. To dive a bit deeper than this in the next lesson. So get ready for the next lesson where I will be teaching you how to make Forms in your view, how to use Tags, why did we define the baseURL In the bootstrap. How is the FactoryDefault component in our bootstrap file really helping us? And that's not just it. I will also be teaching you how to remove and create a new record in Phalcon by using models we created. And if that's not enough, I might also give you a quick sneak peek into model validation. So see you guys in the next one. Until then bye bye

Back to the top