2014-11-14 70 views
0

我已經調用actionIndex()Yii的範圍與工會

時,它顯示了在表中Select * FROM store

的數據我怎麼會編輯Scope這種電流模式稱爲store?還是有另一種方法來做到這一點,而不使用範圍?

SELECT j.* FROM 
    (
     (
      SELECT * 
      FROM `make` AS v 
      JOIN `ads` AS a 
      ON (v.id=a.make_code) 
      GROUP BY v.make 
     ) 
     UNION 
     (
      SELECT * 
      FROM `store` AS d 
      JOIN `make` AS v 
      ON (v.id=d.make) 
      GROUP BY d.make 
     ) 
    ) AS j GROUP BY j.make 

我想這一點,但得到一個錯誤

public function Scope(){ 
    return array(
     '_with'=>array(
      'alias' => 'd', 
      'select'=>array('j.* 
          (
           ( 
            SELECT * 
            FROM `make` AS v 
            JOIN `ads` AS a 
            ON (v.id=a.make_code) 
            GROUP BY v.make 
           ) 
           UNION 
           (
            SELECT * 
            FROM `store` AS d 
            JOIN `make` AS v 
            ON (v.id=d.make) 
            GROUP BY d.make 
           ) 
          ) AS j' 
      ), 
      'group'=>'j.make', 
     ) 
    ); 
} 

這裏是錯誤消息

CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: 
Syntax error or access violation: 1064 You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version for the right syntax to use near 
'(
    (
     SELECT *' at line 2. The SQL statement executed was: SELECT j.* 
      (
       (
        SELECT * 
        FROM `make` AS v 
        JOIN `ads` AS a 
        ON (v.id=a.make_code) 
        GROUP BY v.make 
       ) 
       UNION 
       (
        SELECT * 
        FROM `store` AS d 
        JOIN `make` AS v 
        ON (v.id=d.make) 
        GROUP BY d.make 
       ) 
      ) AS j FROM `store` d WHERE d.pending!="1" GROUP BY j.make sq 
+0

此查詢正常工作嗎? – Dinistro

+0

@Dinistro是它的確 – user2636556

回答

0

問題是你沒有設置你的from。你也可以在from之外創建一個sql視圖,然後使用它。它使你的代碼更清潔。

public function Scope(){ 
    return array(
     '_with'=>array(
      'alias' => 'd', 
      'select'=>array('j.*'); 
      'from' => '(
           ( 
            SELECT * 
            FROM `make` AS v 
            JOIN `ads` AS a 
            ON (v.id=a.make_code) 
            GROUP BY v.make 
           ) 
           UNION 
           (
            SELECT * 
            FROM `store` AS d 
            JOIN `make` AS v 
            ON (v.id=d.make) 
            GROUP BY d.make 
           ) 
          ) AS j', 
      'group'=>'j.make', 
     ) 
    ); 
}