I'm having some troubles when i get data of one of my table.
I'm using Doctrine 1 + Codeigniter and the problems is that the data obtains only the last one registry.
I don't have define any relation between this tables (OpModelos and AnSimNavTrans) and this is the scenery:
Controller:
class Modelos extends CI_Controller {
public function view($idModelo) {
$aData['site_description'] = 'AutoDato';
$aData['site_keywords'] = 'keywords';
$aData['site_robots'] = 'robots';
$aData['site_author'] = 'author';
$modelo = Doctrine::getTable('OpModelo')->find($idModelo);
$modelo_caract = Doctrine::getTable('anCaractUserMo')->getByModeloId($idModelo);
$modelos_similares_nav = Doctrine::getTable('OpModelo')->getModeloSimNav($idModelo);
$aData['modelo'] = $modelo;
$aData['modelo_caract'] = $modelo_caract;
$aData['modelos_similares_nav'] = $modelos_similares_nav;
$aData['view'] = 'modelo/view';
$this->load->view('template', $aData);
}
}
data table AnSimNavTrans
op_modelo_id cod_mod med_sim
1 10 0.9
1 2 0.8
1 11 0.7
1 4 0.5
1 6 0.1
Model
class OpModeloTable extends Doctrine_Table {
public function getModeloSimNav($idModelo) {
$query = Doctrine_Query::create();
$query->from('AnSimNavTrans');
$query->where('op_modelo_id = ?', $idModelo);
$query->orderBy('med_sim DESC');
$query->limit('3');
$result = $query->execute();
return $result->toArray();
}
}
View:
print_r($modelos_similares_nav);
Array ( [0] => Array ( [op_modelo_id] => 1 [cod_mod] => 11 [med_sim] => 0.7 ) )
As you can see in view, when I get the array of the query, this only show the last record.
What its wrong with this?
First:
It feel strange to me that OpModeloTable->getModeloSimNav
make a query to AnSimNavTrans
. Why not using AnSimNavTransTable
to do that?
Second:
If you want an array, hydrate with array instead of using toArray
:
$result = $query->execute(array(), Doctrine_Core::HYDRATE_ARRAY);.
Test with this last thing. I don't know why this way is working when your solution doesn't. When taking a look at the code, toArray comment's function says :
Mimics the result of a $query->execute(array(), Doctrine_Core::HYDRATE_ARRAY);
So, maybe it's better to not mimics this function but actually use it..
By the way, using hydration like I told you is better than using toArray
. The idea behind using a hydration with array is that doctrine won't hydrate objects (and all dependencies). This will save you lots of time when the page is loaded.
When using toArray
, you will hydrate with objects and after convert it to array. You waste lots of time.
It's the first step in a performance issue fixing.