como juntar tabelas usando tablegateway
Como juntar tabelas no zend3 ao usar o tableadapter? A questão não é sobre como unir tabelas em geral, é sobre como fazer isso no zend e onde colocar o código.
Digamos que eu possua uma classe * table por exemplo:
namespace Import\Model;
use RuntimeException;
use Zend\Db\TableGateway\TableGatewayInterface;
class ProjectTable
{
private $tableGateway='t_project';
public function __construct(TableGatewayInterface $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function fetchAll()
{
return $this->tableGateway->select();
}
Eu gostaria de juntar duas mesas, como posso fazer isso, não é aqui o lugar certo? Eu tentei implementar a seguinte função:
public function Project_Unit(Unit $unit = null){
$select = $this->tableGateway->getSql()->select()
->join('t_unit', 't_project.ProjectID = t_unit.ProjectID',array('UnitID','CI_Number', 'Unitname','Shortcut','Suppliername')); //, left
return $this->tableGateway->selectWith($select);
}
Não recebi um erro; em vez disso, misturei dados. Depois disso, tentei com aliases também não funcionou.
Minha pergunta é: como construir esta toalha de mesa, se eu precisar juntar duas tabelas. As tabelas serão do projeto 1 -> n unit (keyID do projeto). A segunda pergunta seria como usar aliases corretamente, porque eu tenho alguns nomes de campo nas duas tabelas com dados diferentes, por exemplo, cada tabela possui um atalho de coluna.
EDIT: Novas informações Para ver de onde vêm os dados, renomeei as variáveis do meu Exchangearray:
public function exchangeArray(array $data)
{
$this->PProjectID= !empty($data['ProjectID']) ? $data['ProjectID'] : null;
$this->PCI_Number= !empty($data['CI_Number']) ? $data['CI_Number'] : null;
$this->PDescription= !empty($data['Description']) ? $data['Description'] : null;
$this->Projectname= !empty($data['Projectname']) ? $data['Projectname'] : null;
$this->PShortcut= !empty($data['Shortcut']) ? $data['Shortcut'] : null;
$this->PComponent_Class= !empty($data['Component_Class']) ? $data['Component_Class'] : null;
}
Agora eu recebo uma saída interessante (adicionei também o conteúdo do meu dataarray)
Eu tenho duas colunas que são nomeadas iguais, que serão atalho e número ci, esses campos de dados são misturados com os mesmos da tabela tableadapter.
U1 não é o atalho do galileo, é o atalho da unidade. O atalho do Galileo deve ser GAL. Parece que as colunas com o mesmo nome são preenchidas pela segunda tabela (unidade), mas não recebo nenhum campo da unidade da tabela.
EDIT: para mostrar a adição que fiz das sugestões do jobaer:
Editei minha classe ProjectTable:
class ProjectTable
{
//private $tableGateway='t_project';
private $projectTableGateway;
private $unitTableGateway;
// public function __construct(TableGatewayInterface $tableGateway)
// {
// $this->tableGateway = $tableGateway;
// }
public function __construct(
TableGatewayInterface $projectTableGateway,
TableGatewayInterface $unitTableGateway)
{
$this->projectTableGateway = $projectTableGateway;
$this->unitTableGateway = $unitTableGateway;
}
public function fetchAll()
{
$sqlSelect = $this->unitTableGateway->getSql()->select();
/**
* columns for the "project_table" exactly it is unit_table
*/
$sqlSelect->columns(array('CI_Number', 'ProjectID','Unitname','Shortcut','Suppliername'));
/**
* this can take two more arguments:
* an array of columns for "unit_table"
* and a join type, such as "inner"
*/
$sqlSelect->join('t_project', 't_unit.ProjectID = t_project.ProjectID');
/**
* set condition based on columns
*/
//$sqlSelect->where(array('unit_table.project_id' => $id));
$resultSet = $this->unitTableGateway->selectWith($sqlSelect);
return $resultSet;
//return $this->tableGateway->select();
}
Também editei meu Module.php como sugerido, aqui vem um trecho
// Model\ProjectTable::class => function($container) {
// $tableGateway = $container->get(Model\ProjectTableGateway::class);
// return new Model\ProjectTable($tableGateway);
// },
Model\ProjectTable::class => function($container) {
$projectTableGateway = $container->get(Model\ProjectTableGateway::class);
$unitTableGateway = $container->get(Model\UnitTableGateway::class);
return new Model\ProjectTable($projectTableGateway, $unitTableGateway);
},
Model\ProjectTableGateway::class => function ($container) {
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Model\Project());
return new TableGateway('t_project', $dbAdapter, null, $resultSetPrototype);
}
Minha ação do controlador não foi alterada:
return new ViewModel([
'projects' => $this->projectTable->fetchAll(),
]);
Na minha opinião, tentei pegar as colunas das duas tabelas:
foreach ($projects as $project) :
// $unit=$units->fetchAllP($project->ProjectID);
var_dump(get_object_vars($project));?>
<tr>
<td><?= $this->escapeHtml($project->Unitname) ?></td>
<td><?= $this->escapeHtml($project->Projectname) ?></td>
<td><?= $this->escapeHtml($project->Shortcut) ?></td>
<td><?= $this->escapeHtml($project->CI_Number) ?></td>
<td><?= $this->escapeHtml($project->Description) ?></td>
<td><?= $this->escapeHtml($project->Component_Class) ?></td>
<td>
<a href="<?= $this->url('project', ['action' => 'edit', 'id' => $project->ProjectID]) ?>">Edit</a>
<a href="<?= $this->url('project', ['action' => 'delete', 'id' => $project->ProjectID]) ?>">Delete</a>
</td>
<?php endforeach; ?>
Eu tenho uma saída interessante, então algo ainda está faltando. Eu esperava ter todas as colunas das duas tabelas unidas.
EDIT2: para mostrar a próxima versão
aqui está o meu método fetchAll () / class ProjectTable
public function fetchAll()
{
$sqlSelect = $this->unitTableGateway->getSql()->select();
$sqlSelect->columns(array('UnitID','CI_Number', 'ProjectID','Unitname','Shortcut','Suppliername'));
$sqlSelect->join('t_project', 't_unit.ProjectID = t_project.ProjectID', array('Project' =>'Projectname','CI' =>'CI_Number','PDescription' =>'Description','PShortcut' =>'Shortcut','PComponent' =>'Component_Class','PProjectID' =>'ProjectID'));
//$sqlSelect->where(array('unit_table.project_id' => $id));
$resultSet = $this->unitTableGateway->selectWith($sqlSelect);
//return $resultSet;
return $resultSet->toArray();
//return $this->tableGateway->select();
Aqui está o meu viewcript:
<?php
//var_dump(get_object_vars($projects));
foreach ($projects as $project) :
//var_dump(get_object_vars($project));
?>
<tr>
<td><?= $project['Project']?></td>
<td><?= $project['CI']?></td>
<td><?= $project['Unitname']?></td>
<?php
endforeach; ?>
}
e aqui uma nova captura de tela:
EDIT3: adicionando itens de unidade
class UnitTable
{
private $tableGateway='t_unit';
public function __construct(TableGatewayInterface $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function fetchAll()
{
return $this->tableGateway->select();
}
classe Unidade também:
class Unit implements InputFilterAwareInterface
{
public $UnitID;
public $CI_Number;
public $ProjectID;
public $Unitname;
public $Shortcut;
public $Suppliername;
private $inputFilter;
public function exchangeArray(array $data)
{
$this->UnitID= !empty($data['UnitID']) ? $data['UnitID'] : null;
$this->CI_Number= !empty($data['CI_Number']) ? $data['CI_Number'] : null;
$this->ProjectID= !empty($data['ProjectID']) ? $data['ProjectID'] : null;
$this->Unitname= !empty($data['Unitname']) ? $data['Unitname'] : null;
$this->Shortcut= !empty($data['Shortcut']) ? $data['Shortcut'] : null;
$this->Suppliername= !empty($data['Suppliername']) ? $data['Suppliername'] : null;
}
Porque eu só tenho sampledata ainda, uma captura de tela da minha unidade e projeto de duas tabelas
EDIT4: Parte da fábrica do module.php
public function getServiceConfig()
{
return [
'factories' => [
Model\ImportTable::class => function($container) {
$tableGateway = $container->get(Model\ImportTableGateway::class);
return new Model\ImportTable($tableGateway);
},
Model\ImportTableGateway::class => function ($container) {
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Model\Import());
return new TableGateway('t_dcl', $dbAdapter, null, $resultSetPrototype);
},
Model\DclimportTable::class => function($container) {
$tableGateway = $container->get(Model\DclimportTableGateway::class);
return new Model\DclimportTable($tableGateway);
},
Model\DclimportTableGateway::class => function ($container) {
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Model\Dclimport());
return new TableGateway('t_dcl_import', $dbAdapter, null, $resultSetPrototype);
},
Model\FollowupTable::class => function($container) {
$tableGateway = $container->get(Model\FollowupTableGateway::class);
return new Model\FollowupTable($tableGateway);
},
Model\FollowupTableGateway::class => function ($container) {
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Model\Followup());
return new TableGateway('t_dcl_wv', $dbAdapter, null, $resultSetPrototype);
},
Model\UnitTable::class => function($container) {
$tableGateway = $container->get(Model\UnitTableGateway::class);
return new Model\UnitTable($tableGateway);
},
Model\UnitTableGateway::class => function ($container) {
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Model\Unit());
return new TableGateway('t_unit', $dbAdapter, null, $resultSetPrototype);
},
// Model\ProjectTable::class => function($container) {
// $tableGateway = $container->get(Model\ProjectTableGateway::class);
// return new Model\ProjectTable($tableGateway);
// },
Model\ProjectTableGateway::class => function ($container) {
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Model\Project());
return new TableGateway('t_project', $dbAdapter, null, $resultSetPrototype);
},
Model\ProjectTable::class => function($container) {
$projectTableGateway = $container->get(Model\ProjectTableGateway::class);
$unitTableGateway = $container->get(Model\UnitTableGateway::class);
return new Model\ProjectTable($projectTableGateway, $unitTableGateway);
}
],
];
}