Codeigniter App Part 8 - Models Task CRUD
Key Takeaways
The video demonstrates how to use CodeIgniter to perform CRUD operations on a tasks table, utilizing the framework's active record and validation library. It covers creating a task controller and model, implementing show, add, edit, and delete functions, and using table joins to display tasks by list.
Full Transcript
all right so in this section we're basically going to add the functionality for our tasks we want to be able to create update and delete tasks and tasks they're not going to have their own index view like like the list does we're not going to have um this go to slash task and then show a list like we are here what we want to do is have that inside of the list have a list of tasks uh what we do want is a show function we want to be able to go to tasks slow and then the ID of that task and I went ahead and created just a couple tasks in PHP my admin uh all right so let's create this show method so we can view a task and the first thing we want to do is create a task controller save this as tasks. PHP we want a task model and let's create a view folder for our tasks and for views we want a show view uh we want an add task View and an edit task view all right so we can close those for now the edit and the ad now in the controller let's go ahead and say class tasks wh it should be a capital extends CI controller and we want to have a public function of show and that should take the ID which comes from the URL and we want to grab the that task info so we'll create a data array value of we'll say this task actually we'll just call that task which will be equal to this DB I'm sorry not this DB this task model get task all right we also want to find out if it's complete and actually no we can do that from within the view View using this all right so let's create um the get task function in the model extends CI model okay so we want to get a task um now we're going to want to actually do a table join we want to get the tasks and we also want to get the the list so we'll have to join the tables and lists but I'm I mean the tasks and lists but I'm not going to do that right now I'll do that probably in the next video so I'm just going to Simply grab the tasks for now so query equals this this DB get from tasks and we want to say this DB whereare ID is equal to ID and return query row all right so now our task info should be available to us uh we need to to link The View here so let me just paste that in okay so this will be our task Show view let's test this out all right so we get to register the task model with the autoloader and that's the last Model that we'll be creating all right so we're in the show view we can now grab this ID um and all the other information I'm going to paste this view in basically we'll have some actions just like we did with the list uh it will show a list of the tasks so let's save that I'm sorry not a list of the tasks but the information to that has to do with this particular task all right so here has our information uh list name so this is something we're going to need later on so let's uh get rid of this which is just a link to the to the list just to get rid of this um error for now and then this one undefined is complete task is complete all right so my original version we had uh a model function to get the completion the is complete so we're going to do that here uh we're setting a data called is complete task model and it's going to be called check if complete which is going to be really simple so I'm just going to paste that in we're just saying we ID uh the task table we want want to return the is complete right and that should get rid of that all right there we go all right so we have we're able to show a single task what we want to do now is we want to be able to add T add a task all right so if we click here we should be able to add a task and if you look at this add we're saying add one this may look a little funny but what this one is is it's the the list ID and you can see that in the view we have task SL addlist ID all right so that's why that's there because obviously if we had a task ID we wouldn't be adding a task we'd be updating it so let's go ahead and create the ad function and we can paste that in valid validation all the same kind of stuff we've been doing we're getting the list name for the view so get list name we can get that through the task model so let's do that okay that's really simple we just returning the list name and let's see what else do we got here we want to get the post data and then we're inserting that using this create task method in the model so we'll do that actually what's this that shouldn't be there all right so we're just going to insert simple insert state and we need the form let's go ahead and go to the task uh the add task View and I'm going to paste in the form this form uses the form helper just like all the other ones okay so if we save that and let's see unexpected get list name task model 15 public oh forgot the word function all right it's still not working uh let's see tasks add not sure why H so the view we created The View which is ADD task. PHP did I not save it all right I must not have saved it right because now it's working all right so we have add task we have the list which is family stuff so let's see pickup son we'll say pick up Sun from baseball that's going to be tomorrow add task and huh let me just let's look at the database see if that went all right so it did show up we don't have our tasks listed on the in the list page yet in the list view um but I want to make it so we get a message so so let's see this is setting this task created whoop this task created message so and that's in the show view all right so it's going back to show list SL showlist ID all right that's good um so we want to go to the list show and I'm actually going to paste in a bunch of different um um messages so when a task gets deleted created updated marked complete Mark new so we'll save that and let's try to add another task to this family thing um say dinner with wife going now to eat with wife and that's going to be Sunday night all right so there's are your task has been created now we're just not seeing them because we haven't added that to the list view yet now what I want to do is create the edit tasks so up here we should be able to say tasks sledit and then then the ID of the task so let's do that now if we go to the controller I'm going to paste in the edit Method All right so we get a few things here we're going to get the task list ID so we want to create this method in the model this get task list ID so let's copy that and paste that in public function and we basically just want to grab the ID of the parent list okay so we're going to say this DB Weare and we want to match the ID to the task ID all right and then we're going to create a variable called query and we'll say this DB get and we're going to grab this from the tasks list and then finally we just want to return the row turn in query row and we want to grab just the list ID so we'll add this on all right so that's that the next one we have is get list name and we're passing in this uh the list ID that we fetched okay so let's go back here and we actually already have that right here so get list name it's being passed in the the ID and we're just saying where the ID matches this list ID uh and we're going to get that from the list table and ultimately it's just returning the list name all right uh this task is going to grab just the task with this ID so we want to create that in the in the model keep doing that all right so get task data and this is another really simple query all right so we're just getting the task with the ID that we pass in and we're just returning the entire row so we can use whatever data that we want all right so let's save the model and then we're just going to load up the edit task View view which is here which we haven't created yet so I'm going to paste this and we're using the form helper once again it's going to be submitted to task sledit and then the the actual ID and this this URI segment is getting the third segment of the URL uh which will be the ID okay so this is the second segment this is the third all right so let's save that and inside our list we don't have a list of tasks yet we still have to do that so the only way to access the edit uh is to manually type it in up here so we'll say tasks sledit SL1 and that gives us the edit page for the for the task with the ID of one okay so that looks good too all right so let's um let's try it out we'll change the title to uh parents dinner okay we can change the date to the 28th update all right so task model edit task we haven't created that yet all right so let's create that now this is going to be a really simple method we're just pass passing in the task ID and we're passing in the the array of data that we want to update with we're saying where the task ID is equal to ID in the database and then just a simple update and then return true so let's try that again all right task has been updated so we can only check it out uh we can actually do show to check it out we can say tasks SL show SL1 okay uh that nope that's we need two uh for some reason that's not it's not showing right uh let's try edit so we'll say task sledit slash1 SL two all right so you can see that parents dinner was updated the date was updated so that's good so we have our show we can show a task we can add a task and we can edit a task all right so uh basically the last thing that we want to do is we want to join these two tables so that when we click on my lists and then we click on a list we want all the tasks available to us so we're going to have to do a table join uh to do that and we'll we'll be using active record to do that so uh I'm going to stop this video and we'll continue that in the next one
Original Description
We will use our task model to perform Create, Update, Read & Delete on the tasks table using active record
The full source code is at - https://github.com/bradtraversy/mytodo_ci
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Traversy Media · Traversy Media · 30 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
▶
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Changing Your DNS/Nameservers
Traversy Media
Create a MySQL database in cPanel
Traversy Media
Install & Uninstall Joomla Extensions
Traversy Media
Adding and linking an article in Joomla
Traversy Media
Create a Joomla Blog
Traversy Media
Import & Export A MySQL Database
Traversy Media
Use A Custom Font On Your Website Using CSS
Traversy Media
Connect Joomla Site With Dreamweaver
Traversy Media
Remove Phoca Gallery 3.2.3 Footer Text
Traversy Media
Drupal 7 Security Update 7.19 to 7.20
Traversy Media
Add An Addon Domain In Cpanel
Traversy Media
Pull A Heroku Rails App and Database
Traversy Media
Create a Custom Joomla 2.5 Module - Part 1
Traversy Media
Create a Custom Joomla 2.5 Module - Part 2
Traversy Media
Create a Custom Joomla 2.5 Module - Part 3
Traversy Media
Joomla SEO Tutorial - sh404sef Configuration
Traversy Media
Font Dragr
Traversy Media
Convert an HTML Template to Joomla 2.5/3.0 - Part One
Traversy Media
Convert an HTML Template to Joomla 2.5/3.0 - Part Two
Traversy Media
Rockettheme Rocketlauncher Joomla Site in Under 10 Minutes
Traversy Media
JQuery FAQ Slider Tutorial
Traversy Media
301 Redirect With htaccess File
Traversy Media
Convert HTML to Wordpress Theme - Part 1
Traversy Media
Convert HTML to Wordpress Theme - Part 2
Traversy Media
Easy JQuery Widgets
Traversy Media
Codeigniter App Part 1 - Creating the Database
Traversy Media
Codeigniter App Part 2 - Installation and Configuration
Traversy Media
Codeigniter App Part 6 - Login/Register System
Traversy Media
Codeigniter App Part 7 - Models List CRUD
Traversy Media
Codeigniter App Part 8 - Models Task CRUD
Traversy Media
Node.js Part 1 - Install NodeJS on Windows
Traversy Media
Node.js Part 3 - Building a Static Page Server
Traversy Media
Node.js Part 4 - NPM
Traversy Media
Node.js Part 2 - Install MongoDB in Windows
Traversy Media
Create a Joomla Quickstart with Custom Sample Data
Traversy Media
Install MongoDB in Ubuntu
Traversy Media
HTML5 Web Storage
Traversy Media
Create a Joomla Bootstrap Template From Scratch
Traversy Media
Ubuntu Server 14.04 Setup Part 1 - Installation
Traversy Media
Ubuntu Server 14.04 Setup Part 3 - Set Static IP
Traversy Media
Create A Wordpress Widget - Part 1
Traversy Media
Create A Wordpress Widget - Part 2
Traversy Media
Create A Wordpress Widget - Part 3
Traversy Media
Create A Wordpress Widget - Part 4
Traversy Media
Get Started With Sass on Windows
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 1
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 6
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 4
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 5
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 3
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 2
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 7
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 10
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 8
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 11
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 9
Traversy Media
Build An Audio Player Using HTML5 & jQuery - Part 1
Traversy Media
Build An Audio Player Using HTML5 & jQuery - Part 2
Traversy Media
Youtube Data API v3 & jQuery To List Channel Videos
Traversy Media
Using Bootstrap With Ruby on Rails
Traversy Media
More on: Database Integration
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Career choice with the advent of AI - pure Computer Science or learn software with a background of core engineering area
Dev.to AI
The AI Hype Cycle: Calm Before the Next Breakthrough?
Medium · Programming
AI won’t replace scientists. It will make the current model of science obsolete
Medium · Data Science
The End of Knowledge: Why Artificial Intelligence Is Changing Not Only What We Know, but What It…
Medium · AI
🎓
Tutor Explanation
DeepCamp AI