【问题标题】:Issue with a Table Model表模型问题
【发布时间】:2015-02-28 02:38:10
【问题描述】:

我最近在使用表模型时遇到了问题,我无法真正解释为什么会发生这种情况。我想从表中选择所有内容并按降序返回。但是当我尝试显示它时,什么都没有显示:

代码:

<?php

//Will be used at a later date

namespace Blog\Model\Table;

use Zend\Db\TableGateway\TableGateway;

class Blog extends TableGateway
{
    public function __construct($adapter)
    {
        parent::__construct('posts', $adapter);
    }

    public function displayPosts()
    {
        $adapter = $this->getAdapter();
        $result = $adapter->query('SELECT * FROM `posts` ORDER BY `date_added` DESC');
        return $result;
    }
}

结果:

但是当我添加 $id 参数时,它会显示数据:

代码:

<?php

//Will be used at a later date

namespace Blog\Model\Table;

use Zend\Db\TableGateway\TableGateway;

class Blog extends TableGateway
{
    public function __construct($adapter)
    {
        parent::__construct('posts', $adapter);
    }

    public function displayPosts($id = 6)
    {
        $adapter = $this->getAdapter();
        $result = $adapter->query('SELECT * FROM `posts` WHERE `post_id` = ?', array($id));
        return $result;
    }
}

结果:

奇怪的是,它与一个论点一起工作,而不是没有任何想法?

【问题讨论】:

    标签: mysql zend-framework pdo model


    【解决方案1】:

    您需要通过 post_id 订购

    public function displayPosts()
    {
        $adapter = $this->getAdapter();
        $result = $adapter->query('SELECT * FROM `posts`')->order(array('post_id' => 'DESC'));
    
        return $result;
    }
    

    【讨论】:

    • 你能把你的数据库表结构贴出来吗
    • Fatal error: Call to undefined method Zend\Db\Adapter\Driver\Pdo\Statement::order() 是不是因为PDO驱动?我应该将其更改为 Mysqli 吗?
    猜你喜欢
    • 2010-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多