\midcom_core_querybuilder

MidCOM DBA level wrapper for the Midgard Query Builder.

This class must be used anyplace within MidCOM instead of the real midgard_query_builder object within the MidCOM Framework. This wrapper is required for the correct operation of many MidCOM services.

It essentially wraps the calls to midcom_helper__dbfactory::new_query_builder().

Normally you should never have to create an instance of this type directly, instead use the new_query_builder() method available in the MidCOM DBA API or the midcom_helper__dbfactory::new_query_builder() method which is still available.

If you have to do create the instance manually however, do not forget to call the \initialize() function after construction, or the creation callbacks will fail.

Summary

Methods
Properties
Constants
get_doctrine()
get_current_group()
add_constraint()
add_constraint_with_property()
begin_group()
end_group()
set_limit()
set_offset()
add_order()
get_classname()
execute()
count()
count_unchecked()
__construct()
execute_unchecked()
get_result()
include_deleted()
$hide_invisible
$denied
No constants found
_convert_class()
_add_visibility_checks()
_reset()
$_visibility_checks_added
$_limit
$_offset
$_orders
$_real_class
$count
$_query
N/A
_execute_and_check_privileges()
execute_windowed()
$max_window_size
N/A

Properties

$hide_invisible

$hide_invisible : boolean

Set this element to true to hide all items which are currently invisible according to the approval/scheduling settings made using Metadata. This must be set before executing the query.

NOTE: Approval checks not implemented in collector yet

Be aware, that this setting will currently not use the QB to filter the objects accordingly, since there is no way yet to filter against parameters. This will mean some performance impact.

Type

boolean

$denied

$denied : integer

The number of objects for which access was denied.

Type

integer

$_visibility_checks_added

$_visibility_checks_added : boolean

Flag that tracks whether deleted visibility check have already been added

Type

boolean

$_limit

$_limit : integer

The number of records to return to the client at most.

Type

integer

$_offset

$_offset : integer

The offset of the first record the client wants to have available.

Type

integer

$_orders

$_orders : array

The ordering instructions used for this query

Type

array

$_real_class

$_real_class : string

Type that the application expects to retrieve from this instance.

Type

string

$count

$count : integer

The number of records found by the last execute() run. This is -1 as long as no query has been executed. This member is read-only.

Type

integer

$_query

$_query : \midgard\portable\query

The query backend, should be set in constructor. Currently collector or querybuilder

Type

\midgard\portable\query

$max_window_size

$max_window_size : 

When determining window sizes for offset/limit queries use this as maximum size

Type

Methods

get_doctrine()

get_doctrine() : \Doctrine\ORM\QueryBuilder

Returns

\Doctrine\ORM\QueryBuilder

get_current_group()

get_current_group() : \Doctrine\ORM\Query\Expr:

Returns

\Doctrine\ORM\Query\Expr:

add_constraint()

add_constraint(string  $field, string  $operator, mixed  $value) : boolean

Add a constraint to the query.

Parameters

string $field

The name of the MgdSchema property to query against.

string $operator

The operator to use for the constraint, currently supported are <, <=, =, <>, >=, >, LIKE. LIKE uses the percent sign ('%') as a wildcard character.

mixed $value

The value to compare against. It should be of the same type as the queried property.

Returns

boolean —

Indicating success.

add_constraint_with_property()

add_constraint_with_property(string  $field, string  $operator, string  $compare_field) : boolean

Add a constraint against another DB column to the query.

Parameters

string $field

The name of the MgdSchema property to query against.

string $operator

The operator to use for the constraint, currently supported are <, <=, =, <>, >=, >, LIKE. LIKE uses the percent sign ('%') as a wildcard character.

string $compare_field

The field to compare against.

Returns

boolean —

Indicating success.

begin_group()

begin_group(string  $operator = 'OR') 

Creates a new logical group within the query. They are set in parentheses in the final SQL and will thus be evaluated with precedence over the normal out-of-group constraints.

While the call lets you decide whether all constraints within the group are AND'ed or OR'ed, only OR constraints make logically sense in this context, which is why this proxy function sets 'OR' as the default operator.

Parameters

string $operator

One of 'OR' or 'AND' denoting the logical operation with which all constraints in the group are concatenated.

end_group()

end_group() 

Ends a group previously started with begin_group().

set_limit()

set_limit(integer  $limit) 

Limits the resultset to contain at most the specified number of records.

