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 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题
  • ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)
  • ¥20 关于#stm32#的问题:STM32串口发送问题,偶校验(even),发送5A 41 FB 20.烧录程序后发现串口助手读到的是5A 41 7B A0
  • ¥15 C++map释放不掉
  • ¥15 Mabatis查询数据
  • ¥15 想知道lingo目标函数中求和公式上标是变量情况如何求解
  • ¥15 关于E22-400T22S的LORA模块的通信问题
  • ¥15 求用二阶有源低通滤波将3khz方波转为正弦波的电路