Jump to content

Database where statement


Jon Erickson

Recommended Posts

Posted

Hello all, I have a question regarding terminology and how exactly the SQL statements work. Essentially, I want to build a where statement and have each element separated by an OR. However, when I append part of the where statement, it sets it in the binds. First question, what is the binds parameter apart of the Select Object? Second question, how do I build my where statement using arrays to be separated by an OR? I know I know I can manually write out OR and append the statements to a string. I would like to use arrays. Thanks!

protected function compileWhereClause()
    {
        $where = array();

        $containerClass = '\IPS\perscom\PersonnelAction\Form';
        $where[] = \IPS\Db::i()->findInSet( 'core_permission_index.perm_' . \IPS\perscom\PersonnelAction\Form::$permissionMap['manage'], \IPS\Member::loggedIn()->permissionArray() ) . ' OR core_permission_index.perm_' . \IPS\perscom\PersonnelAction\Form::$permissionMap['manage'] . "='*'";

        // Make sure this user is a soldier
        if ( $soldier = \IPS\Member::loggedIn()->isSoldier() )
        {
            // Add manage permission wheres
            $where[] = '( form_permissions_key = ' . \IPS\perscom\PersonnelAction\Form::PERSONNEL_ACTION_REQUEST_FORM_SUPERVISOR . ' AND perscom_personnel.personnel_supervisor = ' . $soldier->id . ' )';
            $where[] = '( form_permissions_key = ' . \IPS\perscom\PersonnelAction\Form::PERSONNEL_ACTION_REQUEST_FORM_ADMIN_POSITION . ' AND FIND_IN_SET( perscom_personnel_action_request_forms.form_permissions, perscom_personnel.personnel_administrative_unit_positions )';
        }

        // Return the where clause
        return $where;
    }

 

Posted

When you pass multiple array elements as the where statement, they will always be grouped with an AND. If you want to use an OR, you need to build out the full statement yourself.

Just from looking at this one function, it looks like you should be able to just change the return line to

return implode( ' OR ', $where );

 

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...