2016-11-17 58 views
0

是否有可能找到附加條件的cakephp,或者我們可以將sum()字段與條件變量相乘? 我不知道,讓你們知道我在問什麼是一個最好的問題。所以我給出了關於問題的更多細節。我可以用一些技巧在單個數組中找到類似的數據。但我無法找到這個。請幫我或給我一個想法做這樣的工作。謝謝。Cakephp sum()與乘法

使用CakePHP 2.6

我想找到這裏第一個表數據..

$incentiveweekly=$this->Incentive->find('all', 
      array('conditions' => 
       array( 
        "AND"=>array(
         "Incentive.fromdate >=" => $from, 
         "Incentive.todate <=" => $to 
        ) 
       ) 
      ) 
    ); 

    according to number of rows. I have to find another table 
Here is the result of above find condition. 
       Array 
      (
       [Incentive] => Array 
        (
         [id] => 2 
         [target1] => 3000 
         [price1] => 1.5 
         [target2] => 6000 
         [price2] => 2.5 
         [target3] => 8000 
         [price3] => 3.5 
         [formonth] => 
         [type] => 1 
         [fromdate] => 2016-11-13 
         [todate] => 2016-11-21 
         [updatedby] => 1 
         [created] => 2016-11-15 23:57:21 
         [modified] => 2016-11-15 23:57:21 
        ) 

      ) 
      Array 
      (
       [Incentive] => Array 
        (
         [id] => 3 
         [target1] => 3000 
         [price1] => 1.5 
         [target2] => 6000 
         [price2] => 2.5 
         [target3] => 8000 
         [price3] => 3.5 
         [formonth] => 
         [type] => 1 
         [fromdate] => 2016-11-24 
         [todate] => 2016-11-28 
         [updatedby] => 1 
         [created] => 2016-11-15 23:57:21 
         [modified] => 2016-11-15 23:57:21 
        ) 

      ) 

現在我想根據數組記錄的號碼找到了陣。

$byweek=array(); // Storing Customer data by Target dates in array()    
    foreach ($incentiveweekly as $weekly){ 
     print_r($weekly); 
     $target3=$weekly['Incentive']['target3']; 
     $target2=$weekly['Incentive']['target2']; 
     $target1=$weekly['Incentive']['target1']; 
     $price3=$weekly['Incentive']['price3']; 
     $price2=$weekly['Incentive']['price2']; 
     $price1=$weekly['Incentive']['price1']; 
     $byweek[]=$customers=$this->Customer->find('all',array(
      'fields'=>array(
      'SUM(amount) AS amount', 
      'created_by' 
      ), 
      'group' => 'Customer.created_by', 
      'conditions' => array(
          "AND" =>array(
           "Customer.created >=" => $weekly['Incentive']['fromdate'], 
           "Customer.created <=" => $weekly['Incentive']['todate'] 
           ) 
          ), 
      'recursive'=>-1 ) 
        ); 
     //print_r($byweek); 
     } 

我越來越像結果...

  Array 
      (
       [0] => Array 
        (
         [0] => Array 
          (
           [Customer] => Array 
            (
             [created_by] => 3 
            ) 

          ) 

        ) 

      ) 
      Array 
      (
       [0] => Array 
        (
         [0] => Array 
          (
           [Customer] => Array 
            (
             [created_by] => 3 
            ) 

          ) 

        ) 

       [1] => Array 
        (
         [0] => Array 
          (
           [Customer] => Array 
            (
             [created_by] => 1 
            ) 

          ) 

         [1] => Array 
          (
           [Customer] => Array 
            (
             [created_by] => 2 
            ) 

          ) 

        ) 

      ) 

但我要的是量將與在那裏我使用三元運算符,如果其他條件繁殖。

$value[0]['amount']>=$valuem['Incentive']['target3']?"Target Third":($value[0]['amount']>=$valuem['Incentive']['target2']?"Target Second":($value[0]['amount']>=$valuem['Incentive']['target1']?"Target First":"None")) 

查找細節的主要目的是。我想創建一個獎勵總額,其總銷售額應與給定的目標金額相匹配。如果目標1達到一個匹配,那麼激勵將是總金額* price1,與目標2和目標3相同。我在哪裏使用Tenantry運營商。目標價格應該乘以(通過條件)與相同的查找條件數據。

我搜索谷歌和堆棧溢出,但無法找到它的解決方案。不過,我很感謝堆棧溢出以及你能夠發現大量技巧和解決方案的全部內容。

回答

0

感謝Oldskool該解決方案Virtualfields with If Condition

最終我,通過此代碼得到我的解決方案波紋管。我正在按預期得到結果。 :)

  $this->Customer->virtualFields=array(
        'incentive' => "if(SUM(Customer.amount)>=$target3,(SUM(Customer.amount))*$price3,if(SUM(Customer.amount)>=$target2,(SUM(Customer.amount))*$price2,if(SUM(Customer.amount)>=$target1,(SUM(Customer.amount))*$price1,0)))", 
      ); 

現在我可以使用Virtualfied獲取激勵值。

0

我還沒有得到確切雖然你可以通過下面的解決方案按我的想法嘗試,

你需要使用的時候接近MYSQL,

$incenve = 'CASE WHEN amount>5000 THEN "FIRST Target" WHEN amount>3000 THEN "Second Target" ELSE 0 ' 

,並在上面查詢中的字段

'incentive' => $incentive 
+0

其實我想把總金額與獎勵金額相乘。但是激勵總額取決於總額如果(總金額> =目標3金額之和,那麼激勵金額將是價格3),但如果(總金額> =目標2金額之和,那麼激勵金額將是價格2)。 – SAHAR

+0

我認爲你幾乎接近我的解決方案。但我想知道我的建議代碼在哪裏。搜索查詢內部或搜索查詢外部。如果我把它放在搜索查詢之外。它找不到總金額。 – SAHAR