2017-01-22 73 views
0

我在views_php創建以下代碼:在views_php一個db_query使用ENTITY_ID(Drupal的7)

$userseeking = db_query("SELECT field_seeking_tid FROM {field_data_field_seeking} WHERE entity_id = 6")->fetchAll(PDO::FETCH_ASSOC); 
$userseekingcount = count($userseeking); 
return $userseekingcount 

在上面的例子中,我使用的 '6' 的特定值ENTITY_ID。我收到此查詢的預期結果。

但是,entity_id需要是取決於登錄用戶的配置文件ID(連接到其用戶ID)的變量。

因此我引入3行來從當前用戶檢索PID($ currentpid)。我已經確認這3行返回$ currentpid值6.當我修改db_query來選擇entity_id = $ currentpid的值時,雖然它們應該產生相同的結果,但看起來是失敗的,請參閱下面的代碼:

global $user; 
$currentuser = $user->uid; 
$currentpid = db_query('SELECT pid FROM {profile} WHERE uid = ' . $currentuser . ' limit 1')->fetchField(); 

$userseeking = db_query("SELECT field_seeking_tid FROM {field_data_field_seeking} WHERE entity_id = ' . $currentpid . '")->fetchAll(PDO::FETCH_ASSOC); 
$userseekingcount = count($userseeking); 
return $userseekingcount 

我在濫用如何在此查詢中引用entity_id嗎?

感謝您的幫助!

PS這可能不是最乾淨的代碼,所以我對先進的道歉! :)

回答

0

事實證明,在上面的例子中有一種語法的誤用。特別是我使用「和」創建$ userseeking時

希望其他人能夠從我的新秀錯誤:)

改正的代碼是學習:

global $user; 
$currentuser = $user->uid; 
$currentpid = db_query('SELECT pid FROM {profile} WHERE uid = ' . $currentuser . ' limit 1')->fetchField(); 

$userseeking = db_query('SELECT field_seeking_tid FROM {field_data_field_seeking} WHERE entity_id = ' . $currentpid . '')->fetchAll(PDO::FETCH_ASSOC); 
$userseekingcount = count($userseeking); 
return $userseekingcount