douliangbian7323
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?

图片转代码服务由CSDN问答提供 功能建议

当我得到一张桌子的数据时,我遇到了一些麻烦。

我正在使用Doctrine 1 + Codeigniter,问题是数据只获得最后一个注册表。

我没有在这些表之间定义任何关系(OpModelos和 AnSimNavTrans),这是风景:

控制器:

 类Modelos扩展CI_Controller {
 
公共功能视图($ idModelo)  ){
 $ aData ['site_description'] ='AutoDato'; 
 $ aData ['site_keywords'] ='keywords'; 
 $ aData ['site_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  ); 
} 
} 
   
 
 

数据表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 
   
 
 

模型

 <  code> class OpModeloTable扩展了Doctrine_Table {
 
公共函数getModeloSimNav($ idModelo){
 $ query = Doctrine_Query :: create(); 
 $ query-&gt; from('AnSimNavTrans'); 
 $ query-  &gt; where('op_modelo_id =?',$ idModelo); 
 $ query-&gt; orderBy('med_sim DESC'); 
 $ query-&gt; limit('3'); 
 
 $ result =  $ query-&gt; execute(); 
 
返回$ result-&gt; toArray(); 
} 
} 
   
 
 

查看:

  print_r($ modelos_similares_nav); 
 
Array([0] =&gt; 数组([op_modelo_id] =&gt; 1 [cod_mod] =&gt; 11 [med_sim] =&gt; 0.7))
   
 
 

正如您在视图中看到的那样 我得到了查询的数组,这只显示了最后一条记录。

这有什么不妥?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duancaishi1897
    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.

    点赞 评论

相关推荐