【问题标题】:Phalcon PHP PDO BIND causes empty resultPhalcon PHP PDO BIND 导致空结果
【发布时间】:2014-03-15 18:11:33
【问题描述】:

我现在正在使用 Phalcon PHP 库,

当我在 GamesModel 的参数中使用 PDO 绑定时,我不再得到结果,有什么想法吗?

我在工作through this

查看:(json_output.phtml)

echo $output->active;

型号:

class GamesModel extends Phalcon\Mvc\Model
{
    public function initialize()
    {
        $this->setSource("games");
    }

    /**
        Returns the status of a game found by ID
        @param $id The game ID
    */
    public function getStatusById( $id )
    {

        $result = GamesModel::query()
            ->where("id = :id:")
            ->bind( array("id" => $id) )
            ->execute();

        //$result = GamesModel::findFirst(3);

        return $result;
    }
}

控制器:

class GameController extends Phalcon\Mvc\Controller 
{

     private $gamesModel;

     public function initialize()
     {
        $this->gamesModel = new GamesModel();
     }   

    public function getStatusByIdAction( $id ){ 


        $this->view->setVar("output", $this->gamesModel->getStatusById( $id ) );

        $this->view->pick("layouts/json_output");

    }

}

表格

../game/getStatusById/3 在我取消注释 findFirst() 时显示为 0

否则我会得到

注意:未定义的属性: Phalcon\Mvc\Model\Resultset\Simple::$active ..\json_output.phtml on 第 3 行

我错过了什么吗?

我已在连接中将字符集定义为 UTF8。

【问题讨论】:

  • GamesModel::findFirst(3) 和 GamesModel::findFirstById(3) 也在内部使用绑定参数
  • 你是说findFirst()自动绑定参数?因为在示例页面中它建议您需要这样做以消除 SQL 注入的风险
  • 是的,它会自动绑定参数,你可以在你的数据库连接中添加一个日志,你可以看到它是如何通过占位符传递参数的

标签: php mysql pdo phalcon


【解决方案1】:

解决方案:

我真的不明白为什么,但是当在绑定参数数组(作为字符串和 int)中硬编码 id 时,我可以使用

echo $output->active 

在视图中,这工作正常,但是当将 id 绑定到传递给 getStatusByIdAction() 的变量 $id 时,我需要遍历结果。

foreach ($output as $outputRow) {
    echo $outputRow->active;
}

有用的东西:

var转储$result实际上包含查询字符串和绑定参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 2018-06-05
    • 2012-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多