2014-01-15 37 views
0

我有点理解需要什么,但我是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; 
    } 

回答

2

为了让你的控制器中的$ _GET参数,可以这样做:

// your IndexController.php 
public function indexAction(){ 
    $viewmodel = new ViewModel(); 

    // get the ID 
    $id = $this->params('id', null); // null is my default value 

    // ... 

    return $viewmodel; 
} 

我强烈建议你检查这个很好的例子:https://github.com/akrabat/zf2-tutorial - http://zf2.readthedocs.org/en/latest/ref/overview.html

检查这条线https://github.com/akrabat/zf2-tutorial/blob/master/module/Album/src/Album/Controller/AlbumController.php#L43

获取参数

$id = $this->params('id', null); // null is my default value 

$id = $request->query()->get('foo', 'default value'); 

编号:http://zend-framework-community.634137.n4.nabble.com/ZF2-How-to-set-get-params-in-url-td4076050.html


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')); 
}