【发布时间】:2013-03-08 22:30:46
【问题描述】:
我有以下型号:
class Model_Job extends ORM
{
public $ID;
public $user_ID;
public $title;
//some more variables
}
在我的控制器中,我有一个函数action_view();查看作业详情单作业,实现方式与http://kohanaframework.org/3.3/guide/orm/using#finding-an-object
public function action_view()
{
$this->render('Shit');
$this->template->content = View::factory('jobs/job')
->bind('job', $job);
$job = ORM::factory('job', $this->request->param('id'));
}
我有另一个函数action_all(),它只是使用find_all 获取所有作业并将它们放在页面上,这很好用(意思是echo $job->ID 做了它应该做的;回显ID。但是action_view() 没有. 我会放一些echo Debug::vars($job)的输出
object Model_Job(39) {
public ID => NULL //Note they are NULL
public user_ID => NULL
public title => NULL
......................
protected _object => array(5) (
"ID" => string(1) "1"
"user_ID" => string(1) "1"
"title" => string(14) "Testbaantjeeee"
................
)
.....................
}
而来自action_all() 的echo Debug::vars($job) 示例如下所示:
object Model_Job(39) {
public ID => 1 //Note they are NOT NULL
public user_ID => 1
public title => "Testbaantjeeee"
......................
protected _object => array(5) (
"ID" => string(1) NULL //now these are NULL
"user_ID" => string(1) NULL
"title" => string(14) NULL
.....................
)
.....................
}
我查看了 kohena 的有关 factory、find、find_all 等的文档,但无法弄清楚 factory 或 find 没有做 find_all 正在做的事情。我错过了什么吗?我让它工作了:
$job = ORM::factory('job')
->where('ID', '=', $this->request->param('id'))
->find_all()[0];
但是这样做对我来说完全没有意义。我错过了什么?
【问题讨论】:
-
尝试将
$_primary_key属性设置为“ID”值。见kohanaframework.org/3.3/guide-api/ORM#property:_primary_key -
另外,你能显示你的型号代码吗?我不明白,为什么您需要公共字段(ID、user_ID 等),而 ORM 允许您“开箱即用”访问它们。
-
$job->object()['title']这行得通,protected _primary_key => string(2) "ID" public ID => NULL没有改变任何东西。那是我的模型代码,还有一些属性,但没有方法或任何东西。 -
不需要像ID这样的表字段。只需使用
$job->title、$job->ID(或$job->id())等
标签: php kohana kohana-orm