duanmao1975 2016-02-01 04:25
浏览 7
已采纳

cakephp访问字段在具有关系模型的模型中搜索

Excuse me everybody that have a great skill in cakephp, i'am beginer in cakephp, i don't know how to access field which i want to use the value.

This is the array data show from controller

Array
(
`[Claim] => Array`
        (
            [id] => 121
            [name] => Gwoo the Kungwoo
            [created] => 2007-05-01 10:31:01
        )
    [ClaimDetail] => Array
        (
            [0] => Array
                (
                    [id] => 123
                    [claim_id] => 121
                    [title] => On Gwoo the Kungwoo
                    [body] => The Kungwooness is not so Gwooish
                    [date] => 2006-05-01 10:31:01
                )
            [1] => Array
                (
                    [id] => 124
                    [claim_id] => 121
                    [title] => More on Gwoo
                    [body] => But what of the 'Nut?'
                    [date] => 2006-05-01 10:41:01
                )
        )
);

in the controller i make the conditions,

$conditions = array(
                    'ClaimDetail.date between ? AND ?'  => array(
                        $this->request->query['start_date'],
                        $this->request->query['end_date']
                    ),
                    'Claim.delete_flag' => 0
                );

but cakephp show the error, unknown field,

this is the find,

$claim = $this->Claim->find('all', array(
                'conditions'    => $conditions
            ));



Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'ClaimDetail.date' in 'where clause'


there're any other ways for me to make conditions like what i want,

thankss before and after... T_T stuck  one week 

  • 写回答

1条回答 默认 最新

  • douqiu0796 2016-02-01 04:59
    关注

    The problem is you're applying the condition on an associated model and not the original model. One way to solve this is to join the two tables.

       $joins   =   array(
                     array(
                        'table' => 'claim_details',
                        'alias' => 'ClaimDetail',
                        'type' => 'INNER',
                        'conditions' => array(
                             'Claim.id = ClaimDetail.claim_id',
                             'ClaimDetail.date between "'.$this->request->query['start_date'].'" AND "'.$this->request->query['end_date'].'" '
                         )
                     )
                );
    
       $claim = $this->Claim->find('all', array(
               'joins'       => $joins
            ));
    

    This should give you what you're looking for.

    Peace! xD

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

报告相同问题?

悬赏问题

  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。