0
I have a question in the use of DAO. If I need to return a set of attributes of 2 objects? Let’s say I have a DAO category that has a method that in addition to the category attributes needs to return together the product id attribute as would the return of the category DAO method? Would the return be an array? Or could it be a predefined class with stdClass? Every example I look for is always a simple CRUD.
An example:
public function opcoesDoFiltro($idCategoria){
try{
$pdo = $this->database->getPDO();
$stmt = $pdo->prepare("SELECT e.endereco_string_bairro, e.endereco_string_cidade, f.facilidade_string_facilidade "
. "FROM(((endereco AS e INNER JOIN contato AS c ON e.endereco_int_idContato = c.contato_int_id) INNER JOIN "
. "facilidade as f ON f.facilidade_int_idContato = c.contato_int_id) INNER JOIN anuncio AS a ON "
. "c.contato_int_id = a.anuncio_int_idContato) WHERE a.anuncio_int_idCategoria = ? "
. "GROUP BY e.endereco_string_bairro, e.endereco_string_cidade, f.facilidade_string_facilidade" );
$stmt->bindValue(1, $idCategoria, PDO::PARAM_INT);
$stmt->execute();
if($stmt->rowCount() > 0){
foreach($stmt->fetchAll(PDO::FETCH_ASSOC) as $opcao){
$gruposDoFiltro[] = $opcoesDoGrupoDoFiltro = new stdClass();
$opcoesDoGrupoDoFiltro->endereco_string_bairro = $opcao["endereco_string_bairro"];
$opcoesDoGrupoDoFiltro->endereco_string_cidade = $opcao["endereco_string_cidade"];
$opcoesDoGrupoDoFiltro->facilidade_string_facilidade = $opcao["facilidade_string_facilidade"];
}
return $gruposDoFiltro;
This code is in DAOS category but returns address attributes and facilities that are required. address and ease has its DAOS. Perhaps you’ll clarify the question.
may be the
array
and may also bestdClass
. Now all this will depend on why to return this information...– novic
So I have a DAO that makes a catch of 3 innerjoin to return these merged properties to the controller, the view needs to display this information together I think this should be common for those who use DAO not? However, it was here that the doubt came because each DAO is separated returning different objects according to its business rule but for this case the attributes are merged?
– Ricardo
you create the DAO with the common methods Insert, Update, Delete, Read, after that you can create a STABBING to solve this problem.
– novic
Virgil but I have numerous cases like this, I will have to generate several facades to exchange several Daos, innerjoin could not be a solution returning an array with the necessary attributes of the classes that assemble the answer list?
– Ricardo
it is difficult to opine, without seeing your case in depth. I gave you a way, but, there are several and perhaps even better.
– novic