So we are missing the implementation of that create static method that should convert this array of
data.
Now we expect this input array to be an associative array, where keys would be the names of the columns
and values, the obvious values of those.
And we're going to use the insert SQL statement.
That's why I'm just opening the docs for it in SQLite, as that's what we are currently using.
Now the first step is to get the database.
So let's get this out of the way.
And I've just extracted this method.
Let's call it this way into a separate video as well.
We're going to be doing a little bit of tricks to construct this SQL that's going to insert this into
the database.
So first we need to figure out what are the column names that we are trying to populate?
So with this insert statement we're going to go this way.
We're going to do insert into that's the table name.
And then we're going to list all the columns inside parentheses that need to be populated.
This would be followed by this values keyword.
And also inside parentheses we're going to be inserting the values.
So we just need to construct this dynamic string with SQL.
That really depends on what's inside the data, what columns are there etc..
So the first thing I'd like to do is get the names of columns inside data.
So data is an associative array with one single record.
So we can do that quite easily by using implode.
And in this case, we're gonna convert them to a string.
And then another PHP function called array keys to get all the keys from data.
So we're gonna get something like ID title, createdat content, whatever.
So that would be the result of running that code.
Okay.
So now we have the columns.
And this is what we're going to insert in this place inside parentheses.
Maybe let me start constructing this SQL so it makes more sense for you.
So this is insert into.
We concatenate with static table Then inside parentheses I'm gonna put the columns string.
Then goes a space, then values keyword.
And this is not ready yet so we need to.
Next work on those values that we need to get.
Let me call that placeholders because we.
Are not inserting the values directly into the SQL.
We just want to use those placeholders and prepared statements.
But anyway we need to generate a proper string with those question marks so that later on the database
can insert the actual data in place of question marks.
Now we just need to construct a string, and we just need to know how many of those question marks we
need.
Okay.
So let me start with this.
We're gonna do implode again because we need to separate all those items from an array using a comma.
As you see here in the syntax, this is just an expression.
Then goes a comma, another expression comma, etc. and now we need to construct the array with those
question marks.
So again we can use array fill.
And let's start with index zero.
How many question marks do we need.
Well the same amount as there are items inside the data array.
And what value do we want to put.
Well the question mark.
Now what this would produce is question mark.
Question mark comma question mark.
Question mark.
Now I can just put this inside this string.
So that's placeholders.
And this is our complete SQL.
So the last step is to just run it.
So I'm doing the database query passing the SQL.
Let me move up a little and I'm getting the array values from data.
So we're almost done because the result of this should be well we should actually fetch the record we've
just inserted.
So we're going to use a method we've already implemented.
This would be static find.
And from the database I'm going to get the last insert ID the ID of previously inserted record.
And then we are using find to return an object from this create method.
And that's everything.
This is how we can nicely insert a record that is in form of an array into the database, and then also
get back the object back from the database.