【问题标题】:JOIN works in SQL not in DrupalJOIN 适用于 SQL 而不是 Drupal
【发布时间】:2014-10-05 06:48:15
【问题描述】:

我正在尝试为 Drupal 7.x 制作一个模块。在某个时候,我想使用 sql 查询 (JOIN)。当我在 MYSQL 中尝试查询时,它可以工作。但是当我想在 Drupal 中尝试时,数组是空的。

所以我猜想sql查询和drupal查询是有区别的(可能实现方式不同)。

SQL 查询

SELECT * FROM friends 
INNER JOIN users 
ON friends.uid=users.uid

Drupal 实施

    function project_myfriends(){
    // Use database API to retrieve tasks
    $query = db_select('friends', 'f');

    $query->join('users', 'u', 'f.uid = u.uid'); // JOIN

    $query->fields('u', array('name'))  
          ->execute();
    return $query;
    }

   /**
    * Implements hook_block_view().
    */
    function project_block_view($delta = ''){
    switch ($delta) {
        case 'project':
            $block['subject'] = t('My Friends');
            // Use our custom function to retrieve data
            $result = project_myfriends();

            $items = array();

            var_dump($result);
            foreach($result as $friend){
                $items[] = array(
                    'data' => $friend->name,            
                );
            }
            // No tasks
                if (empty($items)) {
                    $block['content'] = t('No friends.');
                }
                else {
                    // Pass data trough theme function
                    $block['content'] = theme('item_list', array(
                        'items' => $items));
                }
            }
        return $block;
    }

提前谢谢

【问题讨论】:

    标签: mysql sql drupal drupal-7


    【解决方案1】:

    您忘记获取结果查询:

    $result = project_myfriends()->execute()->fetchAll();
    var_dump($result);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-05
      • 2012-02-26
      相关资源
      最近更新 更多