Codeigniter 3 Model best practices |
It's a very first post here so...Hi guys
![]() Well I'm trying to achieve an OOP principes in Codeigniter, something like ORM but in simpler way. Usually I'm using a simple spl loader in MY_Loader, with my own classess, where properties look like in tables. But the problem are .... relations - espiecially many to many. For example - I have Database with User($email, $password, $id), Item($name $id) and relation table ($idUser, $idItem). And now I want to get items what are related to User by item->name. Database makes results by custom_results_object. Usually I'm just making a several queries to database, table by table, and operate results by standard php methods, to return an array of needed object. How it's yours idea? I think it's not a good solution to make a several queries, while I can use for example join method. But how to convert it to objects ?
You can convert the arrays to object like this:
PHP Code: $object = (object)$array; What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
The database library in CI will return query results as objects by default:
PHP Code: $this->db Now your model returns the records as an array of objects. If you prefer an array of arrays, then do this: PHP Code: return $query->result_array();
this isn't trivial at all, and i'm unsure about your level but i'll try ...
First of all - i strongly suggest the use of objects (respective classes) in any circumstance - because of its flexibility any kind of array which holds a set of data (multiple rows) - should be an object (one of the better approaches would be the use of ArrayObjects) lets take an example based on author and books (one author has many books) That said your structure should be: PHP Code: class Collection extends ArrayObject {} You'll see how structured your data are right now The main problem now is - how to integrate such a simple looking example into Codeigniter - to be honest i just can deliver some abstract basics, because i'm really busy right now The first thing i would recommend is to deal with the problem how and where to define your relations For this matter i strongly suggest one model for each corresponding table the second thing is - you should think about your type of relations (maybe take a look @laravels eloquent) But for now there are 2 relations important:
How would this look in our model ? A possible approach would be (since namespaces and CI are a special topic - i'll stick with the common practice to suffix the names of the respectivce classes. PHP Code: class Author_model extends ORM_Model The next thing would be to think about - whats the job of your ORM Model and which functions and processes should be there ... I for myself would integrate the query builder and use it for your own purpose it could look like (keep in mind this is just an abstract shot, in order to understand how to achieve a structured query building process) PHP Code: abstract class ORM_model the Relation builder is still missing - PHP Code: class Relations_Factory Now you have to think about your relation Structure and thats it. The end goal would be something like: PHP Code: $colAuthor = $this->Author_model->where('lasname', 'King')->read(); There is a topic in the CI4 Forum - for additional infos https://forum.codeigniter.com/thread-71733-page-2.html |
Welcome Guest, Not a member yet? Register Sign In |