开发者

codeigniter model vs library which one to use

开发者 https://www.devze.com 2023-04-09 00:58 出处:网络
there is a function get_user_group_ids($user_id), it returns all groups the user belongs. this pings the batch table and gets all the batch_ids. ANd then pings group table and get group_ids correspond

there is a function get_user_group_ids($user_id), it returns all groups the user belongs. this pings the batch table and gets all the batch_ids. ANd then pings group table and get group_ids corresponding 开发者_运维百科to batch_ids

This is used in event, job and other libraries like eg , get all the job which belongs to my batch. here i need to call this function to get my group ids.

because this is a common function, i have doubt whether to create common library or model


If this function is fetching the ID's from a database or other external datasource then a model is the way to go :)


I've been in a similar situation where I needed to share a model (an abstract collection of methods) between a framework (CakePHP in my case) and shell scripts for batch jobs. To do so I found that the simplest method was to create a CakePHP model like any other model EXCEPT it didn't extend the base (parent) framework model class (CI_Model in your case). The advantage with this was that I was still able to "load" (initiate) the class from CakePHP using the same conventions as any other framework model but was able to use the same model (the actuall file) in batched shell scripts without needing to load anything extra.

I'm pretty certain the same is possible in CI (although I'm not a heavy CI user):

# the model definition /application/models/CustomModel.php
class CustomModel {
    public function __construct() {} # ...
    public function get_user_group_ids( $id ) {} # ...
}


# from inside the a controller method:
$this->load->model( 'CustomModel' );
$this->CustomModel->get_user_group_ids( $id ); # ...


# from inside the shell scripts:
require_once( YOUR_CI_PATH . '/application/models/CustomModel.php' );
$obj = new CustomModel();
$obj->get_user_group_ids( $id ); # ...

Hope this helps, Cheers

0

精彩评论

暂无评论...
验证码 换一张
取 消