Redeem Now
Read More
Read now
Menu Close
    Start a Project Request quote Reading list Switch to dark mode

    Magento Development 09: Editing and Updating

    Let’s give our users an option to edit their blogs.

    First we will edit the blogs page and an edit link, let’s edit view/frontend/templates/list.phtml

    Here we are adding an edit column on the list page. You may have noticed that we are passing an associative array in the second param of the getUrl function. It gets appended to the URL as key value pair (i.e. ?key=value) as in get requests.

    Here if you check the url of the edit link you will find something like, http://hostname/blog/manage/edit/id/203/ , the id/203 is equivalent to ?id=203
    Anything after the frontName/controllerName/actionName is considered as key value pair. That means we can think of the url as http://hostname/frontName/controllerName/actionName/key1/value1/key2/value2

    Edit page

    Now we have to create the edit page. For that we have to create controller, layout file, template file and block. Let’s create these files one by one.

    The controller file will be Controller/Manage/Edit.php

    Here we have extracted the blog id from the get request then we have get the current customer id. We are collecting all of these to check if the current user is authorised user of the blog. Because if we do not do this check then any one can access any blog just be changing the id in the url.
    Here if you notice we are using the model factory and calling the getCollection method which will return the collection factory. And we are using two addFieldToFilter methods so these conditions will be combined with AND operator. So the condition will look like “WHERE user_id=$customerId AND entity_id=$blogID“.

    We have already discussed all of these earlier also. But what is getSize() method? The getSize will return the count, i.e. the number of rows or models in the collection. So the sql query will be like select count(*)
    That means the actual query will be something like “SELECT COUNT(*) FROM blogmanager_blog WHERE user_id=$customerId AND entity_id=$blogID”
    As you can see here we can chain the methods one after the another which makes easier to write complex queries.

    The $isAuthorised will contain the count, which will be zero if there is no such entry in the table. In that case we have redirected the user to the list page with error message. Otherwise we will show the edit form.

    Now let’s create the layout file view/frontend/layout/blogmanager_manage_edit.xml

    There is nothing new here as we have seen similar syntax for list page as well.

    Now let’s create the template file view/frontend/templates/edit.phtml

    Here we are getting the blog data through the block class by calling the getBlog method. We are displaying the title and content in their respective input fields. One thing that you have to notice here is that we are using an hidden input field which will be used to pass the blog id along with the blog data when we submit the edited blog data.

    Now let’s create the block file, Block/Blog.php

    Here we have loaded the blog model by the blog id by calling the load method and passing the blog id in the param because the id is the primary key. And we have returned the loaded model. Which we have used in the template file.

    Now let’s see how the edit page looks like,

    Updating Blog

    Instead of creating a new controller to submit the edit data, we will be modifying the Controller/Save.php which we used to save new blogs.

    Here first we have checked whether the id field exist or not. If id exist in the post data then the user is updating a blog otherwise s/he is creating a new blog. So we have put the previous code in the else section.
    Let’s focus on the if section, here we have again checked if it’s the right user because someone can change id field with inspect element feature.
    To save the updated data we have loaded the model with load method and in that we have set the updated title and content with setTitle and setContent respectively. After that we have saved the model and added a success message, and in the end we have redirected to list page.

    Folder structure should look like,

    Next Blog -> Magento Development 10: Deleting

    Previous Blog -> Magento Development 08: Filter and Sorting

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*

    Be the first to comment.

    Back to Top
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again