PHP MVC Framework Database Access Layer EntityManager Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] back again back again youtube and welcome back to the cobian workshop and we're going to just picked up from where we left off um in the last video we were doing our query builder but before we do that i want to actually jump back to the query builder factory and do a little bit of refactoring because as you can see at the bottom here we're sort of like returning new query builder but actually what i want to do is is just return um that variable query builder object and that should be the end of that so that's to be a refactoring that i wanted to do and i wanted to show you guys so i'm not doing it off-screen then you see something different on git so whatever changes that needs to be made i'm going to do it on the screen so you can see it so you can follow along so i'm going to close that out and the next bit of our orm that i want to tackle is the entity part of our orm and the responsibility of this section is to this is going to be the component that directly directly um that's the word this is the part of the com this is the part of our orm that's gonna directly interact with our database like it's going to do all the crud operation perform all the crude operation so i'm going to create a new folder i'm going to call it entity manager and within that i'm going to create you guessed it entity manager interface dot php and i'm going to create a entity manager class just.php and this also got a factory entity manager factory.php and let's first implement the interface because this is probably going to be one of the easier class to implement because it's only got one method i'll say php declare strict types name space say magma say liquid orm and it's going to be entity manager i'll say interface entity manager interface and the method that we're going to put in is going to be public public function and it's going to be called get crude that is it and actually we'll just set the return type and it's going to be an object i believe it's an object so let's get to our main class start implementing that declare strict types namespace magma it's gonna be liquid orm and it's going to be entity manager and say class entity manager implements entity manager interface right so i need to put a constructor within this within this class and i'll say main constructor class return void say public function construct and i'm going to pass in the crude interface is that a good name crit interface could name it something better um let's go with crude interface for now it's a class that we've not created yet and we're going to assign the variable or the argument to that sorry we're going to type int could interface to that crude variable and we need to set a protected protected crude interface crude interface to that variable which is just going to say var current interface and that's something we need to bring in and that's going to be located within this entity directory so i'm going to say magma um liquid orm entity manager and it's going to be a crude interface now again we've not created this class to class yet so we can actually go and create it so it's going to be crude.php and we've got crude interface dot php i'm going to come and list all the method that we want within this interface in a minute so back to our manager we want to pipe this property to this constructor argument so this could equal crude and because this is a protected method we need to return it in a meth we need to return that property within a methodology so we can access it outside of this class so i'm going to say public public function and it's going to be get crud it's going to return this crude again it says an object because we're returning the code object and that's as easy as it gets that is as much code are we going to put in our entitymanager class i'm just gonna inherit the metadoc from the interface when we do come to put that in we've not written anything yet but we will put some description in and close that out this is a very skinny skinny class there's only one method and a constructor in this in this class so the next thing we need to look at is we need to start putting in all the method that we require in our interface class so i'm going to start implementing those so we're going to say php declare again strict types equal one namespace magma again capital m magma liquid orm and we return the entity manager and yes i'm going to say interface interface in this crude interface now there's a couple of method that's not exactly could related but it's better served within this interface and within this class you see what i'm saying when we start implementing it so the first one is going to be a public function and it's going to be called get schema and it's going to return a string the second one is going to be public and it's going to be get schema id that's also going to return a string so let me explain what these two methods are so when i say get schema what i'm referring to is get the database table name the name of a database table and then get schema id is get the name of the prime get the primary key column of that of that database table so that primary key could be whatever is it that we define when we create our database table we want to get that key we want to get that key within this method and you'll see how that works in a bit next method that we want is public function and we want last id and this is the last return id of our insert query statement and again we want it to be an integer so that's the free method that's not exactly crude related but we need these method in order to sort of like um insert create stuff in the correct table so our first crude method is going to be create right and this is going to be return a bold because create is going to return true our folds it will either create successfully or it fails so it's going to return a ball and this is going to take one argument which is an array and i'll call it fields i'm going to set it to a empty array now the third argument not the third argument the second crude argument that we got is the r in crud which is going to be the read the read method and this is going to return an array this is going to return an array and this accepts multiple arguments so the first one i'm going to set is an array it's going to be the selectors and to set that to an empty array because it's optional the second one is also an array that's going to be the conditions that's also optional the third argument is also an array and that's going to be the parameters for query string that is also optional and the fourth argument we're going to put in is also an array and that is gonna be called optional that's optional and i think the fifth no i think that's it i think requires four arguments and i think that's it so we can actually break these down on separate lines that's a php coding standard as well this is allowed so we are allowed to do stuff like this but again i don't think we need to in this particular um interface class because it's not we can see we can see all of it so let's carry on so next method that we want to create is the u so we've got create read so we'll say update and that's going to be a bowl as well just like the create method is going to either return true or false with update that's the same we either update successfully or we fail and this method is going to accept two arguments and the first is going to be the array and it's going to contain an associative array of of key valid period that we want to update um and set that to optional and the second argument is going to be a string this is going to be the primary key which we want to update by right the next one in our crud is our delete method and just like what we've said when we're doing the query builder i want to delete via an array rather than specify an id i want to delete it by an array so i could delete by whatever field that i'll desire so it's going to be an array that's going to be conditions that's going to be optional now that's all you need for your crude operation but i'm going to go a couple of steps further and i'm going to add an s onto my code and i'll call it cruds and what the s is going to take care of is our search functionality and when i say public function i'm going to say search and that's going to return an array of search results and the arguments are going to be an array and the first one's going to be selectors so we can return an array of selected columns or fields or columns that's going to be optional and the second argument is an array and that's going to be the search conditions and that's also going to be optional and the next one is going to be public function which is not part of the current which is just the the rock the raw query bit that we've implemented within our query builder we're going to implement the method that can help us output custom query i'm going to call this raw query and i'm not going to set a return type on this just yet because i'm still not unsure of i'm going to actually are the best way to implement this i'm going to leave it like that for now but i know i'm going to need the argument which is going to be a string for the first argument is going to be the actual raw query yeah and the second argument is going to be an array of conditions for that query and i'm going to set that to optional and i think that's all the bits that we need in this particular interface for the time being we just now want to go ahead and implement those code for these implement this method so back into our crude class we're going to start build that out let's say php declare strict types equal one or equal true and namespace let's say magma yes i got it right first time magma um liquid orm and i'm gonna say entity manager and that's it so class crud implements that crude interface and we're going to have a constructor within this method which in this class so but before that i want to go ahead and create some properties that we are going to require and the first is going to be well they're all going to be protected it's going to be protected and it's going to be our data mapper object right i'm going to say data mapper because in order to sort of like use or or use our curve function we need that that prepare statement that we've got in our data map so we'll need that to be part of this this class or need that to be something that we can access within this class and the next one is going to be protected and that's going to be our query builder you see we type int in the class onto the variable right and the next one is going to be a protected string that's going to be our table name the name of our table protected string name actually it's not table name we call it getschema so we're gonna go with the same name naming convention so we're gonna call it table schema that's a better name and string and call is table schema id and i'm not going to initialize any of these properties i'm just going to bring them down into or construct and pipe them to the constructor arguments so let's say public function construct and we're going to say data mapper again we type intin data mapper and you say query builder query builder wrong to be a variable query builder we've got a string we've got table schema and a string table schema id then we can pipe them now so so this data mapper equal date mapper so this query builder equal query builder this table schema equal table schema you can see this table scheme id equal table schema id so because these are protected method we need to return them so we can use them outside of this class so i'm going to say public function i'm going to say get schema and it's going to be a string i'm going to say return this table schema right and the second one is going to be public this will be a similar public get schema id that's going to be a string as well i'm going to say return this table schema id and that's as simple as it is now for the third one that we had in our interface which is this method we're going to say public function last id and that's going to return an int correct that syntax error are we going to say return this data mapper i'm going to say get class id and remember this method that we've created in our data mapper class which is get lost id so we we're sort of like getting this method between our code using this property this data map up property right so let's start implementing some of our crude method say public function and the first one is create and that return ball accepts one argument which is an array and it's called fields and it's optional right so i'm going to wrap all of my crib method in a try catch block to catch any exceptions or any error the first thing i'm going to define is a associative array i'm going to say args i'm going to store it within this variable and i'm going to say table say this get schema which is this method right here which returns a table name right then we're going to define the type and this is going to be our insert type because create we're inserting and the fields is going to be fields and that's the variable that's the argument we're passing into this key or the value for this fields this field key is this appeals variable if that makes sense what it should do um why are we getting an error esl args all right there we go that's gone wasn't an error so we're going to create our next variable and call it query i'm going to say is equal to this query builder and i'm gonna say build query and what's in that build query method we're gonna pass that args in the args variable in and on this build query remember what we said in our query builder this is a chaining method so we're going to chain on our insert query onto that method if we take it back to the query builder you see we've got our build query method which is what we're calling right here through this property because we've now got access to that query builder object once we pass that in to our constructor we've we didn't get access to all the methods within that class so we're referencing this build query method we're passing in our argument right here this is our argument and we we're rewriting some of the keys within that default list that we've got so we've got table which is empty and we're setting it to get schema which is our database table which are a database table and we're setting the type which is insert right and the fields we're setting the fields as the argument that's coming into this method which is the fields so all of our iteration that we've been doing in our create method this is the field so we're getting all the arguments from that variable i'm hoping that's starting to make sense right so where are we so we've got query this query builder build query pass that argument in and call it insert query on that method right so we're going to say this data mapper we're going to call a persist method which we have not created yet and this method is going to take a couple of arguments it's going to take that actual query that we've just built right here and we're going to say this data map again because this method is within this data mapper class and it's going to be called build query parameters and we're going to pass that field argument in i'm going to say if this data mapper um nom rose is equal to one meaning if we've got at least one record that's been inserted then we're going to return true because that was successful and we're going to use a throwable for now we might changes depending on the behavior that it outputs and we're going to say throw that throwable right so you can see now some of our we're sort of like mapping methods from this class our data mapper contains this num roll which return the number of affected rows so we're saying if if at least one row was affected then our insert was was successful so i'm going to return true all right so there's two method here that we have not yet created which we need to create and what we want to do actually we want to actually go and create this method so we need to go back to our data mapper class data mapper and where can we put these we can put these we can put these at the bottom if we want so i'm going to say public function i'm going to say build query parameters as i'm saying this method see what it returns this method accepts two arguments we only define one in our in our crit class but it accepts two arguments and the first is an array which is the conditions conditions and the second is also an array which is the parameters and what we're going to do with this is we want to take you back here because this method will be used in different crude method we want to actually sort of like merge any conditions and additional parameters together so we're going to delegate that to this method in this class and we're going to say return it's going to return it and say if if isn't empty parameters or is an empty you should say open parenthesis in empty conditions then we want to merge both conditions and parameters together so if they're not empty we want to merge them together else just return the parameters all right so if parameters and conditions are not empty if they're not empty we're going to merge them together of one big array and if it's empty if one of them is empty we're just going to return the parameters oh that makes sense and we call that method right here and we only specifying the one argument which means it's only going to return the parameters again i can maybe mess around with defeat with the variable names but again we can do that when we come to refactor and the second method that we have not created is this persist method so if we go back to our data mapper class we can actually create that and it's a simple method and say public function persist and as we've said over here it takes in two arguments the first is a string that's going to be the query that we're passing in and the second is an array and that's going to be the parameters right and we're going to say we're going to wrap this into our try catch block as well all right i'm just gonna do a throwable let's put this back up here again we're gonna mess around with these exception as we go along we'll change and change out and put in the one that suits best and it's not really an exception it's referable i'm just going to throw that out and in this method i'm just going to return this prepared which is our prepared statement that we define at the very top let me get that which is this prepared method that we defined up here we're going to pass that query in from our persist as a well from our purchase method that argument which is the sql query which is coming from our crude class we're passing that in that actual query that we'll build we're passing that in to our prepare statement and we're going to chain on the bind parameters method on that bind parameters method we're going to pass in the parameters which should really be the fields because that's what we're passing in and again remember what i said that buying parameters is a chainable method because we're returning this we're returning this so it's chainable so passing the parameters and the parameters is what get passed to this bind values which is this method up here which bind or name parameters to whatever values we we've set those to so we're going to chain the execute method onto this because once we've prepared a query statement we bind all the name parameters to the values then we want to execute right and that is it for persist method for our persist method so we return this prepared we're passing in that s that sql query we're binding the parameters then we're executing the the query to get us the result but we won't do anything like fetch result nor anything here because we could fetch or we could fetch all or we could fetch column we could do anything so we don't want to do that a bit right here we can do that bit somewhere else so back to our code so define a try catch block we've override some of our query builders um we've override some of our sql default array we've override the take the type and the table and the fields right i'm trying to make you understand what i'm doing so we've override that by setting by creating this associative array we then pass that array into our build query method and then call it insert query on that method right then we get our data mapper and we persist our query to our prepared statement or we pass a query in as an argument which then gets filters down to this prepared method prepare the sql or the sql query then bind the parameters then execute the code and we're saying if this data mapper of at least one affected row then our insertion was successful so we're going to return true i really hope that makes sense because it doesn't get any simpler than that it's very simple in what it's doing and all the work that we've been creating to help build this class this is some of the implementation of all the hard work that we've been doing so let's crack on this public function read and i said this is going to return an array and it's got a body and we're going to specify the arguments i think there are four arguments and they're all an array but let's make things a little bit easier let's just copy these out it's not cheating this this is acceptable paste them in and again try catch block i'm just gonna put in our throwable i'm just going to throw that out all right so we've taken care of that bottom bit again the implementation is going to be very similar to what we've done right here so we're going to define a variable called args i'm going to override some of the key value or overwriting of the key within our sql default which is located within our query builder class so i'm going to say table i'm going to say this get schema which is a method i'm going to define the type as this is a select query and we're going to define the selectors to the first argument that we've got in this method and what else have we got in this we're going to get the conditions we're going to pipe it to the conditions argument um well that's how we got we got a params which is what i was forgetting in you know query builder class so i need to go in there and refactor that a bit i'm going to pass in all parameters in our which is our third argument within that method and we've got extras which is going to be our optional we're going to make use of this optional when we come to do our repositories you're going to see what we're doing with that and again it's i need to break it down a bit because it's gone off screen somewhat and let's leave it for now let's crack on i'm gonna say query i'm gonna say it called this query builder build query password variable in i'm going to call the select query method on that one i'm going to say this that data mapper and we're going to persist and pass that query in our query string in and what we did we did just data mapper so i'm going to pass that query in i'm going to say this data mapper i'm going to say build query parameters but only this time we're going to make use of both arguments for this for this method so we're going to pass in our conditions and our parameters and what's going to happen is as we've discussed if we've got conditions and parameters we're going to merge them together which is what we're doing right here and in this method we've passed in both conditions and parameters so they're going to get merged together as one big fat array that's going to be mapped to each name property before it gets execute so again when i say if this data mapper i'm going to call the nom roll method numerals and say greater than zero then we want to return this data mapper and we want to return an array of results right so we're saying if we've got at least if we've got more than one um if we've got a result you know more than zero zero will indicate no result but if we've got greater than zero it means that we've got some results so we want to return them as an associative array which is what this is doing data mapper results and again this is something that we build in our data mapper class which is this method right here is what we're calling results which is going to fetch all the data and that is it for our read method and the third method is going to be um where are we our update and it's going to be update and that's going to be a ball and as we said we can just quickly get the arguments and paste them in again that's allowed and again we're going to go with our try catch block getting really late now it's off at two in the morning now i think i'm gonna wrap up after this method and we'll pick up again in the morning so try catch and again we're going to do all four i'm just going to throw that out and again implementations are the same it's consistent is what we after what is what we're after consistency table i'm going to do this get schema i'm going to say the type is going to be update and we want to pass in the fields fields and we also want to pass in the primary key say primary key i'm going to pass it in primary key all right i'm going to say query is equal to this query builder so build query and it's going to accept that argument i want to fetch the update query on that method all right so i'm going to say this data mapper we're going to persist you can see that this persist method is going to be in all of our code operation i'm going to pass our query in and we're going to the second argument which is going to be data mapper and it's going to be build query parameters i want to pass that fields in right i'll say if if data mapper let's say this actually this data mapper num rolls is equal to one then return true again similar to or create method if at least one row was affected then we can say database was updated successfully right and let's quickly just create this delete method and that's going to return a ball as well is that what we're set for let's just check this um that's going to return a ball as well because again true if you've been update if you've been deleted and false if it was if it failed that it's going to return a boolean and the argument is just conditions and was that optional conditions was optional yet so i'm going to say empty array again try catch block and do a throwable just gonna throw that probable and again to define or array we're going to set our table and say this get schema and say type is delete and we've got some conditions which we're going to pass in conditions i'm going to say this our query you call this query builder and say build query args i'm going to call up delete query method and say this a lot of this this is what object orientated programming is all about a lot of this stuff this self so yeah this mapper persists and passing that query going to say this data mapper i'm going to say build little query parameters and we've only got the conditions to pass in right i'm going to say if this data mapper num rolls was affected which is equal one equal equal one then we're going to return true all right so if at least one row was affected we're going to return true because it was successfully deleted right and again i should have logged up but i'm going to do one last method that we've got and it's going to be a public function search and that's going to return an array and this contains a couple of arguments let's copy that out copy paste and again it's so tedious we could just copy this block of code and make the changes so just going to change up type to search which i don't think we add in our query builder so we can say search make that part of our query types and what condition has this got it's got conditions and it's got selectors that's the next one selectors and i'm going to say selectors comma and we just need the search query which i don't even think we've got you know query builder now we've not even got the search query but again it is something that we're going to implement because we want our application to of search functionality it's it's pretty important for application to other features like that so ours are not going to be no exception we want to up that so it's going to be a string and i'm going to implement it later but let's also put that within our interface let's just copy that i'm just going to call it search i might as well just rewrite it myself cut that must be tired because now it's starting to slur my typing even worse now so it's going to be search query and that's going to be that for that close it out query builder we're going to implement this tomorrow i'm going to close that out um close that out i'm going to close it out we're going to come back to that in a bit as well not tonight but in a bit and now we got the result that we want we need to say if nominal is greater than zero if we've got if we've at least found one search result we're going to again return this data mapper results right return an array of search results and i think that's going to be it for for tonight we're going to pick up back again tomorrow and crack on with the completion of or liquid orm so we can then move on to different parts of our framework because as i've said we've got quite a lot to do so again guys thanks for watching i hope you're getting some some value from from what i'm doing and please like share and subscribe to the channel i'm gonna leave links down in the description so you can jump onto github and clone the clone the project download it have a look for the source code leave comments tell me what you like what you don't like and so we can make all the changes together so that's gonna be for tonight i'm gonna pick up again tomorrow so stay tuned be safe god bless good night
Info
Channel: The Coding Workshop
Views: 291
Rating: undefined out of 5
Keywords: database access layer, html, developer, CMS, web developer, learning, training, php web development, ORM, MVC, design pattern, composer, how to, Github, version control system, php tutorial, programming for beginners, coding for beginners, programming tutorial, software engineer day in life, web developer career, web development, how to become a web developer, javascript, css, web dev, web design, software developer, learn about, Web architect, Object relational mapping in php, orm
Id: 8D83Lwez1-8
Channel Id: undefined
Length: 54min 32sec (3272 seconds)
Published: Tue Nov 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.