WEBVTT

00:00.770 --> 00:08.660
So we are missing the implementation of that create static method that should convert this array of

00:08.660 --> 00:09.410
data.

00:09.440 --> 00:16.520
Now we expect this input array to be an associative array, where keys would be the names of the columns

00:16.520 --> 00:19.640
and values, the obvious values of those.

00:21.380 --> 00:24.710
And we're going to use the insert SQL statement.

00:24.740 --> 00:32.120
That's why I'm just opening the docs for it in SQLite, as that's what we are currently using.

00:32.150 --> 00:35.030
Now the first step is to get the database.

00:35.450 --> 00:37.880
So let's get this out of the way.

00:38.660 --> 00:43.610
And I've just extracted this method.

00:43.640 --> 00:48.470
Let's call it this way into a separate video as well.

00:48.500 --> 00:57.110
We're going to be doing a little bit of tricks to construct this SQL that's going to insert this into

00:57.260 --> 00:58.580
the database.

00:58.580 --> 01:06.360
So first we need to figure out what are the column names that we are trying to populate?

01:06.510 --> 01:12.360
So with this insert statement we're going to go this way.

01:12.360 --> 01:16.800
We're going to do insert into that's the table name.

01:16.800 --> 01:24.510
And then we're going to list all the columns inside parentheses that need to be populated.

01:24.540 --> 01:27.750
This would be followed by this values keyword.

01:27.750 --> 01:32.490
And also inside parentheses we're going to be inserting the values.

01:32.520 --> 01:39.330
So we just need to construct this dynamic string with SQL.

01:39.420 --> 01:45.540
That really depends on what's inside the data, what columns are there etc..

01:45.630 --> 01:57.150
So the first thing I'd like to do is get the names of columns inside data.

01:57.630 --> 02:02.070
So data is an associative array with one single record.

02:02.070 --> 02:06.800
So we can do that quite easily by using implode.

02:07.490 --> 02:14.420
And in this case, we're gonna convert them to a string.

02:16.130 --> 02:23.960
And then another PHP function called array keys to get all the keys from data.

02:26.660 --> 02:37.220
So we're gonna get something like ID title, createdat content, whatever.

02:37.220 --> 02:40.730
So that would be the result of running that code.

02:40.760 --> 02:41.330
Okay.

02:41.330 --> 02:43.340
So now we have the columns.

02:43.340 --> 02:48.410
And this is what we're going to insert in this place inside parentheses.

02:48.440 --> 02:54.500
Maybe let me start constructing this SQL so it makes more sense for you.

02:54.530 --> 02:57.950
So this is insert into.

02:59.690 --> 03:11.070
We concatenate with static table Then inside parentheses I'm gonna put the columns string.

03:11.550 --> 03:15.390
Then goes a space, then values keyword.

03:16.020 --> 03:20.430
And this is not ready yet so we need to.

03:20.460 --> 03:24.510
Next work on those values that we need to get.

03:24.540 --> 03:27.270
Let me call that placeholders because we.

03:27.300 --> 03:31.620
Are not inserting the values directly into the SQL.

03:32.130 --> 03:36.990
We just want to use those placeholders and prepared statements.

03:36.990 --> 03:45.780
But anyway we need to generate a proper string with those question marks so that later on the database

03:45.780 --> 03:50.310
can insert the actual data in place of question marks.

03:50.430 --> 04:00.000
Now we just need to construct a string, and we just need to know how many of those question marks we

04:00.000 --> 04:00.420
need.

04:00.450 --> 04:01.110
Okay.

04:01.110 --> 04:03.000
So let me start with this.

04:03.030 --> 04:11.080
We're gonna do implode again because we need to separate all those items from an array using a comma.

04:11.110 --> 04:15.700
As you see here in the syntax, this is just an expression.

04:15.700 --> 04:25.750
Then goes a comma, another expression comma, etc. and now we need to construct the array with those

04:25.750 --> 04:27.130
question marks.

04:27.790 --> 04:31.600
So again we can use array fill.

04:32.860 --> 04:36.220
And let's start with index zero.

04:36.280 --> 04:38.320
How many question marks do we need.

04:38.350 --> 04:43.300
Well the same amount as there are items inside the data array.

04:43.630 --> 04:46.900
And what value do we want to put.

04:46.930 --> 04:49.900
Well the question mark.

04:50.080 --> 04:54.160
Now what this would produce is question mark.

04:54.640 --> 04:56.650
Question mark comma question mark.

04:56.650 --> 04:57.580
Question mark.

04:57.610 --> 05:00.790
Now I can just put this inside this string.

05:00.790 --> 05:03.160
So that's placeholders.

05:03.160 --> 05:05.500
And this is our complete SQL.

05:05.500 --> 05:07.960
So the last step is to just run it.

05:07.960 --> 05:14.190
So I'm doing the database query passing the SQL.

05:14.610 --> 05:23.130
Let me move up a little and I'm getting the array values from data.

05:23.130 --> 05:30.510
So we're almost done because the result of this should be well we should actually fetch the record we've

05:30.510 --> 05:31.620
just inserted.

05:31.620 --> 05:35.370
So we're going to use a method we've already implemented.

05:35.370 --> 05:37.740
This would be static find.

05:37.740 --> 05:48.090
And from the database I'm going to get the last insert ID the ID of previously inserted record.

05:48.090 --> 05:54.060
And then we are using find to return an object from this create method.

05:54.060 --> 05:56.910
And that's everything.

05:56.910 --> 06:06.090
This is how we can nicely insert a record that is in form of an array into the database, and then also

06:06.120 --> 06:09.600
get back the object back from the database.
