XF 2.1 Trying to detect if an entity's value equals 1

Matt C.

Well-known member
I know this is going to be a stupid mistake on my part, but I'm trying to detect whether or not an entity's value equals 1.

Here is my template code. This doesn't work.
Code:
<xf:if is="$match.voice == 1">
    <span class="mic" data-xf-init="tooltip" title="{{ phrase('ah_mm_x_has_a_microphone') }}">
        <xf:fa icon="microphone" />
    </span>
<xf:else />
    <span class="mic" data-xf-init="tooltip" title="{{ phrase('ah_mm_x_does_not_have_a_microphone') }}">
        <xf:fa icon="microphone-slash" />
    </span>
</xf:if>

Thank you.
 
Is voice a boolean in your entity, like your if check would imply? If so, just <xf:if is="$match.voice"> will do.

If voice is an actual integer, and you want to check whether it's an integer with the value of 1, you can use strict type checking: $match.voice === 1

The triple = is particularly useful when checking the output of strpos and other functions that may return the value 0 (for position 0), which is equal to boolean false.

if (strpos($haystack, $needle) == 0) will return true both if the needle exists in the haystack at the start of the string, and if it didn't exist in the haystack at all.

if (strpos($haystack, $needle) === false) will return true only if the needle did not exist in the haystack at all.

Hope that helps :)


Fillip
 
Is voice a boolean in your entity, like your if check would imply? If so, just <xf:if is="$match.voice"> will do.

If voice is an actual integer, and you want to check whether it's an integer with the value of 1, you can use strict type checking: $match.voice === 1

The triple = is particularly useful when checking the output of strpos and other functions that may return the value 0 (for position 0), which is equal to boolean false.

if (strpos($haystack, $needle) == 0) will return true both if the needle exists in the haystack at the start of the string, and if it didn't exist in the haystack at all.

if (strpos($haystack, $needle) === false) will return true only if the needle did not exist in the haystack at all.

Hope that helps :)


Fillip

Thank you Fillip. Couple questions. In phpMyAdmin, the voice field is set as tinyInt. I tried changing it to boolean but it just stays as tinyInt when I save it. In the entity file, the voice field is set as a boolean field.

Does the value have to be true or false as boolean?

Is that supposed to happen?
 
In phpMyAdmin, the voice field is set as tinyInt. I tried changing it to boolean but it just stays as tinyInt when I save it. In the entity file, the voice field is set as a boolean field.
Leave it as TINYINT(3), that's the default XF2 way of storing booleans. I'm not sure why, it could be that the BOOLEAN type is not widely supported in MySQL yet, and XF2 needs to work with older versions of MySQL. That's just me speculating, though.

If your DB column is TINYINT(3) (with 1 for true and 0 for false) and entity is set as self::BOOL then you're good to go :)

You'll then refer to your entity column as boolean true/false, and you don't have to concern yourself with how it's stored in the database.


Fillip
 
Leave it as TINYINT(3), that's the default XF2 way of storing booleans. I'm not sure why, it could be that the BOOLEAN type is not widely supported in MySQL yet, and XF2 needs to work with older versions of MySQL. That's just me speculating, though.

If your DB column is TINYINT(3) (with 1 for true and 0 for false) and entity is set as self::BOOL then you're good to go :)

You'll then refer to your entity column as boolean true/false, and you don't have to concern yourself with how it's stored in the database.


Fillip

Thank you. I still can't get it to work. Maybe because it's inside content check?

Code:
<div class="structItem-cell structItem-cell--main" data-xf-init="touch-proxy">
    <xf:if contentcheck="true">
        <ul class="structItem-statuses">
        <xf:contentcheck>
            <xf:if is="$match.voice">
                <span class="mic mic--on" data-xf-init="tooltip" title="{{ phrase('ah_mm_x_has_a_microphone')}}">
                    <xf:fa icon="microphone" />
                </span>
            <xf:else />
                <span class="mic mic--off" data-xf-init="tooltip" title="{{ phrase('ah_mm_x_does_not_have_a_microphone')}}">
                    <xf:fa icon="microphone-slash" />
                </span>
            </xf:if>
            <xf:if is="$match.match_state == 'moderated'">
                <li>
                    <i class="structItem-status structItem-status--moderated" aria-hidden="true" title="{{ phrase('awaiting_approval')|for_attr }}"></i>
                    <span class="u-srOnly">{{ phrase('awaiting_approval') }}</span>
                </li>
            </xf:if>
            <xf:if is="$match.match_state == 'deleted'">
                <li>
                    <i class="structItem-status structItem-status--deleted" aria-hidden="true" title="{{ phrase('deleted')|for_attr }}"></i>
                    <span class="u-srOnly">{{ phrase('deleted') }}</span>
                </li>
            </xf:if>
        </xf:contentcheck>
        </ul>
    </xf:if>
 
🤔

Directly below your <div> type {{ dump($match) }} then inspect the result, that should tell you why.

Also, your contentcheck in this case is useless since the $match.voice check will always return some amount of text. It will always print either "has a mic" or "does not have a mic", so there will always be content inside the <ul> :)


Fillip
 
Can you show me the getStructure function from the AH\Matchmaking\Entity\Match file? There might be something else going on here but I'm not sure 🤔


Fillip
 
Oh actually it was working, I was just using the wrong syntax for the font awesome icon,. microphone instead of fa-microphone

All good now (: Thanks Fillip

190702
 
Leave it as TINYINT(3), that's the default XF2 way of storing booleans. I'm not sure why, it could be that the BOOLEAN type is not widely supported in MySQL yet, and XF2 needs to work with older versions of MySQL. That's just me speculating, though.

FWIW BOOLEAN / BOOL just alias to TINYINT(1) even in MySQL 8.0, they do (since 5.0.3) support BIT though which can have a length of 0-64 is probably a cleaner way to handle booleans, but I haven't used it
 
Back
Top Bottom