开发者

Check if one object is related to another with Datamapper

开发者 https://www.devze.com 2023-03-24 03:53 出处:网络
Using Codeigniter and Datamapper: I have 2 tables: tags and clients. Clients can have many tags, tags can have many clients. I am using a separate join table to save the relationships.

Using Codeigniter and Datamapper: I have 2 tables: tags and clients. Clients can have many tags, tags can have many clients. I am using a separate join table to save the relationships.

I have a page for managing an individual tag, where I am iterating through every client, and want to check if each one is related to this tag.

The ???? in the code below de开发者_JS百科termines if the checkbox is checked, it should be TRUE if the client has the tag, FALSE if not.

<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; ?>

How can I check if $client is related to $tag in this loop with Datamapper?


How about

$client->is_related_to($tag)

or

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

See 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])
0

精彩评论

暂无评论...
验证码 换一张
取 消