-1

使用 Codeigniter 和Datamapper:我有 2 个表:tagsclients。客户端可以有很多标签,标签可以有很多客户端。我正在使用单独的连接表来保存关系。

我有一个用于管理单个标签的页面,我在其中遍历每个客户端,并想检查每个客户端是否与该标签相关。

下面????的代码中确定是否选中了复选框,TRUE如果客户端有标签,则应该是,FALSE如果没有。

<h2>Manage Tag: <?php echo $tag->name; ?></h2>

<?php foreach ($clients as $client): ?>

    <label>
        <?php echo form_checkbox('client_id[]', $client->id, ????); ?>
        <?php echo $client->name; ?>
    </label>

<?php endforeach; ?>

如何使用Datamapper检查此循环中$client是否与相关?$tag

4

1 回答 1

2

怎么样

$client->is_related_to($tag)

或者

$client->is_related_to('tag', $tag->id)

请参阅http://datamapper.wanwizard.eu/pages/count.html#is_related_to

Note that this will fire additional count() queries, you might be better of fetching $tag->clients, and then check in your loop if

isset($tag->clients->all[$client->id])
于 2011-08-01T16:15:48.953 回答