我有点理解需要什么,但我是ZF2的新手,所以只需要朝正确的方向推动即可。Zendframework 2. SQL查询中的ID参数
我目前有一个路线设置,例如viewsystem/1
,其形式为[action][id]
。
当一个人点击一个链接时,他们改变他们的ID,例如,viewsystem/5
。
在我运行SQL模型,祝ID更改SQL语句:
->where('system.Id = "'.$id.'" ')
谁能解释我在哪里可以“搞定”的参数,并使用此作为一个变量SQL? 我需要在控制器中做些什么吗?我能不能只使用$_GET
什么的?
我已经更新了这个,因为很清楚看到发生了什么。 viewsystemAction()
的路线与ajaxviewsystemAction()
的路线不同。
当我使用$id = (int) $this->params()->fromRoute('id', 0);
的viewsystemAction()
里面,它返回了页面链接ID途径,例如viewsystem/220
当我使用$id = (int) $this->params()->fromRoute('id', 0);
的ajaxviewsystemAction()
里面,它返回了0作为路由标识。
我需要的路由通过此功能
private function getSourceViewAllSystems($id)
{
return $this->getSystemsTable()->fetchViewAllSystems($id);
}
public function viewsystemAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
echo $id; //i see the correct id for example 220 from the route in the browser
}
public function ajaxviewsystemAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
echo $id; //to see the id of the route with the ajax page
//displays 0 and not the route id from the viewsystemAction
$table = new TableExample\Advance();
$table->setAdapter($this->getDbAdapter())
->setSource($this->getSourceViewAllSystems($id))
->setParamAdapter($this->getRequest()->getPost());
return $this->htmlResponse($table->render('custom' , 'custom-b2'));
}
被传递到尝试解释更好一点,这里是我的问题。
正如你可以看到我传递一个PARAM如你所说到fetchViewAllSystems($id = 1);
fetchViewAllSystems
是在我的模型和完美的作品,与1有,它会显示系统1。
但是,1需要是网址ID。
$id = (int) $this->params()->fromRoute('id', 0);
这得到在ViewAction这一标识,但ViewAction这一不控制fetchViewAllSystems
所以它是相当棘手的从URL传递此值。
private function getSourceViewAllSystems()
{
return $this->getSystemsTable()->fetchViewAllSystems($id = 1);
}
public function viewsystemAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
/*if (!$id) {
return $this->redirect()->toRoute('systems', array(
'action' => 'activesystems'
));
}*/
echo $id;
}
public function ajaxviewsystemAction()
{
/*$table = new TableExample\Base();
$table->setAdapter($this->getDbAdapter())
->setSource($this->getSourceViewAllSystems())
->setParamAdapter($this->getRequest()->getPost())
;
return $this->htmlResponse($table->render());*/
$table = new TableExample\Advance();
$table->setAdapter($this->getDbAdapter())
->setSource($this->getSourceViewAllSystems())
->setParamAdapter($this->getRequest()->getPost())
;
return $this->htmlResponse($table->render('custom' , 'custom-b2'));
echo $id;
}