【问题标题】:Translate an SQL query using ISNULL into Datamapper ORM for Codeigniter将使用 ISNULL 的 SQL 查询转换为 Datamapper ORM for Codeigniter
【发布时间】:2012-04-07 16:46:26
【问题描述】:

我有 3 个 mySQL 表:user、section 和 user_section(一个连接表)。

下面的SQL:

SELECT id, description, 
NOT ISNULL(
    (SELECT user_id 
     FROM user_section 
     WHERE user_section.section_id = section.id 
     AND user_id=5)) AS user_in_section
FROM section
WHERE site_id = 3

很好地产生了以下结果:

id   description    user_in_section
3    section1       1
8    section2       0
9    section3       1

我的视图中有一个表单来编辑用户及其所属的部分。我正在这样写复选框($user_sections 包含上述数据):

<div class="checkboxes">
    <?php foreach ($users_sections as $row) { ?>
    <label class="checkbox"><input <?= ($row->checked>0?'checked':'') ?> type="checkbox" name="section[]" value="<?= $row->id ?>" /> <?= $row->description ?></label></br>
    <?php } ?>
</div>

我的问题是:

有没有更好的写sql查询的方法?还是我有什么好/唯一的方法来获取我想要的数据?

我想使用 Datamapper ORM 来编写查询。到目前为止我有这个......

$section
->select('sections.id, description')
->select_func('NOT ISNULL', array('(SELECT id FROM sections_site_users where sections_site_users.site_section_id=sections.id and sections_site_users.site_user_id='.$user_id.')'), 'checked')
->where('site_id', $site_id)
->get();

ISNULL 之前的 NOT 用单引号转义,防止查询是正确的 sql。如何在 Datamapper 中正确编写?

非常感谢任何改进建议。提前致谢。

编辑:

我找到了一个可行的解决方案 - 这是我的 Datamapper 代码:

$sections = new Section();
$sections
    ->where('site_id', $site_id)
    ->get();

foreach ($sections as $s)
{
    $s2 = new Section();
    $s2
        ->where('id', $s->id)
        ->where_related('user', 'id', $user_id)
        ->get();

    $s->checked = ($s2->exists() ? 1 : 0);
}

它产生与我答案顶部的 sql 相同的数据。然而,它确实使用了更多的数据库查询(我希望在一个数据库查询中做到这一点)。

谁能帮我确认我在这里做得最好?

谢谢!

【问题讨论】:

    标签: mysql codeigniter codeigniter-datamapper


    【解决方案1】:

    是的!简单地说:

    $user = new User();
    $sections = $user->section->where('id', $site_id)->get_iterated();
    foreach($sections as $section)
    {
        // $section->id
        // $section->description
    }
    

    但你必须正确设置你的关系!

    即:

    类用户: public $has_many = array('section);

    类部分: public $has_many = array('user');

    联接表应该是:users_sections

    用户表:用户 节表:节

    编辑: 您想要具有给定站点的特定用户的部分。您在站点和用户之间建立了关系。因此,您将首先结束加载用户,然后获取他的部分:

    $user = new User($user_id);
    if ($user->result_count())
    {
        $sections = $user->section->where_related_site('id', $site_id)->get_iterated();
    }
    

    这应该没问题。对不起,我一开始没听懂。 对您的模型进行简短描述会有所帮助:)

    【讨论】:

    • 感谢 Spir,我的关系设置正确,但我还没有使用 get_iterated - 所以我会尝试一下。您是否有机会帮助我准确地编写 Datamapper 代码以在我的问题顶部实现与我的 SQL 相同的功能?我还不清楚细节。谢谢。
    • 你试过我的代码了吗:$user = new User(); $sections = $user->section->where('id', $site_id)->get(); get_iterated 比经典的 get 快得多,因为它实现了“IteratorAggregate”。你应该在循环一组元素时使用它(参见文档:datamapper.wanwizard.eu/pages/getalt.html#get_iterated
    • 对不起,如果我解释得不够清楚......但你还没有理解我想要什么。如果您查看我的问题顶部的 SQL 及其产生的结果,您会看到我得到了一个站点的部分列表,然后对于每个部分,我得到 1 或 0 是否特定用户在该部分中。无论如何,我有一个可行的解决方案,所以我会关闭这个问题。
    • 我只是注意到,在我回答之后,这就是我编辑第一个答案的原因。
    猜你喜欢
    • 2016-04-20
    • 2020-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多