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

3.2 Deleting Contacts

In this lesson, I’ll teach you how to remove a record from the database in a Phalcon app. You’ll also learn to use some supporting goodies! We’ll build up some functionality and understanding of concepts that will serve us well as we continue to implement the app.

Related Links

3.2 Deleting Contacts

Hey everybody, welcome back. In this lesson, I'm going to teach you how to remove a record from the database in a Phalcon application, along with some supporting goodies and concepts on the way that are going to help you achieve this. So let's start by adding some URLs for add, update, and delete actions in the view first. This is how our index page currently looks like. Open up the index.phtml file in your editor. Well, now we can basically add a URL with a simple HTML anchor tag like this. But Phalcon provides us with a service called Tag. It simplifies the building of HTML tags. It provides a set of helper methods to generate HTML in a dynamic way. Like a link to method can generate an anchor tag for us by taking some parameters. There are many different ways you can generate the tags and customize them as per your need. But here for the sake of this tutorial, I am going to use the basic way where my first parameter is the URL to the action, which has a controller and action name in it. And the next is the name for the URL, simple. Then refresh the page in your browser, and there you go. Our add new contact link is already there. If we check the source code of the page, you should see the anchor tag as expected. So the link to method has generated this line for us. Another thing worth noticing is the URL. If you remember, we just provided the controller and action name in the URL back-end action. But here we can see a full URL. This is where our base URL helps us. It takes care of all the URLs in our application. Next time, if you move to another server or subdomain, we don't have to worry about anything as long as we will take care of the base URL and the Bootstrap file if needed. Great, now let's quickly repeat the process and add some added and delete URLs. We need an added and delete action for each record on the page. That's the reason we are generating them inside the loop. Also, since these are row-specific actions, the URL will also need a unique primary key to relate with their specific row. For that, we can take the ID for each record from this contacts object. Just like we have used the name and the email up here. And then concatenate that with the URL like this. Great, now, jumping back to the browser and refreshing the page, there we have all the URLs. If we check the source code again, each URL has its unique ID in the end. And, if you remember from the earlier lesson, anything after the name of the action is considered as its parameter. We will see how to use them in the controller, but for now we have some broken URLs in our page. So let's just start by implementing these actions in our controller. I will start with the delete method, since it's the most simplest and the easiest one here. A quick note, I expect some of you didn't notice that we never registered this tag service here in the Bootstrap file, but we still were able to generate the links. That is because of this factory default component here. It has got us covered by registering most of the full stack services we will need in our application. Coming back to our controller, we are implementing the delete action. And deleting a record in Phalcon is very easy, but still, I have divided the process into two parts for better understanding. In part one, we will just define the function. Then by passing a parameter here, in the function you can get the values passed in the URL as HTTP parameters. In our case, the ID. Next, we need to find the record by using findFirst, a method from the MVCModel class which which can take the ID of the parameter and fetch the record from the database. And then call the delete method on it to remove the record. Very simple. Let's quickly test it. Okay, so a blank page. That means our delete request was smoothly finished without an error. We can confirm the deletion from the database. There you go. The contact with ID 3 and name Kumar is not in our database anymore. Great, so it's good. But there are a couple of issues with this that we can fix and make it a bit more robust. One of the first issue is the redirection to another page. Wouldn't it be better if we could just remove the record and stay on the same page? Next, it doesn't display any success message, so it's a bit confusing. And if you try to delete any invalid ID, It throws some fatal error like this. So it must display some human readable success and error messages by the given situations. Hence, the part two of the process, error messages and proper redirection. For the error messages, we can simply do it with an if statement and check if we found a record. If not, just display the nice error message, like, don't try to remove a contact that doesn't even exist in the first place. Good. To display messages, we are using a service called flash. Once again, just like tags, we didn't have to register it manually, because factory default has already done it for us. Though one interesting insight is that it provides two adapters. One is direct, which just outputs the message. Another is session, where you can save the message and then display it later. Factory default has registered the direct adopter version for us. Using it is very simple. It provides you with these four basic types of flashing your message. But we can always create one of our own type, by flash message method. The only difference that these all message types have is the HTML output they generate. They all generate a div tag, but with different classes. You can always write CSS for these classes and represent them in your view accordingly. Or you can always change the default classes by manually registering the service in the Bootstrap file. Declaring the class first, then registering the service with the object, and lastly creating a new instance. Then in its parameter, you can provide your preferred classes or any other configuration option from the documentation, and voila, you will get your tailored output in your view. It can be very handy while using front end frameworks like Bootstrap and templates with predefined CSS classes. Also, you can just override one or create one of your own, it doesn't have to be all at the same time. So coming back to the code, display the error message if you don't find the record or else run delete operation. And similarly, if the delete operation failed, we will write another error message. And on success, we can flash a success message. Something like, The Contact R.I.P. successful. And here in case of failure, you could just flash a simple text message like a success case. But if DB fails on a delete operation, it can be due to many reasons. So we can display specific SQL error in this case. We can find the error messages by calling a method called get messages on the contacts object that we fetched from the database. And since it will return an array, it can contain a single or multiple error messages at once. So we can iterate through this array of the error messages with a for each loop like this, and then flash the errors, just like that. Now, for the redirection issue and sending it back to the index page, we can do it by a Phalcon service called dispatcher. It's a very powerful service. Even the application component uses it to execute respective actions after the handling of request. I'm using the forward method, which passes the execution to the given action, in our case, to index. You can also transfer it to an action in any other controller if needed. But if you are doing it within the controller, you don't need to mention it. Just one last thing, which is preparing your index view to display the flash messages. We can do it by using a method called getContent. And with this, our view is ready to display the flash messages it will receive from the controller action. Great, let's give it a try. Refreshing the page and trying to delete a record, and here we go. Now with an invalid ID, great. Both of the cases work really well. We also get to be on the index page along with the error or success message accordingly. So this is it from this lesson, guys. I really hope the concepts of the Bootstrap file lesson has at last started to make some sense now. The more deeper we will dive, the better we will get. But there is one more tip that I want to pass on before finishing this lesson. I always encourage everyone to look into the documentation more often. Not just for Phalcon, but but for any new platform that you want to learn. Because in my opinion, it's a very essential piece of resource to learn any technology. No course or tutorial can cover almost all the sides of just even a single feature, but documentations do. They are thorough and they are detailed. So I will finish by once again encouraging you guys to explore it more often, look into the services I am introducing in each lesson, or even better, if you can find ones you will like to play around with them on your own. Till then, bye bye.

Back to the top