Open Source Content Management Framework

MidgardQueryBuilder method add_constraint

  1. Usage:
  2. Available comparison operators
    1. INTREE

void $qb->add_constraint(str property, str constraint, mixed value)

add_constraint is a method for adding query constraints to Query Builder queries.

The method takes three arguments:

  • property is the name of a property in the MgdSchema type the constraint applies for
  • constraint is the comparison used for the constraint. See below for details.
  • value is the value compared to the property value using the constraint

Midgard 1.8: See also the add_placeholder_constraint method for a mechanism to specify the constraint value only when the query is executed.

Midgard 1.8: References with constraints may be used for metadata objects or for referenced link types.

Usage:

<?php
$qb = new MidgardQueryBuilder("midgard_article");

// List only articles in topic #123
$qb->add_constraint('topic', '=', 123);

// List only articles by current user
$qb->add_constraint('author', '=', $_MIDGARD['user']);

// List articles posted after May 7th 1999
$timestamp = mktime(0, 0, 0, 5, 7, 1999);
$qb->add_constraint('created', '>', $timestamp);

// List articles with word "Midgard" anywhere in their title
$qb->add_constraint('title', 'LIKE', '%Midgard%');
?>

Available comparison operators

The following SQL operators are supported:

  • <, <=, =, <>, >=, > for numeric or lexicographic comparisons of values.
  • LIKE for substring searches, with % and _ as wildcard characters.
  • IN, NOT IN, NOT LIKE
  • Midgard 1.8: INTREE

The IN and NOT IN operators expect the values as a PHP array:

    <?php
    $topics_list = array(1, 2, 3);
    $qb->add_constraint('topic', 'IN', $topics_list);
    ?>

INTREE

The INTREE operator is not a typical SQL operator. It's related to Midgard tree content structures. The property name used as first argument should be either the parent or the up property name. In any other case midgard-core produces a warning message about an incorrect property used with the INTREE operator. This operator expects only integer values. midgard-core produces a warning message if any other value type is going to be used:

    <?php
    $qb->add_constraint('up', 'INTREE', 1);
    ?>

The value of properties with the unsigned integer value type is typecasted to integer when adding this type of property constraint. Thus, the value can not be greater than 2,147,483,648.

Tagged
midgard-1.8.0
midgard-1.7.0
ragnaroek
Designed by Nemein, hosted by Anykey