doulongdan2264 2014-12-11 08:14
浏览 199
已采纳

Joomla数据库以及如何提取数据

i have kind of hacked away at a joomla component, it works well with one table, but i was looking at having more than one table, but i cant seem to get it to draw from 2 tables. ie one table will have lets say article information, the other table will have other data relevant to the articles and their id's. Any ideas on how i can do this

  • 写回答

1条回答 默认 最新

  • donglaohua1671 2014-12-12 18:38
    关注

    Selecting Records from Multiple Tables

    Using the JDatabaseQuery's join methods, you can select records from multiple related tables. The generic "join" method takes two arguments; the join "type" (inner, outer, left, right) and the join condition. In the following example you will notice that we can use all of the keywords we would normally use if we were writing a native SQL query, including the AS keyword for aliasing tables and the ON keyword for creating relationships between tables. Also note that the table alias is used in all methods which reference table columns (I.e. select, where, order).

    // Get a db connection.
    $db = JFactory::getDbo();
    
    // Create a new query object.
    $query = $db->getQuery(true);
    
    // Select all articles for users who have a username which starts with 'a'.
    // Order it by the created date.
    // Note by putting 'a' as a second parameter will generate `#__content` AS `a`
    $query
        ->select($db->quoteName(array('a.*', 'b.username', 'b.name')))
        ->from($db->quoteName('#__content', 'a'))
        ->join('INNER', $db->quoteName('#__users', 'b') . ' ON (' . $db->quoteName('a.created_by') . ' = ' . $db->quoteName('b.id') . ')')
        ->where($db->quoteName('b.username') . ' LIKE \'a%\'')
        ->order($db->quoteName('a.created') . ' DESC');
    
    // Reset the query using our newly populated query object.
    $db->setQuery($query);
    
    // Load the results as a list of stdClass objects (see later for more options on retrieving data).
    $results = $db->loadObjectList();
    

    Even if you create your components using Component Creator you still have to do this kind of stuff manually.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