class \mpf\datasources\sql\ModelCondition

class \mpf\datasources\sql\ModelCondition

Description of ModelCondition

@author mirel

Attributes
public string $model

Model class for current table; It's optional, conditions can be created outside the model also;

public int $offset

Offset from wich to start reading/updating

public int $limit

Maximum number of elements to be returned;

public string $condition

String sql ready condition;

public string $order

Column or expression used for order;

public string $having

Having condition; Can be used in addition to group

public string $group

Column or expression to be used for grouping

public mixed $with = []

An array of relation names or a string with relation names separated by ","; Can only be used from a model. @example "child1, child2, child2.subchild2"

public mixed $fields = "*"

Fields to be selected; Can be an array of field or a string with columns / expressions separated by ,

public string $join = ""

Extra joins can be manually written here. Can be used when there is no model but relations are still needed in the query;

public string $procedure

Set procedure name and arguments for select; @link https://dev.mysql.com/doc/refman/5.0/en/select.html

public string $into = ""

Possible options: OUTFILE 'filename' export options DUMPFILE 'filename' varname [, varname] @link https://dev.mysql.com/doc/refman/5.0/en/select.html

public bool $for_update

If set to true "FOR UPDATE" will be added to query;

public bool $lock_in_share_mode

If set to true "LOCK IN SHARE MODE" will be added to query;

public bool $ignore

If set to true will be used for INSERT & UPDATE queries;

public bool $together = false

Select multiple conditions(HASMANY and MANYTO_MANY) in the same query for all models. If it's set to no then it will be selected for each model when is used. In that case it will be added in with only for the conditions to work.

protected string[] $params = []

List of parameters linked to current condition;

protected array $relationsInCondition = []

List of relations used in columns conditions

protected array $conditionColumns = []

List of known columns used in condition. Used to know which relations to include.

protected \mpf\datasources\sql\RelationsParser $relationsParser

-no description found-

Methods

public string[] getParams ()

Return list of params and the values.

public string __toString ()

Get full select query from current condition;

public string getAsQuery (bool $forCount = false)

  • $forCount

    -no description found-

Get as string query.

public string getCountQuery ()

Get full select query for count;

public string forDelete (array $deleteOptions = [])

  • $deleteOptions

    Possible Options: LOW_PRIORITY, QUICK or IGNORE

Get query For Delete

public \mpf\datasources\sql\ModelCondition compareColumn (string $name, string $value, bool $partial = false, string $link = "AND")

  • $name

    column name to be compare. if it's from a relation then it's relationName.columnName

  • $value

    value to compare with; Can start with: >, <, >=, <=, !=. Can also be an array, in that case "IN" will be used as condition

  • $partial

    if it's set to true then "LIKE" will be used with "%" added before and after the value;

  • $link

    Connection between this condition and current one. Can be AND | OR

Compare a colum with a single value or multiple values; Example:


 $condition->compareColumn('name', 'Mirel'); // simple comparison with exact value.
   //  Result: "`tableName`.`name` = :name" where :name = 'Mirel'
 $condition->compareColumn('name', array('Mirel', 'Nicu')); // compare with multiple values;
   //  Result: "`tableName`.`name` IN (:name1, :name2)" where :name1 = 'Mirel' and :name2 = 'Nicu'
 $condition->compareColumn('age', '>=20'); // must be equal or bigger than selected value
   //  Result: "`tableName`.`age` >= :age" // where :age = 20
 $condition->compareColumn('name', 'Mirel', true); // partial match
   //  Result: "`tableName`.`name` LIKE :name" //where :name = '%Mirel%'
 

public \mpf\datasources\sql\ModelCondition compareColumns (string $column1, string $column2, bool $partial = false, string $link = "AND")

  • $column1

    -no description found-

  • $column2

    -no description found-

  • $partial

    -no description found-

  • $link

    -no description found-

Same as compareColumn but instead of value another column is used.

Example:


 $condition->compareColumn('name', 'firstname'); // simple comparison with exact value of the columns.
 //   Result: "`tableName`.`name` = `tableName`.`firstName`"
 $condition->compareColumn('name', array('fname', 'lname')); // compare with multiple columns;
 //   Result: "`tableName`.`name` IN (`tableName`.`fname`, `tableName`.`lname`)"
 $condition->compareColumn('age', '>=fname'); // must be equal or bigger than selected column
 //    Result: "`tableName`.`age` >= `tableName`.`fname`"
 $condition->compareColumn('name', 'fname', true); // partial match
 //    Result: "`tableName`.`name` LIKE CONCAT('%', `tableName`.`fname`, '%')"

public \mpf\datasources\sql\ModelCondition addInCondition (string $column, string[] $values, string $link = "AND")

  • $column

    Column name to be compared

  • $values

    List of possible values

  • $link

    Connection to other conditions can be "AND" or "OR"

