📜 ⬆️ ⬇️

HyperActive Record - under-ORM on CodeIgniter


general description


HyperActive Record is an add-on for the DB class for the CodeIgniter 2.0 and higher frameworks that allows you to get the field values ​​of objects associated with the recording using foreign keys (without foreign keys) without additional description of links and data. Such under-ORM
On githaba
Library page

Why do you need it?



Here is the structure of the project table, for example. Here you can see that there are connections with the project type, client and manager.
In the normal case, when retrieving data using ActiveRecord, it would look like this:
In the model:
$this->db->select(); $this->db->from($this->table); $query = $this->db->get(); return $query->result(); 

As a result:
 array(1) { [0]=> object(stdClass)#19 (9) { ["id"]=> string(1) "2" ["name"]=> string(21) " " ["date_start"]=> string(19) "2012-07-19 00:00:00" ["date_end"]=> string(19) "2012-07-31 00:00:00" ["description"]=> string(16) "" ["project_types_id"]=> string(1) "1" ["clients_id"]=> string(1) "1" ["sum"]=> string(5) "12333" ["manager_id"]=> string(1) "1" } } 

Instead of getting the client's name, project type, manager information, we get their id. Accordingly, to get the values, one must either manually append queries, or write join in AR. And that, and the other does not give enlightenment and happiness, because have to do constantly and routinely.
Yes, of course, you can take a ready-made ORM, tinker with screwing it to CodeIgniter (or you have it in the delivery, if you use another framework), write a description of the classes of the model and the links ... Stop. Write again.
Because I am extremely lazy and do not like to do the same thing all the time, on one of the projects I decided to find some kind of library that would allow me to write a simple get and, at the same time, draw objects related to the record without my intervention. I did not find it, that’s HyperActive Record.
For the idea and the way to extend the standard DB driver, thanks to Simon Emms

Library features:


  1. retrieving record-related objects with the values ​​of all their fields by Foreign keys.
  2. one-to-many, many-to-one, many-to-many communications support
  3. Retrieving related objects only by the fields specified in the request, without unnecessary data
  4. unlimited (so far) number of nesting levels (if the project has an author, the author has a position, then the resulting object will contain both projects and authors attached to them with the value of their posts)

Limitations:


  1. MySQL only
  2. in MySQL, only InnoDB, MyISAM does not support foreign keys
  3. in contrast to the “adult” ORM, it is impossible to call model methods on the objects obtained, etc., this is just data


ToDo



Documentation



')

Installation


Download and unpack the library, put the files in the core folder of the application (in the archive, the path leads as application / core , but your application may be called differently)

Anything, no longer need any gestures to use.


Using


Making the usual design Active Record
  $this->db->select(); $this->db->from($this->table); $query = $this->db->get(); return $query->result(true,$this->table); //      return $query->row(true,$this->table); 

For backward compatibility, if there are no parameters for row () or result () , the parent function of the MySQL driver is called. $ this-> table is the base table from which to get the records
The names of the fields of the result object containing the associated records are taken from the names of the tables containing the related records.
The result of the query (for example, the same table as above):
 array(1) { [0]=> object(stdClass)#19 (14) { ["id"]=> string(1) "2" ["name"]=> string(21) " " ["date_start"]=> string(19) "2012-07-19 00:00:00" ["date_end"]=> string(19) "2012-07-31 00:00:00" ["description"]=> string(16) "" ["project_types_id"]=> string(1) "1" ["clients_id"]=> string(1) "1" ["sum"]=> string(5) "12333" ["manager_id"]=> string(1) "1" ["project_types"]=> object(stdClass)#23 (2) { ["id"]=> string(1) "1" ["name"]=> string(24) "-" } ["clients"]=> object(stdClass)#22 (2) { ["id"]=> string(1) "1" ["name"]=> string(12) " " } ["users"]=> object(stdClass)#20 (7) { ["id"]=> string(1) "1" ["login"]=> string(11) "megalogin" ["password"]=> string(32) "202cb962ac59075b964b07152d234b70" ["name"]=> string(12) "John" ["surname"]=> string(18) "Doe" ["roles_id"]=> string(1) "2" ["roles"]=> object(stdClass)#28 (2) { ["id"]=> string(1) "2" ["name"]=> string(16) "" } } ["project_files"]=> array(0) { } ["projects_milestones"]=> array(1) { [0]=> object(stdClass)#25 (7) { ["id"]=> string(1) "1" ["name"]=> string(10) "" ["description"]=> string(0) "" ["count_files_needed"]=> string(1) "0" ["checklist_has_urls"]=> string(1) "0" ["final_file_needed"]=> string(1) "0" ["projects_id"]=> string(1) "2" } } } } 

For convenience, and again reducing the amount of code, it is proposed to create a basic model with the functions get_list () and get () and in the inherited models just to set $ this-> table in the constructor

Source: https://habr.com/ru/post/148462/


All Articles