duanbi3786 2014-11-06 10:52
浏览 23
已采纳

使用Cakephp 1.3中的find('list')和自定义字段

I'm working in a cakephp 1.3 project. I have a problem retrieving some data from my database using find('list'). Before my last change I had this code:

$listaHabilidades = $this->Habilidad->find('list',array('fields'=>array('id','habilidad'),'order'=>array('habilidad'))));

This command gives me the next array:

Array
(
    [40] =>  Bicicleta
    [42] =>  enganches de poleas
    [28] =>  Escalada
    [43] =>  transfer
    [41] => 4x4
    [53] => Administración Linux
    [72] => Ángeles
    [59] => Baile Flamenco
    [57] => Baloncesto
    [39] => Barranquismo
    [66] => Cante Flamenco
    [30] => Conducción
    [52] => Consola Linux
    [80] => cuarta prueba
    [75] => Demonios
    [84] => Esgrima
    [58] => Futbol
    [76] => Limpiar
    [77] => Limpiar2
    [54] => Linux
    [27] => Montañismo
    [60] => MS Office
    [65] => Natación
    [45] => Patinaje
    [56] => PC
    [78] => probar
    [44] => Programación
    [82] => Protocolo
    [81] => quinta prueba
    [63] => Tenis
    [79] => tercera prueba
    [83] => Triatlón
    [55] => W8
    [51] => XP
    [64] => Zapateado
)

But I had a problem because the data can have spaces at the begining of the field 'habilidad' and I want to get the data without the and order them also without them. So I changed the command to:

$listaHabilidades = $this->Habilidad->find('list',array('fields'=>array('id','trim(Habilidad.habilidad)'),'order'=>array('trim(Habilidad.habilidad)'))));

It gives me the next array:

Array
(
    [41] => 
    [53] => 
    [72] => 
    [59] => 
    [57] => 
    [39] => 
    [40] => 
    [66] => 
    [30] => 
    [52] => 
    [80] => 
    [75] => 
    [42] => 
    [28] => 
    [84] => 
    [58] => 
    [76] => 
    [77] => 
    [54] => 
    [27] => 
    [60] => 
    [65] => 
    [45] => 
    [56] => 
    [78] => 
    [44] => 
    [82] => 
    [81] => 
    [63] => 
    [79] => 
    [43] => 
    [83] => 
    [55] => 
    [51] => 
    [64] => 
)

How can I get the list trimmed (without loops)?

  • 写回答

2条回答 默认 最新

  • douchun5976 2014-11-06 11:11
    关注

    Can you add a virtual field to your model:

    public $virtualFields = array(
        't_habilidad' => 'LTRIM(Habilidad.habilidad)'
    );
    

    Then use

    $listaHabilidades = $this->Habilidad->find('list', array('fields'=>array('id','habilidad'), 'order' => array('t_habilidad'))));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器