This is a shortcut to compareColumn when only multiple values can be sent.

public \mpf\datasources\sql\ModelCondition addNotInCondition (string $column, string[] $values, string $link = "AND")

  • $column

    Column name to be compared

  • $values

    List of values

  • $link

    Connection to existent condition

Same as addInCondition() but it will use "NOT IN" condition.

public \mpf\datasources\sql\ModelCondition addBetweenCondition (string $column, int $start, int $end, string $link = "AND")

  • $column

    Name of the column to be compared

  • $start

    Minimum value

  • $end

    Maximum value

  • $link

    Connection to existent condition

Adds a new condition that compares value of a column be between to other values.

public \mpf\datasources\sql\ModelCondition addInSelectCondition (string $column, string $query, array $params = [], string $link = "AND")

  • $column

    -no description found-

  • $query

    -no description found-

  • $params

    -no description found-

  • $link

    -no description found-

Add a new condition to search a column in the result of a query; Will create a subselect in the condition;

public \mpf\datasources\sql\ModelCondition addIsNullCondition (string $column)

  • $column

    -no description found-

Checks if a column is null

public \mpf\datasources\sql\ModelCondition addIsNotNullCondition (string $column)

  • $column

    -no description found-

Checks if a column is not null

public \mpf\datasources\sql\ModelCondition addCondition (string $condition, array $params = [], string $link = "AND")

  • $condition

    Condition to be added. Can be a string or an associative array column=>value

  • $params

    Optional, list of parameters used be the string condition

  • $link

    Connection with old condition, by default it's "AND"

Adds another condition to the current one.

public string getJoin (bool $forCount = false)

  • $forCount

    -no description found-

Get join info query for current condition;

public mixed getJoinParams ()

-no description found-

public array getExtraRelations (\mpf\datasources\sql\DbModel[] $models)

  • $models

    -no description found-

Return list of relations that were not selected in main query.

public string getSelect ()

Get fields for select; If model is set then the fields are processed so that it will return the data in a way that can be used to generate the models.

public string getCondition ()

Get main condition or "1" if none it's set

public string getHaving ()

Get having condition

public string getOrder ()

Get order for select or update

public string getGroup ()

Return sql Group

public string getLimit ()

Return SQL limit;

public string getProcedure ()

Get called procedure name;

public string getInto ()

Get select INTO option

public static \mpf\datasources\sql\ModelCondition getFrom (mixed $condition, mixed $model)

  • $condition

    The original condition

  • $model

    -no description found-

Get ModelCondition or subclass from a condition that can be string or array or another condition object, in wich case it will return that object;

public \mpf\datasources\sql\ModelCondition setParam (string $name, string $value)

  • $name

    -no description found-

  • $value

    -no description found-

Set a single parameter used for query.

public \mpf\datasources\sql\ModelCondition setParams (string $params)

  • $params

    -no description found-

Set multiple parameters used by the query

protected string _param (string $column, string $value, bool $isIn = false)

  • $column

    column name

  • $value

    value for the parameter

  • $isIn

    specifiy if parameter is for "IN" condition;

Used internally to set a param. It will generate param name searching first if it already exists or not; In case it already exists it will try to add a suffix to it. Examples: :agname, :agname0, :agname1, :agname2...

protected string _column (string $name, bool $forSelect = false, bool $mainQuery = true)

  • $name

    Original name, can be a simple column or relation.column or relation.relation2.column

  • $forSelect

    Specify if it is for select or just for conditions

  • $mainQuery

    -no description found-

Get escaped column name from input. If it's set for select then it will also set the alias depending on relation. Examples: column: name for select: t.name as t_name condition: t.name column: rel.name for select: rel.name as rel_name condition: rel.name column: rel.subrel.name for select: rel_subrel.name as rel_subrel_name condition: rel_subrel.name

protected string _fromArray (string $colValues, string $separator = "AND")

  • $colValues

    List of columns and values to be compared

  • $separator

    Separator to be used between conditions

Transforms an array condition to string. Parameters are automatically generated and set. Example: condition: array('col1'=>'val1', 'col2' => 'val2') separator: AND result: 't.col1 = :col1 and t.col2 = :col2' params: :col1 = val1 , :col2 = val2

protected bool _columnIsExpression (string $column)

  • $column

    -no description found-

Checks if the selected string is a SQL expression or a simple column so that it will know if must processit or not using _column() method.

protected string[] getAllColumnsForSelect ()

Return list of columns to select from current tables;

protected mixed _columnsForModel (mixed $tableName, mixed $db)

  • $tableName

    -no description found-

  • $db

    -no description found-

-no description found-

protected mixed _columnsForRelation (mixed $relation, mixed $connection, mixed $relationName)

  • $relation

    -no description found-

  • $connection

    -no description found-

  • $relationName

    -no description found-

-no description found-