Adding CRUD Operations5:17 with Alena Holligan
We're going to be adding the rest of our CRUD operations. Getting a single course, create a course, update a course and delete a course.
Before checking the solution in the next step, try to put together the Review Model with the following methods:
- Finally, instead of getting ALL reviews, you'll want to getReviewsByCourseId
If you would like to learn more details about CRUD operations, we have a whole course dedicated just to that! CRUD Operations with PHP
We'll go back into Course and we'll copy the getCourses. 0:00 This ensures that our formatting is the same throughout. 0:06 Instead of getCourses, we wanna say getCourse. 0:11 And this is going to accept a course_id. 0:14 Instead of the ORDER BY, we're going to use a WHERE. 0:19 Where id equals the named id. 0:24 We'll need a line to bind our parameter. 0:29 Statement bindParam and 0:34 our id is going to equal our course_id. 0:38 Since this is returning a single course we, can change fetchAll to just fetch. 0:46 We need to create three more methods, create, update, and delete. 0:56 You can pause this video and 1:03 give it a try yourself before I walk through it with you. 1:05 Okay, did you give it a try? 1:09 Let's see how I would do it. 1:11 Once again, I'm going to duplicate one of these methods. 1:13 This insures that the formatting is the same. 1:20 Instead of getCourse, I'm going to set this to createCourse. 1:23 For this method, I'm going to accept all the user entered data as an array. 1:30 Now I need to change my SQL statement. 1:37 INSERT INTO courses. 1:42 We'll need a title and a url, 1:46 and the values will be title and url. 1:50 Then I'll be binding two parameters. 1:59 The title which will be data, title, 2:05 And the url which will be data, url. 2:13 I want to return this newCourse so 2:20 I'm going to use a call to my getCourse method. 2:22 Return this getCourse, 2:32 and then I'm going to 2:37 pass this database and 2:42 the lastInsertId. 2:47 For update, I'm going to duplicate this createCourse. 2:51 I'll change it to update, and then I'll change my SQL statement. 3:00 UPDATE courses SET 3:09 title=: title, 3:14 url=:url WHERE id=:id. 3:18 And you'll notice I will need a third parameter to be bound. 3:25 This will be the id. 3:34 Course_id. 3:40 Finally, instead of using the lastInsertId, 3:41 I can use data course_id. 3:48 Our final action is to delete a course. 3:53 I'll duplicate my update course, 3:55 And rename it to delete. 4:02 This is going to accept a course_id. 4:09 We're going to DELETE FROM courses. 4:14 And then I just need to bind my course_id. 4:22 There's no course to return, so this time I'm just going to return a message, 4:25 letting the user know that the course was deleted. 4:30 Message, 4:36 The course was deleted. 4:45 Our review model is going to work very similar to our course model. 4:48 Try putting it together yourself, and I'll show you the code in the next step. 4:54 Remember, you'll need to get a review, 4:58 create a review, update a review, and delete a review. 5:02 Instead of getting all reviews, you'll want to get review by course ID. 5:08 Good luck, give it a try now. 5:15
You need to sign up for Treehouse in order to download course files.Sign up