zendframework/zend-db

HydratingResultSet Not Using Model Files

Closed this issue · 2 comments

GeeH commented

This issue has been moved from the zendframework repository as part of the bug migration program as outlined here - http://framework.zend.com/blog/2016-04-11-issue-closures.html


Original Issue: https://api.github.com/repos/zendframework/zendframework/issues/7558
User: @kingharrison
Created On: 2015-05-29T17:46:57Z
Updated At: 2015-11-06T21:59:37Z
Body
I noticed this because our column names in a DB2 database use # and $ and I have to use the model file with exchange array to transpose to object entities I can access (COMP# becomes COMPNUM for example). When I use the configuration below the results that return are building the objects on the fly and not using my model files.

//Model

<?php
namespace K3sbase\Model;

class BUYRGRP
{

    public $BY_COMPNUM; // Company ID

    public function exchangeArray($data)
    {
        $this->BY_COMPNUM = (isset($data['BY_COMP#'])) ? $data['BY_COMP#'] : null;
    }
}

// Here is my TableFactory

<?php 

namespace K3sbase\Model;


use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use K3sbase\Model\BUYRGRP;
use K3sbase\Table\BUYRGRPTable;
use Zend\Db\TableGateway\TableGateway;
use Zend\Stdlib\Hydrator\ObjectProperty;
use Zend\Db\ResultSet\HydratingResultSet;

class BUYRGRPTableFactory implements FactoryInterface
{

    public function createService(ServiceLocatorInterface $serviceLocator)
    {

        $db = $serviceLocator->get('Zend\Db\Adapter\Adapter');

        $resultSetPrototype = new HydratingResultSet();
        $resultSetPrototype->setHydrator(new ObjectProperty());
        $resultSetPrototype->setObjectPrototype(new BUYRGRP());

        $tableGateway = new TableGateway('K_BUYRGRP', $db, null, $resultSetPrototype);
        $table = new BUYRGRPTable($tableGateway);

        return $table;
    }

}

//Table

<?php
namespace K3sbase\Table;

use Zend\Db\TableGateway\TableGateway;

class BUYRGRPTable
{

    protected $tableGateway;

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

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

// The results I get from this query will be two fields, BY_COMPNUM (which will be null) and BY_COMP# which
// will have the value I am looking for.


Comment

User: @Martin-P
Created On: 2015-05-30T10:37:44Z
Updated At: 2015-05-30T10:37:44Z
Body
Duplicate with #6859


Comment

User: @kingharrison
Created On: 2015-05-30T10:41:42Z
Updated At: 2015-05-30T10:41:42Z
Body
Not a duplicate. Separate issue.


@kingharrison
Why do you use the ObjectProperty hydrator and the exchangeArray method?

Closing due to inactivity