douliangbian7323 2013-02-14 17:40
浏览 51
已采纳

Doctrine 1不会在CodeIgniter 2上正确返回数据数组

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?

  • 写回答

1条回答 默认 最新

  • duancaishi1897 2013-02-17 09:01
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog