Php Zendframework 2。SQL查询中的ID参数
我有点了解需要什么,但我是ZF2的新手,所以只需要朝着正确的方向努力 我目前设置了一个路由,例如,Php Zendframework 2。SQL查询中的ID参数,php,zend-framework2,Php,Zend Framework2,我有点了解需要什么,但我是ZF2的新手,所以只需要朝着正确的方向努力 我目前设置了一个路由,例如,viewsystem/1,其形式为[action][id] 当一个人单击一个链接时,他们会更改自己的id,例如,viewsystem/5 在运行SQL的模型中,我希望更改SQL语句的id: ->where('system.Id = "'.$id.'" ') 有人能解释一下我在哪里可以“获取”参数并将其用作SQL中的变量吗? 我需要在控制器中执行某些操作吗?我能不能不用$\u GET之类的东西
viewsystem/1
,其形式为[action][id]
当一个人单击一个链接时,他们会更改自己的id,例如,viewsystem/5
在运行SQL的模型中,我希望更改SQL语句的id:
->where('system.Id = "'.$id.'" ')
有人能解释一下我在哪里可以“获取”参数并将其用作SQL中的变量吗?
我需要在控制器中执行某些操作吗?我能不能不用$\u GET
之类的东西
我已经更新了这一点,因为很清楚看到正在发生什么。
viewsystemAction()
的路由与ajaxviewsystemAction()
的路由不同
当我使用$id=(int)$this->params()->fromRoute('id',0)时
在viewsystemAction()
中,它回显页面链接id路径,例如viewsystem/220
当我使用$id=(int)$this->params()->fromRoute('id',0)时
在ajaxviewsystemAction()
中,它将0作为路由id回显
我需要通过此函数的路径
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'));
}
为了更好地解释,这是我的问题 如您所见,我正在按照您的建议将一个参数传递到
fetchViewAllSystems($id=1)代码>
fetchViewAllSystems
在我的模型中,可以完美地工作,其中的1表示系统1
但是,1必须是url id
$id = (int) $this->params()->fromRoute('id', 0);
这将获取viewaction中的ID,但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;
}
要在控制器中获取$\u get params,请执行以下操作:
// your IndexController.php
public function indexAction(){
$viewmodel = new ViewModel();
// get the ID
$id = $this->params('id', null); // null is my default value
// ...
return $viewmodel;
}
我强烈建议您检查这个伟大的示例:-
检查这条线
获取参数
$id = $this->params('id', null); // null is my default value
或
参考:
controller.php
我不知道getSystemsTable()返回什么,也不知道fetchViewAllSystems返回什么,但应该是这样的
private function getSourceViewAllSystems($id = 1)
{
return $this->getSystemsTable()->fetchViewAllSystems($id);
}
public function viewsystemAction()
{
$id = $this->params()->fromRoute('id', null);
if (!$id) {
return $this->redirect()->toRoute('systems', array(
'action' => 'activesystems'
));
}
echo $id;
}
public function ajaxviewsystemAction()
{
$id = $this->params()->fromRoute('id', null);
$id = $this->params()->fromQuery()['id']; // if from ajax GET param
$table = new TableExample\Advance();
$table->setAdapter($this->getDbAdapter())
->setSource($this->getSourceViewAllSystems($id)) // send the current id
->setParamAdapter($this->getRequest()->getPost())
;
return $this->htmlResponse($table->render('custom' , 'custom-b2'));
}
private function getSourceViewAllSystems($id = 1)
{
return $this->getSystemsTable()->fetchViewAllSystems($id);
}
public function viewsystemAction()
{
$id = $this->params()->fromRoute('id', null);
if (!$id) {
return $this->redirect()->toRoute('systems', array(
'action' => 'activesystems'
));
}
echo $id;
}
public function ajaxviewsystemAction()
{
$id = $this->params()->fromRoute('id', null);
$id = $this->params()->fromQuery()['id']; // if from ajax GET param
$table = new TableExample\Advance();
$table->setAdapter($this->getDbAdapter())
->setSource($this->getSourceViewAllSystems($id)) // send the current id
->setParamAdapter($this->getRequest()->getPost())
;
return $this->htmlResponse($table->render('custom' , 'custom-b2'));
}