Codeigniter App Tutorial
Lección 8 de 10 · Codeigniter App Part 8 – Models Task CRUD · TI y software
Codeigniter App Part 8 – Models Task CRUD
alright 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 task
ugh they're not going to have their own index view like like the list does we're not going to have 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 what we do want is a show function we want to be able to go to tasks / show and then the ID of that task and I went ahead and created just a couple tasks in PHP
myadmin alright 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 dot PHP
we want to test model and let's create a view folder for our tasks and for views we want a show view we want an ad task view and an edit task
view alright so you can close those for now the Edit in the ad now in the controller let's go ahead and say class tasks look Jimmy a capital extends CI controller
what and we want to have a public function show and that should take the ID which comes from the URL and we want to grab that task info so we'll create a data array value of will say this task
actually we'll just call that task which will be equal to this DB I'm sorry not this DV 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 using this all right so let's create the get task function in the model extends CI model okay so we want to get a task
now we're going to want to actually do a table join we want to get the taps and we also want to get the list so we'll have to join the tables and lists but them I mean that the tasks and list 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 DB get from tasks and we want to say this DB where
ID is equal to ID return query bro alright so now our task info should be available to us we need to link the view here so let me just paste that in
okay so this will be our tests show view test this out alright so we get a register of the task model with the autoloader and that's the last model that will be
creating alright so we're in the show view we can now grab this ID and all the other information I'm going to paste this view in basically we will have some actions
just like we did with the list and we'll show a list of the task so let's save that I'm sorry not a list of the task but the information that has to do with this particular task
alright so here's our information list name so this is something we're going to need later on so let's get rid of this which is just a link to them to the list just to get rid of this error for now
and then this one undefined is complete task is complete alright so my original version we had a model function to get the completion he is complete so we're going to do that
here well 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 where ID the task table we want to return is complete all right 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 to add a task all right so if we click here we should be able to add a task and if you look at this ad we're seeing add 1 this may look a little funny but
what this one is is it's the list ID and you can see that in the view we have tasks / add / list ID alright so that's why that's there because obviously if we had a task ID we wouldn't be adding a
task would be updating it so let's go ahead and create the add function and we can paste that in 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 ok that's really simple I'm 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 alright so we're just going to insert simple insert statement and we need to form let's go ahead and go to the task the add task view and the 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 getlist name
task model 15 public Oh got word function all right that's still not working let's see tasks add not sure why
hmm so the view we created the view which is add task dot PHP that I not save it alright I must not have saved it right
because now it's working alright so we have add task we have the list which is family stuff so let's see pick up son will say pick up son from baseball that's going to be tomorrow add task and
huh we 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 but I want to make it so we get a message so let's see this is setting this task created book this task created message so and that's in the show view all right
so it's going back to show lists / show list ID alright that's good so we want to go to the list show and I'm actually going to paste in a bunch of different messages so when a
task gets deleted created updated marked complete mock knew so we'll save that let's try to add another task to this family thing say dinner with wife now to eat with
life and that's going to be sunday night alright so there's our your task has been created now we're just not seeing them because we haven't added that to the list view yet you know what I want
to do is create the Edit task so up here we should be able to say tasks / edit and 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
alright so we got 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 book public function and we basically just want to grab the ID of the parent list okay so we're going to say this DB where and we want to match the ID to the task ID alright
and then we're going to create a variable called query and will say this DB get and we're going to grab this from the tasks list and then finally we just want to return a row turn query
ro and we want to grab just a list ID so we'll add this on alright so that's that the next one we have is get list name and we're passing in this 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 ID and we're just saying where the ID matches this list ID and we're going to get that
from the list table and ultimately it's just returning the list name all right 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
you can use whatever data that we want alright so let's save the model and then we're just going to load up the Edit task 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 tasks / edit and then the the actual ID and the seat this uri segment is getting the third segment of the URL which will
be the ID okay so this is the second segment this is the third alright 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 is to manually type it in up here so we'll say tasks / edit / 1 and that gives us the edit page for the task with the ID of one okay so that looks good too alright
so let's um let's try it out we'll change the title to parents dinner and we can change the date to the 28th update alright so task model edit task we haven't created that yet alright so
let's create that now this is going to be a really simple method we just passed 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 alright task has been updated so we can only check it out we can actually do
show to check it out you can say tasks / show / 1 okay ah that nope that's me too ah some reason that's not it's not showing right let's try edit
so we'll say task / edit / 1 / 2 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 alright so 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 to do that and we'll be using active record to do that so I'm going to stop this video and we'll continue that in the next one
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