<? 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 .
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 .
<? php
class Email_model extends MY_Model
{
function Email_model () {
parent :: MY_Model ();
}
}
* This source code was highlighted with Source Code Highlighter .
<? 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 .
Source: https://habr.com/ru/post/30535/
All Articles