【问题标题】:ZF2 TableGateway select() returns rows but values are emptyZF2 TableGateway select() 返回行但值为空
【发布时间】:2015-05-15 22:41:36
【问题描述】:

我是 ZendFramework2 的新手,并尝试构建一个简单的 CRUD 应用程序,但我已经遇到了来自数据库的书籍概览的问题。

在我的索引视图中,我循环遍历返回的书籍数组,该数组的计数为 7(8 行)但值是空的,除了包含作者 (??) 的 id 字段。很奇怪。

控制器:

<?php

namespace BookList\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use BookList\Form\BookForm;
use BookList\Model\Book;

class BookController extends AbstractActionController {

protected $bookTable;

public function indexAction() {
    return new ViewModel(array(
            'books' => $this->getBookTable()->fetchAll()
    ));
}// ...

型号:

<?php

namespace BookList\Model;
class Book {
    public $id;


    public $title;
    public $author;

    public function exchangeArray($data) {
        $this->id = (!empty($data['id'])) ? $data['id'] : null;
        $this->id = (!empty($data['title'])) ? $data['title'] : null;
        $this->id = (!empty($data['author'])) ? $data['author'] : null;
    }

}

书桌:

use Zend\Db\TableGateway\TableGateway;

class BookTable {

    protected $tableGateway;

    public function __construct(TableGateway $tableGateway) {
        $this->tableGateway = $tableGateway;
    }

    public function fetchAll() {
        $resultSet = $this->tableGateway->select();
        return $resultSet;
    }//...

索引.phtml:

<?php foreach ($books as $book) { ?>
    <tr>
        <td><?php echo $this->escapeHtml($book->title); ?></td>
        <td><?php echo $this->escapeHtml($book->author); ?></td>
        <td>
            <a href="<?php echo $this->url('book', array('action' => 'edit', 'id' => $book->id))?>">Edit</a>
            <a href="<?php echo $this->url('book', array('action' => 'delete', 'id' => $book->id))?>">Delete</a>
        </td>
    </tr>
<?php }?>

模块.php

..//
    public function getServiceConfig() {
        return array(
                'factories' => array(
                        'BookList\Model\BookTable' => function($sm) {
                            $tableGateway = $sm->get('BookTableGateway');
                            $table = new BookTable($tableGateway);
                            return $table;
                        },
                        'BookTableGateway' => function ($sm) {
                            $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                            $resultSetPrototype = new ResultSet();
                            $resultSetPrototype->setArrayObjectPrototype(new Book());
                            return new TableGateway('book', $dbAdapter, null, $resultSetPrototype);
                        }
                ),
        );
    }

【问题讨论】:

    标签: php zend-framework2


    【解决方案1】:

    我认为问题出在您的模型上。在exchangeArray() 中,您只需要设置id 字段。所以因为最后一行是给作者的,所以id 被设置为。

    试试这个:

    public function exchangeArray($data) {
        $this->id     = (!empty($data['id'])) ? $data['id'] : null;
        $this->title  = (!empty($data['title'])) ? $data['title'] : null;
        $this->author = (!empty($data['author'])) ? $data['author'] : null;
    }
    

    【讨论】:

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