【问题标题】:cakephp 2.6 retrieving data from databasecakephp 2.6 从数据库中检索数据
【发布时间】:2015-08-01 02:45:04
【问题描述】:

这是我在 cakephp 控制器中的函数编辑:

public function edit($id = null) { 
if ($this->request->is('get')) {
            $this->request->data = $this->Topic->findById($id);
        }
...

第一个问题是传递给函数的参数 id 的类型是字符串而不是整数。 其次,在数据库中有一个 id = 14 的主题,这两个代码检索到相同的结果,我不明白为什么:

http://localhost/cakephp1/topics/edit/14
http://localhost/cakephp1/topics/edit/14anyCharactersHere

【问题讨论】:

    标签: php cakephp


    【解决方案1】:

    URL 传递的参数当然是字符串类型,因为 URL 是字符串。

    假设您的数据库中的id 列是整数Cake 会将传递给findById() 的参数转换为整数:(int)"14"(int)"14anyCharactersHere" 都导致14。因此,您问题中的两个 URL 都会导致从数据库中提取相同的数据。

    如果你想确保$id 只包含一个整数,请使用ctype_digit():-

    if (ctype_digit($id) === true) {
        // Is integer
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-19
      相关资源
      最近更新 更多