我一直看到以$this->example_model->method('Some Title');
我最近在这里看到了一个答案(现在找不到),说正确创建的模型应该接收这样的方法的参数:
$this->example_model->method->title = 'Some Title';
我似乎无法弄清楚如何做到这一点,模型方法将如何实现这一目标?这真的是如何传递参数吗?
答案 0 :(得分:0)
嗯,取决于需求和使用情况。
假设我需要从模型中的数据库表中提取所有条目 - 这没关系:
$this->example_model->get();
假设我需要根据一些标准获取所有条目。我可能会做这样的事情:
$criterias = array('age' => '> 10', 'gender' => 'm');
$this->example_model->get($criterias);
你想要完成的事情会更像是这样:
$this->example_model->criterias->age = '> 10';
$this->example_model->criterias->gender = 'm';
$this->example_model->get();
或者你可以选择更适合方法绑定的东西:
$this->example_model->set_criteria('age', '> 10');
$this->example_model->set_criteria('gender', 'm');
$this->example_model->get();
// With method binding:
$this->example_model->set_criteria('age', '> 10')->set_criteria('gender', 'm')->get();
基本上,所有解决方案都很好 - 其中没有一个是最正确的,并且仍然有几种方法可以实现相同的目标。但有些比其他人更具可读性(并且不易出错)。你应该选择适合你的任何东西,你很乐意与之合作。