📜 ⬆️ ⬇️

Codeigniter - make your life easier (expanding the model)

In those cases when you need a full-fledged orm, I recommend using Doctrine, since it integrates well with Codeigniter.

But it happens that so many possibilities are not needed, and to prescribe a CRUD to each table is long and tedious.

The obvious solution is to expand the model.
So create the file MY_model.php (/system/application/libraries/MY_model.php)

<? php
class MY_model extends model {
private $ key_field = null ;
private $ table = null ;
private $ params = array ();
private $ item = array ();
')
public function MY_model ($ table = null ) {
parent :: model ();
if ($ table! = null ) {
$ this -> setFields ($ table);
}
}

public function setFields ($ table, $ key_field) {
$ fields = $ this -> db-> list_fields ($ table);
foreach ($ fields as $ field)
{
$ table_fields [] = $ field;
}
$ this -> params = $ table_fields;
$ this -> table = $ table;
$ this -> key_field = $ key_field;
}

public function getItem (array $ searchValues) {
foreach ($ searchValues as $ key => $ value ) {
if (! in_array ($ key, $ this -> params )) {
show_error ( 'wrong searchValues' );
die ();
}
}

$ query = $ this -> db-> get_where ($ this -> table, $ searchValues, 1);
if ($ query-> num_rows () <1) {
show_error ( 'can \' t find entry ' );
die ();
}
else if ($ query-> num_rows ()> 1) {
show_error ( 'found multiply entries' );
die ();
}
else {
foreach ($ query-> result_array () as $ row) {
$ this -> item = $ row;
}
}
}

public function editItem (array $ editValues) {
if ($ this -> item === array ()) {
show_error ( 'no item to edit' );
die ();
}
else {
foreach ($ editValues as $ key => $ value ) {
if (! in_array ($ key, $ this -> params )) {
show_error ( 'wrong editValues' );
die ();
}
}

foreach ($ editValues as $ key => $ value ) {
$ this -> item [$ key] = $ value ;
}
}

}

public function saveItem () {
if ($ this -> item === array ()) {
show_error ( 'no item to save' );
die ();
}

if ($ this -> key_field == null ) {
show_error ( 'no key_field defined' );
die ();
}

if (array_key_exists ($ this -> key_field, $ this -> item) && $ this -> item! = array ()) {

$ this -> db-> where ($ this -> key_field, $ this -> item [$ this -> key_field]);
$ this -> db-> update ($ this -> table, $ this -> item);
}
else {
$ this -> db-> insert ($ this -> table, $ this -> item);
}
}

public function deleteItem () {
if ($ this -> item === array ()) {
show_error ( 'no item to delete' );
die ();
}

if ($ this -> key_field == null ) {
show_error ( 'no key_field defined' );
die ();
}

if (array_key_exists ($ this -> key_field, $ this -> item) && $ this -> key_field! = null ) {
$ this -> db -> delete ($ this -> table, array ($ this -> key_field => $ this -> item [$ this -> key_field]));
}
else {
show_error ( 'no id set to delete an entry' );
die ();
}
}

public function newItem (array $ newValues) {

foreach ($ newValues as $ key => $ value ) {
if (! in_array ($ key, $ this -> params )) {
show_error ( 'wrong newValues' );
die ();
}
}
$ this -> item = $ newValues;

}

public function cleanItem () {
$ this -> item = array ();
}
}
* This source code was highlighted with Source Code Highlighter .


In the autoload.php file (/system/application/config/autoload.php)
write loading the necessary classes
<code>
$ autoload ['libraries'] = array ('database', 'model', 'MY_model');


Preparations are complete.
Now it would be necessary to somehow use this business.

Let's say we create a table where we are going to store e-mail addresses with the following structure:
CREATE TABLE `email_list` (
`id` int (11) NOT NULL auto_increment,
`email` varchar (255) NOT NULL ,
PRIMARY KEY (`id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8 AUTO_INCREMENT = 1; * This source code was highlighted with Source Code Highlighter .


create a model:
<? php
class Email_model extends MY_Model
{
function Email_model () {
parent :: MY_Model ();
}
}
* This source code was highlighted with Source Code Highlighter .

You don’t need to write anything more, you can immediately start using

Here is how the application will look like creating a new record in the controller:
<? php
class Email extends Controller {

function Email () {
parent :: Controller;
// Load the model for access in all controller methods
$ this -> load-> model ( 'Email_model' );
// table name and key
$ this -> Email_model-> setFields ( 'email_list' , 'id' );
}

function index ()
{
// create a new record
$ this -> Email_model-> newItem (array ( 'email' => 'someemail@somedomain.com' ));
// save the record
$ this -> Email_model-> saveItem ();
// clear the record
$ this -> Email_model-> cleanItem ();
// find record by field by email field value
$ this -> Email_model-> getItem (array ( 'email' => 'someemail@somedomain.com' ));
// edit it
$ this -> Email_model-> editItem (array ( 'email' => 'someotheremail@somedomain.com' ));
// save the record
$ this -> Email_model-> saveItem ();
// erase the record
$ this -> Email_model-> deleteItem ();
// clear the record
$ this -> Email_model-> cleanItem ();
} * This source code was highlighted with Source Code Highlighter .


That's all. The decision does not claim to be universal, but in some cases it can make life easier.

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


All Articles