本文介绍了在Codeigniter中编写模型代码的最好方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于在codeigniter中使用代码模型的技术,我非常困惑。



我搜索了很多,并发现各种编码器使用的各种方法。请引导我使用codeigniter编写模型类的最佳方法。



几个例子;




  •   function insert_data($ name,$ address,$ email,$ ....,$ ...,$ .. )

    而其他人只是使用以下内容并将所有邮政编码写在
    模型中。 / p>

      function insert()
    {
    $ name = $ this-> input-> post 'var1');
    $ name = $ this-> input-> post('var1');
    $ name = $ this-> input-> post('var1');
    }


  • 某些表为每个表创建不同的Model,模型..



    不同开发人员之间的更多矛盾,
    我非常困惑,使用哪种技术来编写更高效的
    和行业



解决方案

这是一个很好的问题。它回到了Codeigniter的利弊。因为你可以自由地自定义Codeitgniter(很多人过度),像这样的问题出现,由开发者决定哪里属于什么。



根据Codeigniter的MVC标准:

因此,模型应该只处理与数据库的通信,或者如果您不使用数据库,它应该处理从其他来源(即json,文本文件等)检索数据。



我推荐:




  • 将所有内部功能保留在控制器方法中,您的 $ this-> input-> post()信息。一旦你拥有所有的 post()信息,创建必要的数组并将其传递给模型

  • 模型,



使用此结构,您可以重复使用这些模型方法来自任何控制器,保持模块化结构。



希望这有助于。


I am very much confused regarding which technique to use to code Model in codeigniter.

I searched a lot and found various methods used by various coders. Kindly guide me the best way to write Model Class using codeigniter.

Few examples;

  • Some take long parameters in Method signature

     function insert_data($name, $address, $email, $....,$...,$..)
    

    While others just use the following and write all post code insideModel..

    function insert()
    {
    $name= $this->input->post('var1');
    $name= $this->input->post('var1');
    $name= $this->input->post('var1');
    }
    

  • Some create different Model for each Table, while some write very few Models..

    And many more contradictions between different developers,I am so much confused in which technique to use to write more efficientand industry standard code.

    Kindly guide me Thanks a lot

解决方案

This is a good question. It goes back on the pros and cons of Codeigniter. Being that you can customize Codeitgniter as much as you wish (which a lot of people overdo), issues like these come up, where its up to the developer to decide where things belong. With that being said, I will share how I use models.

According to Codeigniter's MVC standards:

Therefore a model should only handle the communication with your database, or if you don't use databases, it should handle the retrieval of data from other sources (i.e. json, text files, etc...).

What I recommend:

  • Keep all your internal functionalities in your controller method, this includes all your $this->input->post() information. Once you have all your post() information, create the necessary array and pass it to the model
  • The model, should receive the array (which you constructed in your controller) and build the database queries needed to retrieve the information.

With this structure, you may reuse these model methods from any controllers, keeping a modular structure.

Hope this helps.

这篇关于在Codeigniter中编写模型代码的最好方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 17:34
查看更多