Zendframework 2. SQL 查询中的 ID 参数 [英] Zendframework 2. ID parameter in SQL query

查看:23
本文介绍了Zendframework 2. SQL 查询中的 ID 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点了解需要什么,但我是 ZF2 的新手,所以只需要朝着正确的方向前进.

I kind of understand what is needed but I am new to ZF2 so just need pushing in the right direction.

我目前设置了一个路由,例如 viewsystem/1,它的格式为 [action][id].

I currently have a route set up, for example, viewsystem/1, which has the form [action][id].

当一个人点击一个链接时,他们会改变他们的 id,例如,viewsystem/5.

When a person clicks on a link, they change their id, for example, viewsystem/5.

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

In the model where I run the SQL, I wish the id to change for the SQL statement:

->where('system.Id = "'.$id.'" ')

谁能解释我在哪里可以获取"参数并将其用作 SQL 中的变量?我需要在控制器中做些什么吗?我不能只使用 $_GET 或其他东西吗?

Can anyone explain where I can "get" the parameter and use this as a variable in the SQL? Do I need to do something in the controller? Can I not just use a $_GET or something?

我已经更新了这个,因为很清楚看到发生了什么.viewsystemAction() 的路由与 ajaxviewsystemAction() 的路由不同.

I have updated this, as it is quite clear to see what is happening. The route for viewsystemAction() is different to the route of ajaxviewsystemAction().

当我在 viewsystemAction()$id = (int) $this->params()->fromRoute('id', 0); 时>,回显页面链接id路由,例如viewsystem/220

When I use $id = (int) $this->params()->fromRoute('id', 0); inside the viewsystemAction(), it echoes back the page link id route, for example viewsystem/220

当我在 ajaxviewsystemAction()$id = (int) $this->params()->fromRoute('id', 0); 时>,它回显 0 作为路由 id.

When I use $id = (int) $this->params()->fromRoute('id', 0); inside the ajaxviewsystemAction(), it echoes back the 0 as the route id.

我需要通过这个函数的路由

I need the route to be passed through this function

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

<小时>

在这里尝试更好地解释是我的问题.


To try explain a bit better here is my issue.

正如你所看到的,我将你建议的参数传递给 fetchViewAllSystems($id = 1);fetchViewAllSystems 在我的模型中并且运行良好,1 在那里,它显示 system1.

As you can see i am passing a param as you suggested into fetchViewAllSystems($id = 1); fetchViewAllSystems is in my model and works perfectly, with the 1 there, it displays the system1.

但是,1 必须是 url id.

however, the 1 needs to be the url id.

$id = (int) $this->params()->fromRoute('id', 0);

这会获取 viewaction 中的 ID,但 viewaction 不控制 fetchViewAllSystems,因此从 url 传递此值非常棘手.

This gets the ID in the viewaction, but viewaction does not control the fetchViewAllSystems so it is quite tricky to pass this value from the 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;
    }

推荐答案

要在控制器中获取 $_GET 参数,请执行以下操作:

To get a $_GET params in your controller, do like this:

// 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

I highly recommend you to check this great example : 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

控制器.php

我不知道 getSystemsTable() 返回什么,也不知道 fetchViewAllSystems 但应该是这样的

I don't know what getSystemsTable() returns and also fetchViewAllSystems but it should be like this

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

这篇关于Zendframework 2. SQL 查询中的 ID 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