查询数据库然后使用结果进行更多查询

时间:2017-10-24 19:41:06

标签: php database

请花点时间阅读全文。我知道这很多但是我不能简短地描述它。

使用下面的代码,我可以获得分配给Joomla 3中登录用户的用户组ID。

$user_  = JFactory::getUser();
$db     = JFactory::getDBO();
foreach($user_->groups as $group){
    $query  = 'SELECT id FROM #__usergroups';
    $query .= ' WHERE id = ' . $group;
    $db->setQuery( $query );
    echo $db->loadResult() . "<br>";
}

大多数用户属于多个群组,上述代码还会返回用户所连接的所有用户群ID的列表。

原因

我需要列出分配给当前登录用户的所有项目。

获得该方法

  1. 我需要获取用户组ID。 (使用上面的代码实现,可以使用ID 20

  2. 作为示例
  3. 然后使用此用户组ID(UGID)在view access table列中搜索视图访问ID(VAID)&#34;规则&#34;包含UGID

  4. 然后在project table中搜索列&#34;访问&#34;匹配此VAID。

  5. 返回所有项目标题的列表。

  6. 为什么要问这里

    我不习惯使用数据库,而且非常适合我的联盟。我在第2步陷入困境,因为我的代码可以返回一个ID或倍数。 当它返回多个ID时,我需要将每个ID检查到view access table

    表格设置

    用户组表

    以下是这个表的构建方式(默认的Joomla 3表。不能改变它!):

    CREATE TABLE IF NOT EXISTS `#_usergroups` (
      `id` int(10) unsigned NOT NULL COMMENT 'Primary Key',
      `parent_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Adjacency List Reference Id',
      `lft` int(11) NOT NULL DEFAULT '0' COMMENT 'Nested set lft.',
      `rgt` int(11) NOT NULL DEFAULT '0' COMMENT 'Nested set rgt.',
      `title` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT ''
    ) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    
    INSERT INTO `y39kd_usergroups` (`id`, `parent_id`, `lft`, `rgt`, `title`) VALUES
    (17, 14, 24, 25, 'Velthoven'),
    (20, 14, 6, 7, 'Bouwman');
    

    查看访问权限表

    以下是这个表的构建方式(默认的Joomla 3表。不能改变它!):

    CREATE TABLE IF NOT EXISTS `#_viewlevels` (
      `id` int(10) unsigned NOT NULL COMMENT 'Primary Key',
      `title` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
      `ordering` int(11) NOT NULL DEFAULT '0',
      `rules` varchar(5120) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'JSON encoded access control.'
    ) ENGINE=InnoDB AUTO_INCREMENT=33 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    
    A little bit of dummy content (here you can see that the Rules contains multiple ID's)
    
    INSERT INTO `y39kd_viewlevels` (`id`, `title`, `ordering`, `rules`) VALUES
    (13, 'Velthoven', 0, '[17,15,7,8]'),
    (18, 'Bouwman', 0, '[20,15,7,8]');
    

    项目表

    以下是这个表的构建方式(默认的Joomla 3 projectfork表。无法更改此内容!):

    CREATE TABLE IF NOT EXISTS `#_pf_projects` (
      `id` int(10) unsigned NOT NULL COMMENT 'Project ID',
      `asset_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'FK to the #__assets table',
      `catid` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Category ID',
      `title` varchar(128) NOT NULL COMMENT 'Project title',
      `alias` varchar(128) NOT NULL COMMENT 'Title alias. Used in SEF URL''s',
      `description` text NOT NULL COMMENT 'Project description',
      `created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'Project creation date',
      `created_by` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Project owner',
      `modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'Project modify date',
      `modified_by` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Last user to modify the project',
      `checked_out` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'User who is currently editing the project',
      `checked_out_time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'Check-out date and time',
      `attribs` text NOT NULL COMMENT 'Project attributes in JSON format',
      `access` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Project ACL access level ID',
      `state` tinyint(3) NOT NULL DEFAULT '0' COMMENT 'Project state: 1 = Active, 0 = Inactive, 2 = Archived, -2 = Trashed',
      `start_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'Project start date',
      `end_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'Project end date'
    ) ENGINE=InnoDB AUTO_INCREMENT=56 DEFAULT CHARSET=utf8 COMMENT='Stores Projectfork project data';
    
    INSERT INTO `#_pf_projects` (`id`, `asset_id`, `catid`, `title`, `alias`, `description`, `created`, `created_by`, `modified`, `modified_by`, `checked_out`, `checked_out_time`, `attribs`, `access`, `state`, `start_date`, `end_date`) VALUES
    (26, 1274, 42, 'http://www.ls.velthovenbv.nl', 'http-www-ls-velthovenbv-nl', '', '2017-10-04 14:10:32', 439, '2017-10-04 14:11:30', 439, 994, '2017-10-18 13:32:22', '{"website":false,"email":"","phone":"","repo_dir":72,"project_color":"","milestone_color":"","tasklist_color":"","task_color":"","currency_code":"","currency_sign":"","currency_position":"","decimal_delimiter":"","thousands_delimiter":""}', 13, 1, '0000-00-00 00:00:00', '0000-00-00 00:00:00'),
    (55, 4712, 57, 'http://www.ls.bouwmanbv.nl', 'http-www-ls-bouwmanbv-nl', '<p>List with Basic Milestones and Tasks to use for all Commercial Projects</p>', '2017-10-19 16:10:57', 994, '2017-10-19 16:12:48', 439, 0, '0000-00-00 00:00:00', '{"website":false,"email":"","phone":"","repo_dir":102,"project_color":"","milestone_color":"","tasklist_color":"","task_color":"","currency_code":"","currency_sign":"","currency_position":"","decimal_delimiter":"","thousands_delimiter":""}', 18, 1, '0000-00-00 00:00:00', '0000-00-00 00:00:00'),
    (155, 4712, 57, 'http://www.ls.serious.nl', 'http-www-ls-serious-nl', '<p>List with Basic Milestones and Tasks to use for all Commercial Projects</p>', '2017-10-19 16:10:57', 994, '2017-10-19 16:12:48', 439, 0, '0000-00-00 00:00:00', '{"website":false,"email":"","phone":"","repo_dir":102,"project_color":"","milestone_color":"","tasklist_color":"","task_color":"","currency_code":"","currency_sign":"","currency_position":"","decimal_delimiter":"","thousands_delimiter":""}', 18, 1, '0000-00-00 00:00:00', '0000-00-00 00:00:00'),
    (255, 4712, 57, 'http://www.ls.another.nl', 'http-www-ls-another-nl', '<p>List with Basic Milestones and Tasks to use for all Commercial Projects</p>', '2017-10-19 16:10:57', 994, '2017-10-19 16:12:48', 439, 0, '0000-00-00 00:00:00', '{"website":false,"email":"","phone":"","repo_dir":102,"project_color":"","milestone_color":"","tasklist_color":"","task_color":"","currency_code":"","currency_sign":"","currency_position":"","decimal_delimiter":"","thousands_delimiter":""}', 18, 1, '0000-00-00 00:00:00', '0000-00-00 00:00:00'),
    (355, 4712, 57, 'http://www.ls.something.nl', 'http-www-ls-something-nl', '<p>List with Basic Milestones and Tasks to use for all Commercial Projects</p>', '2017-10-19 16:10:57', 994, '2017-10-19 16:12:48', 439, 0, '0000-00-00 00:00:00', '{"website":false,"email":"","phone":"","repo_dir":102,"project_color":"","milestone_color":"","tasklist_color":"","task_color":"","currency_code":"","currency_sign":"","currency_position":"","decimal_delimiter":"","thousands_delimiter":""}', 18, 1, '0000-00-00 00:00:00', '0000-00-00 00:00:00');
    

    代码的预期结果

    我希望从“获得该方法的方法”一章中描述的代码中获得以下结果:

    1. 用户组ID 20
    2. 查看访问ID 18
    3. 项目ID 55,155,255,355 =&gt;获得标题
    4. http://www.ls.bouwmanbv.nlhttp://www.ls.serious.nlhttp://www.ls.another.nlhttp://www.ls.something.nl
    5. 当其他用户登录时,它会显示:

      1. 用户组ID 17
      2. 查看访问ID 13
      3. 项目ID 26 =&gt;获得标题
      4. http://www.ls.velthovenbv.nl
      5. 到目前为止我的尝试

        在这里,我发布了有关我尝试的事情的信息,这会让专业人士大笑,看到我做了愚蠢的事情。

        foreach($user_->groups as $group){
            $query  = 'SELECT id FROM #__usergroups';
            $query .= ' WHERE id = ' . $group;
            $db->setQuery( $query );
            $ViewAccessquery  = 'SELECT id FROM #__viewlevels';
            $ViewAccessquery .= ' WHERE rules = ' . $group;
            $db->setQuery( $ViewAccessquery );
            echo $db->loadResult() . "<br>";
        }
        

        下面从cmorrissey提供的答案我越来越近了。到目前为止它还没有工作,因为下面的代码存在问题。

          foreach($user_->groups as $group){
            $group_query  = 'SELECT id FROM #__usergroups';
            $group_query .= ' WHERE id = ' . $group;
            $db->setQuery( $group_query );
            echo $db->loadResult($group_query);
          }
          $query = $db->getQuery(true);
          $query->select('#__pf_projects'.'title');
          $query->from('#__pf_projects');
          $query->join('inner', $db->quoteName('#__viewlevels') . ' ON (' . $db->quoteName('#__viewlevels'.'id') . ' = ' . $db->quoteName('#__pf_projects'.'access') . ')');
          $query->join('inner', $db->quoteName('#__usergroups') . ' ON (' . $db->quoteName('#__usergroups'.'title') . ' = ' . $db->quoteName('#__viewlevels'.'title') . ') AND FIND_IN_SET("#__usergroups"."id", :group_ids)');
          //$query->where('pdt'.'product_id' = 68);
          $db->setQuery($query);
          echo $db->loadResult($query);
        

        问题在于这一部分:

        AND FIND_IN_SET("#__usergroups"."id", :group_ids)');

        我尝试移动它但在编辑器中遇到语法错误。当代码原样时,我收到此错误:

        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 '."id", :group_ids)' at line 4

        我知道group_ids尚未定义,但我不知道我是否可以将其更改为group_query

        PS 所有数据都是假数据!上述文本中的所有实体都是虚构的,与同名的现有实体无关。

2 个答案:

答案 0 :(得分:0)

你可以这样做。

SELECT `#_pf_projects`.`title`
    FROM `#_pf_projects`
        INNER JOIN `#_viewlevels` ON `#_viewlevels`.`id` = `#_pf_projects`.`access`
        INNER JOIN `#_usergroups` ON `#_usergroups`.`title` = `#_viewlevels`.`title` AND FIND_IN_SET(`#_usergroups`.`id`, :group_ids)
    WHERE 1

你的:group_ids将是一个以逗号分隔的列表,即&#39; 20,18&#39;或者只是&#39; 20&#39;这样你就不需要那个其他循环...或者你可以更进一步加入更多东西,只需使用用户ID来提取所有内容。

答案 1 :(得分:0)

显然,Joomla有一些非常好的功能可以让这个结果变得更容易。我需要另一个SO问题来实现结果。

Removing empty spaces and BR from string in echo

但我也会把最后的代码放在这里。

$user_  = JFactory::getUser();
$db     = JFactory::getDBO();
$levels = JAccess::getAuthorisedViewLevels($user->id);
foreach($levels as $key => $level)
{
  $query  = 'SELECT title FROM #__pf_projects';
  $query .= ' WHERE access = ' . $level;
  $db->setQuery($query);
  $projectlist = '<div class="project">'.$db->loadResult($query).'</div>';
  echo $projectlist;
}

创建如下列表:

http://www.domain1.com
http://www.domain5.com
http://www.domain23.com
http://www.domain65.com
http://www.domain213.com

在我们的案例中是项目名称。