gpt4 book ai didi

php - Zend Framework 2 TableGateway 错误的字段映射

转载 作者:行者123 更新时间:2023-11-30 23:01:15 26 4
gpt4 key购买 nike

尝试适应 ZF2 skeleton application ,我在处理 resultSet 中检索到的字段时遇到问题。我的表“project”包含四个字段:id、title、shortTitle、year。我正在使用典型的 fetchAll 函数:

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

Project类体现了四个领域

class Project {
public $id;
public $title;
public $shortTitle;
public $year;

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

Module.php中的表网关工厂如教程所述:

public function getServiceConfig() {
return array(
'factories' => array(
'Project\Model\ProjectTable' => function($sm) {
$tableGateway = $sm->get('ProjectTableGateway');
$table = new ProjectTable($tableGateway);
return $table;
},
'ProjectTableGateway' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Project());
return new TableGateway('project', $dbAdapter, null, $resultSetPrototype);
},
),
);
}

传递给数据库的查询是正确的,并给出了正确的结果:

SELECT `project`.* FROM `project` 

$resultSet 看起来它获得了正确的原型(prototype)和正确的行数,但它没有正确匹配列名。 print_r($resultSet->current());给出:

Project\Model\Project Object ( [id] => 1 [title] => Some title 1 
[shortTitle] => [year] => )

同时 print_r($data);在 exchangeArray 函数中给出:

Array ( [id] => 1 [title] => Some title 1 [project] => 2012 )

对象似乎只获得了前两个字段,然后创建了一个字段 [project],其中包含结果中可用的最后一列的值。

我一定是在某个地方犯了一个愚蠢的错误,但我想不通在哪里。谢谢。

最佳答案

请检查 Module.php 中的 TableGateway 工厂 - 它应该与此类似 -

也需要 use ... 语句。

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use [.....]\Model\Product; //Add the Module Name here
use [.....]\Model\ProductTable; //Add the Module Name here

'Project\Model\ProjectTable' => function($sm) { //Change Module name accordingly.
$tableGateway = $sm->get('ProjectTableGateway');
$table = new ProjectTable($tableGateway);
return $table;
},
'ProjectTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Project());
return new TableGateway('project', $dbAdapter, null, $resultSetPrototype);
//Change the the tablename accordingly.
},

然后在你的问题描述中添加print_r($resultSet->current());的结果。

关于php - Zend Framework 2 TableGateway 错误的字段映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23858093/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com