しかし、多くの機能は必要ないため、各テーブルにCRUDを登録するのは長くて面倒です。
明らかな解決策は、モデルを拡張することです。
ファイルMY_model.php (/system/application/libraries/MY_model.php)を作成します
<? php
クラス MY_modelはモデルを拡張します{
private $ key_field = null ;
private $ table = null ;
private $ params = array();
private $ item = array();
パブリック関数MY_model($ table = null ){
親::モデル();
if ($ table!= null ){
$ this-> setFields($ table);
}
}
パブリック関数setFields($テーブル、$ key_field){
$ fields = $ this-> db-> list_fields($ table);
foreach ($フィールドを $フィールドとして )
{
$ table_fields [] = $フィールド;
}
$ this-> params = $ table_fields;
$ this-> table = $ table;
$ this-> key_field = $ key_field;
}
パブリック関数getItem(array $ searchValues){
foreach ($ searchValues as $ key => $ value ){
if (!in_array($ key、$ this-> params )){
show_error( 'wrong searchValues' );
死ぬ();
}
}
$ query = $ this-> db-> get_where($ this-> table、$ searchValues、1);
if ($ query-> num_rows()<1){
show_error( '\\ tエントリが見つかりません' );
死ぬ();
}
else if ($ query-> num_rows()> 1){
show_error( 'found multiple entries' );
死ぬ();
}
その他 {
foreach ($ query-> result_array() as $ row){
$ this-> item = $ row;
}
}
}
パブリック関数editItem(array $ editValues){
if ($ this-> item === array()){
show_error( '編集するアイテムがありません' );
死ぬ();
}
その他 {
foreach ($ key => $ value としての $ editValues){
if (!in_array($ key、$ this-> params )){
show_error( 'wrong editValues' );
死ぬ();
}
}
foreach ($ key => $ value としての $ editValues){
$ this-> item [$ key] = $ value ;
}
}
}
パブリック関数saveItem(){
if ($ this-> item === array()){
show_error( '保存するアイテムがありません' );
死ぬ();
}
if ($ this-> key_field == null ){
show_error( 'key_field defined' );
死ぬ();
}
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);
}
その他 {
$ this-> db-> insert($ this-> table、$ this-> item);
}
}
パブリック関数deleteItem(){
if ($ this-> item === array()){
show_error( '削除するアイテムがありません' );
死ぬ();
}
if ($ this-> key_field == null ){
show_error( 'key_field defined' );
死ぬ();
}
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]);
}
その他 {
show_error( 'エントリを削除するためのIDが設定されていません' );
死ぬ();
}
}
パブリック関数newItem(array $ newValues){
foreach ($ newValues as $ key => $ value ){
if (!in_array($ key、$ this-> params )){
show_error( 'wrong newValues' );
死ぬ();
}
}
$ this-> item = $ newValues;
}
パブリック関数cleanItem(){
$ this-> item = array();
}
}
*このソースコードは、 ソースコードハイライターで強調表示されました。
autoload.phpファイル(/system/application/config/autoload.php)
必要なクラスの書き込みロード
<コード>
$ autoload ['libraries'] = array( 'database'、 'model'、 'MY_model');
準備が完了しました。
今、このビジネスを何らかの形で使用する必要があります。
次の構造を持つ電子メールアドレスを格納するテーブルを作成するとします。
CREATE TABLE `email_list`(
`id` int (11) NOT NULL auto_increment、
`email` varchar (255) NOT NULL 、
主 キー ( `id`)
)エンジン= InnoDB DEFAULT CHARSET = utf8 AUTO_INCREMENT = 1; *このソースコードは、 ソースコードハイライターで強調表示されました。
モデルを作成します。
<? php
クラス Email_modelはMY_Modelを拡張します
{
関数Email_model(){
親:: MY_Model();
}
}
*このソースコードは、 ソースコードハイライターで強調表示されました。
これ以上書く必要はありません。すぐに使用を開始できます
コントローラで新しいレコードを作成するようにアプリケーションがどのように見えるかは次のとおりです。
<? php
クラス Email extends Controller {
関数Email(){
親::コントローラー;
//すべてのコントローラーメソッドでアクセスするためにモデルをロードします
$ this- > load- > model( 'Email_model' );
//テーブル名とキー
$ this-> Email_model-> setFields( 'email_list' 、 'id' );
}
関数インデックス()
{
//新しいレコードを作成します
$ this-> Email_model-> newItem(array( 'email' => 'someemail@somedomain.com' ));
//レコードを保存します
$ this-> Email_model-> saveItem();
//レコードをクリアします
$ this-> Email_model-> cleanItem();
//電子メールのフィールド値でフィールドごとにレコードを検索します
$ this-> Email_model-> getItem(array( 'email' => 'someemail@somedomain.com' ));
//編集します
$ this-> Email_model-> editItem(array( 'email' => 'someotheremail@somedomain.com' ));
//レコードを保存します
$ this-> Email_model-> saveItem();
//レコードを削除します
$ this-> Email_model-> deleteItem();
//レコードをクリアします
$ this-> Email_model-> cleanItem();
} *このソースコードは、 ソースコードハイライターで強調表示されています。
それだけです。 解決策は普遍的であると主張していませんが、場合によっては人生を楽にすることができます