我想将SQL转换为DQL

时间:2016-04-19 04:39:54

标签: mysql doctrine-orm dql symfony-2.7

我想将一些SQL语句翻译成DQL的任何帮助吗? 我有用户实体

-------user--------
   id 
   username
   email
   password 
   ...
--------------------

和朋友实体有2个属性friend_one和friend_two引用用户

------friend---- 

   id 
   friend_one
   friend_two
   statu

--------------

所以我的SQL语句是

UPDATE friend
SET status="1"
WHERE 
(friend_one="$user_id" OR friend_two="$user_id") 
AND 
(friend_one="$friend_id" OR friend_two="$friend_id")
SELECT 'friend_one','friend_two','status' FROM friend
WHERE
(friend_one="$user_id" OR friend_two="$user_id")
AND
(friend_one="$friend_id" OR friend_two="$friend_id")
SELECT F.statu, U.username, U.id
FROM user U, friend F
WHERE
CASE
WHEN F.friend_one = '$user_id'
THEN F.friend_two = U.user_id
WHEN F.friend_two= '$user_id'
THEN F.friend_one= U.user_id
END
AND 
F.statu='1'

任何帮助请:/

2 个答案:

答案 0 :(得分:0)

很好,但到目前为止你有没有尝试过任何东西?如果您在问题中包含当前代码,那就太好了。

我建议您点击以下这些查询: (免责声明:请注意它们非常原始且尚未经过测试)

/* For update */
$result = $db->executeUpdate('UPDATE friend AS f SET f.status = "1" WHERE (f.friend_one = ? OR f.friend_one = ?) AND (f.friend_one = ? OR f.friend_two = ?)', 
            array($user_id, $user_id, $friend_id, $friend_id),
            array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
            );


/* For first select */
 $stmt = $db->executeQuery('SELECT f.friend_one, f.friend_two, f.status from friend AS f 
            WHERE (f.friend_one = ? OR f.friend_one = ?) AND (f.friend_one = ? OR f.friend_two = ?)', 
            array($user_id, $user_id, $friend_id, $friend_id),
            array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
            );

$result = $stmt->fetchAll();


/* For second select */     
 $stmt = $db->executeQuery('SELECT f.status, u.username, u.id from friend AS f 
        INNER JOIN user as u ON u.user_id =
        CASE WHEN f.friend_one = ? THEN f.friend_two WHEN f.friend_two = ? THEN f.friend_one END
        AND f.status = "1"', 
        array($user_id, $user_id),
        array(\PDO::PARAM_INT, \PDO::PARAM_INT)
        );

$result = $stmt->fetchAll();

答案 1 :(得分:0)