Set the limit to zero to retrieve all available records.

Parameters

integer $limit

The maximum number of records in the resultset.

set_offset()

set_offset(integer  $offset) 

Sets the offset of the first record to retrieve. This is a zero based index, so if you want to retrieve from the very first record, the correct offset would be zero, not one.

Parameters

integer $offset

The record number to start with.

add_order()

add_order(string  $field, string  $direction = 'ASC') : boolean

Add an ordering constraint to the query builder.

Parameters

string $field

The name of the MgdSchema property to query against.

string $direction

One of 'ASC' or 'DESC' indicating ascending or descending ordering. The default is 'ASC'.

Returns

boolean —

Indicating success.

get_classname()

get_classname() 

Get the DBA class we're currently working on

execute()

execute() : array<mixed,\midcom_core_dbaobject>

Execute the Querybuilder and call the appropriate callbacks from the associated class. This way, class authors have full control over what is actually returned to the application.

The calling sequence of all event handlers of the associated class is like this:

  1. boolean _on_prepare_exec_query_builder(&$this) is called before the actual query execution. Return false to abort the operation.
  2. The query is executed.
  3. void _on_process_query_result(&$result) is called after the successful execution of the query. You may remove any unwanted entries from the resultset at this point.

Returns

array<mixed,\midcom_core_dbaobject> —

The result of the query builder.

count()

count() : integer

Returns the number of elements matching the current query.

Due to ACL checking we must first execute the full query

Returns

integer —

The number of records found by the last query.

count_unchecked()

count_unchecked() : integer

This is a mapping to the real count function of the Midgard Query Builder.

It is mainly intended when speed is important over accuracy, as it bypasses access control to get a fast impression of how many objects are available in a given query. It should always be kept in mind that this is a preliminary number, not a final one.

Use this function with care. The information you obtain in general is negligible, but a creative mind might nevertheless be able to take advantage of it.

Returns

integer —

The number of records matching the constraints without taking access control or visibility into account.

__construct()

__construct(string  $classname) 

The initialization routine executes the _on_prepare_new_querybuilder callback on the class.

Parameters

string $classname

The classname which should be queried.

execute_unchecked()

execute_unchecked() 

Runs a query where limit and offset is taken into account <i>prior</i> to execution in the core.

This is useful in cases where you can safely assume read privileges on all objects, and where you would otherwise have to deal with huge resultsets.

Be aware that this might lead to empty resultsets "in the middle" of the actual full resultset when read privileges are missing.

get_result()

get_result(integer  $key, string  $mode = null) : mixed

Get result by its index

Parameters

integer $key

Requested index in result set

string $mode

Execution mode: normal (default), unchecked, notwindowed

Returns

mixed —

False on failure (key does not exist), object given to constructor on success

include_deleted()

include_deleted() 

Include deleted objects (metadata.deleted is true) in query results.

Note: this may cause all kinds of weird behavior with the DBA helpers

_convert_class()

_convert_class(string  $classname) : string

Class resolution into the MidCOM DBA system.

Currently, Midgard requires the actual MgdSchema base classes to be used when dealing with the query, so we internally note the corresponding class information to be able to do correct typecasting later.

Parameters

string $classname

The classname which should be converted.

Returns

string —

MgdSchema class name

_add_visibility_checks()

_add_visibility_checks() 

_reset()

_reset() 

Resets some internal variables for re-execute

_execute_and_check_privileges()

_execute_and_check_privileges() : array<mixed,\midcom_core_dbaobject>

Executes the internal QB and filters objects based on ACLs and metadata

Returns

array<mixed,\midcom_core_dbaobject> —

Array filtered by ACL and metadata visibility

execute_windowed()

execute_windowed() : array<mixed,\midcom_core_dbaobject>

Windowed querying.

Since ACLs/approval can hide objects on the PHP level, we cannot apply offsets directly to the QB, but must instead verify that all objects are visible to the current user before discarding them until the offset is met. To reduce memory consumption, a maximum number of entries is loaded in each iteration.

If we have a limit, we use it to dynamically adjust the window size between iterations to try to not load too much data, i.e. we start by querying the minimum number required to fill the offset and limit. If we end up with too few results, we do another iteration where we adjust the window according to how many we still need plus a padding that corresponds to the weighted number of denieds so far. This is repeated until the limit is filled or we run out of db results

Returns

array<mixed,\midcom_core_dbaobject>