0

我见过其他类似的问题,但没有一个对我想做的事情真正有帮助。

我有三张桌子;sites,user_sitessite_domains. 我想从user_sitessite_domains表中查询一个站点以及用户和域的列表。以下是表格示例:

数据库站点

+------+-------------+---------------------+--------------+
| id   | site_name   | site_options        | created_on   |
+------+-------------+---------------------+--------------+
| 1    | name1       | {serialized data}   | 10/10/12     |
+------+-------------+---------------------+--------------+
| 2    | name2       | {serialized data}   | 24/10/12     |
+------+-------------+---------------------+--------------+
| 3    | name3       | {serialized data}   | 08/09/12     |
+------+-------------+---------------------+--------------+
| 4    | name4       | {serialized data}   | 03/01/12     |
+------+-------------+---------------------+--------------+
| 5    | name5       | {serialized data}   | 10/12/11     |
+------+-------------+---------------------+--------------+

db.user_sites

+------+-----------+-----------+-----------+
| id   | user_id   | site_id   | default   |
+------+-----------+-----------+-----------+
| 1    | 1         | 1         | 0         |
+------+-----------+-----------+-----------+
| 2    | 3         | 2         | 0         |
+------+-----------+-----------+-----------+
| 3    | 1         | 3         | 0         |
+------+-----------+-----------+-----------+
| 4    | 10        | 1         | 0         |
+------+-----------+-----------+-----------+
| 5    | 5         | 1         | 1         |
+------+-----------+-----------+-----------+

db.site_domains

+------+-----------+-------------------+
| id   | site_id   | site_domain       |
+------+-----------+-------------------+
| 1    | 1         | www.domain1.com   |
+------+-----------+-------------------+
| 2    | 1         | sub.domain2.com   |
+------+-----------+-------------------+
| 3    | 3         | www.domain3.com   |
+------+-----------+-------------------+
| 4    | 2         | www.domain4.com   |
+------+-----------+-------------------+
| 5    | 3         | www.domain5.com   |
+------+-----------+-------------------+

当它完成时。我希望能够填充这样的对象:

array (
    [1] => array (
        [site_name]  => 'Name1',
        [created_on] => '10/10/12',
        [site_options]    => {serialized data},
        [domains]    => array (
                            [0] => 'www.domain1.com',
                            [1] => 'www.domain2.com'
                        ),
        [users]      => array (
                            [0] => 1,
                            [1] => 10,
                            [2] => 5
                        )
    ),
    [2] => array (
        [site_name] => 'Name2',
        [created_on] => '24/10/12',
        [site_options] => {serialized data},
        [domains]    => array (
                            [0] => 'www.domain4.com'
                        ),
        [users]      => array (
                            [0] => 3
                        )
    )
)

....等等。

我正在使用 Codeigniter 创建我的应用程序,但如果我只知道它的 SQL,我可以将其转换为使用 Codeigniter。SQL 不是我最适合的,但任何帮助将不胜感激。

4

2 回答 2

0

如果您使用 ActiveRecord,那么交叉表查询的语法非常简单:

$this->db->select('sites.site_name');
$this->db->from('sites');
$this->db->join('sites', 'sites.id = user_sites.user_id');
$data['query'] = $this->db->get();

显然这不是确切的查询,因为我不知道您要在数据之间寻找什么关系,但希望您能理解。

于 2013-02-20T00:20:39.700 回答
-1

我认为您将很难将其纳入一个查询。

我没有对此进行测试,但这样的事情可能会给你一些里程:

function get_site_data() {

    $sites = $this->db->get('sites')->result();

    foreach ($sites as &$row)
    {
       $row['domains'] = $this->get_domains_for_site($row->id);
       $row['users'] = $this->get_users_for_site($row->id);
    }

    return $sites;
}

function get_domains_for_site($site_id) {
    return $this->db->get_where("site_domains", array("site_id" => $site_id))->result();
}

function get_users_for_site($site_id) {
    return $this->db->get_where("user_sites", array("site_id" => $site_id))->result();
}
于 2013-02-20T00:28:26.980 回答