thesnake 发表于 2011-3-2 12:18:43

如此问题用CI的model要怎么解决?

本帖最后由 thesnake 于 2011-3-2 14:59 编辑


$first = new test(1);

$second = new test(12);

print_r($first->value);

echo "<br>";

print_r($second->value);

class test
{
        var $id;
        var $value;

        function __construct($id) {
                $this->id = $id;

                $this->value = $id + 1;
        }
}


上面代码,用普通的php类,是很平常的应用,这样就可以生成两个互不影响的对象.

但是因为CI应用model的时候,是不能传递参数的,所以像上面例子这样的代码,用CI的model要如何实现?

visvoy 发表于 2011-3-2 12:35:31

$first=$this->load->model('test');
$first->init(1);

thesnake 发表于 2011-3-2 12:39:50

$first=$this->load->model('test');
$first->init(1);
visvoy 发表于 2011-3-2 12:35 http://codeigniter.org.cn/forums/images/common/back.gif


这个init需要我自己在test里面定义吧?

Hex 发表于 2011-3-2 13:55:44

是的

thesnake 发表于 2011-3-2 14:28:43

本帖最后由 thesnake 于 2011-3-2 14:54 编辑

是的
Hex 发表于 2011-3-2 13:55 http://codeigniter.org.cn/forums/images/common/back.gif

CI设计的时候,为什么没有将model设计的跟library一样可以传参数进去?

而且CI的model中,不能在处__construct以外的方法中调用另外的model吧,这样的话,用1楼的方法就不灵活了.

thesnake 发表于 2011-3-2 15:30:53

回复 2# visvoy


    您的例子在我这里没有办法正确运行 1.7.3 和 2.0.0都不行

thesnake 发表于 2011-3-2 15:38:15

$first=$this->load->model('test');
$first->init(1);
visvoy 发表于 2011-3-2 12:35 http://codeigniter.org.cn/forums/images/common/back.gif

我看了一下CI的load->model方法的源代码,没有return啊,所以按你的方法 $first应该是没有值的.

visvoy 发表于 2011-3-2 17:35:12

那你要扩展CI_Loader,让->model可以带参数

visvoy 发表于 2011-3-2 17:36:57

回复visvoy


    您的例子在我这里没有办法正确运行 1.7.3 和 2.0.0都不行
thesnake 发表于 2011-3-2 15:30 http://codeigniter.org.cn/forums/images/common/back.gif


首先乃要看下手册,知道怎么写model,然后乃还要看下手册,知道怎么用model

thesnake 发表于 2011-3-2 18:13:09

那你要扩展CI_Loader,让->model可以带参数
visvoy 发表于 2011-3-2 17:35 http://codeigniter.org.cn/forums/images/common/back.gif


    你是说改CI的内核代码么?我暂时还不打算这么干.
页: [1] 2
查看完整版本: 如此问题用CI的model要怎么解决?