XF 2.0 Checking check boxes with 2 different entities

AndrewSimm

Well-known member
The first entity titled "honors" list all honors a player could have. A second entity called "HonorResults" list which honors a player actually has. I am creating a player update page where a staff member can update what honors the player has

Here is how I get both in the controller

PHP:
                //Get honors
                $repo = $this->repository('CIS\Backend:Honors');
                $finder = $repo->findHonors();
                $honors = $finder->fetch();

                //Get honor results
                $repo = $this->repository('CIS\Backend:HonorResults');
                $finder = $repo->findHonorResults();
                $honor_results = $finder->where('id',$id)->fetch();

Here is how I display the checkboxes
HTML:
<xf:checkboxrow>
    <xf:foreach loop="$honors" value="$honor">
        <xf:option name="honors[]" value="{$honor.manage_honor}" label="{$honor.manage_honor}" />
    </xf:foreach>
</xf:checkboxrow>

I am trying to check the check boxes when the values exist within "HonorResults"
 
I would run a loop in the controller over HonorResults that adds honors[honor][isChecked] where needed then just check for that in your current template.
 
I would run a loop in the controller over HonorResults that adds honors[honor][isChecked] where needed then just check for that in your current template.

I am not sure how to do that when both "Honors" and "HonorResults" are objects with multiple rows. Essentially I would need to loop through one and then loop over reach result. Normally I would do this in SQL, but I can't do this with finder.
 
When I try this I get "checked" is an invalid column (which it is, since it isn't in the database)

Basically I need to "checked" to act like a column, without being one.

PHP:
$i = 0;              
foreach($honors as $honor) {

    foreach($honor_results as $honor_result) {

        if($honor->manage_honor == $honor_result->honor) {
            $honor->checked = 'yes';
        } else {
            $honor->checked = 'no';
        }

    }

    $i++;
}
 
Last edited:
It looks like I just added to add it to the entity and not specify a default. Is this ok, would it cause any issues?
 
You don't need a checked column. If honours is the list of honours, and user_honours is a relation table linking honours and users together, you simply loop over the honours and tick them if there's an associated relation. Also not sure why you're doing this in the PHP code, this should be in the template.

You should change the design if possible, though. It looks to me like you can just do something like the secondary_user_groups field xf_user has. A comma separated list of honours would work fine in this case. A prefix_honours column on xf_user for example, with IDs like 5,7,8,9, then you can just do:

PHP:
<xf:checkboxrow name="user[prefix_honours]" value="{$user.prefix_honours}"
        listclass="listColumns"
        label="{{ phrase('prefix_user_honours') }}">
    <xf:options source="$honours" />
</xf:checkboxrow>

If that isn't possible for some reason, then go with option 1 and an in_array check should do the trick, something like <option [...] {{ (in_array($honour.id, $userHonours) ? 'checked' : '') }}> (dummy code, probably won't work, but that's the concept anyway). The 2nd option is far more ideal, though.
 
Top Bottom