Zendframework 2. SQL查询中的ID参数

时间:2014-01-15 12:36:56

标签: php zend-framework2

我有点理解需要什么,但我是ZF2的新手所以只需要朝着正确的方向努力。

我目前设置了路线,例如viewsystem/1,其格式为[action][id]

当某人点击某个链接时,他们会更改其ID,例如viewsystem/5

在我运行SQL的模型中,我希望为SQL语句更改id:

->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作为路由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;
    }

1 个答案:

答案 0 :(得分: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'));
}
相关问题