Skip to content
This repository has been archived by the owner on Oct 8, 2020. It is now read-only.

Document new changes in CRM_Utils_SQL_Select #225

Merged
merged 2 commits into from
Jul 24, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions docs/standards/sql.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ $optedOut = 0; /* un-trusted data */

$query = "
SELECT id
FROM civicrm_contact
WHERE
FROM civicrm_contact
WHERE
display_name like %1 AND
is_opt_out = %2";

Expand Down Expand Up @@ -44,13 +44,23 @@ Further information on this method can be found in the [CRM_Utils_SQL_Select cla

```php
$columnName = CRM_Utils_Type::escape('cm.membership_status', 'MysqlColumnNameOrAlias');
$sql = CRM_Utils_Sql_Select::from('civicrm_contact c')
$dao = CRM_Utils_Sql_Select::from('civicrm_contact c')
->join('cm', 'INNER JOIN civicrm_membership cm ON cm.contact_id = c.id')
->where('!column = @value', array(
'column' => $columnName,
'value' => 15,
))
->where('membership_type_id IN (#types)', array('types', array(1,2,3,4)))
->toSQL();
$result = CRM_Core_DAO::executeQuery($sql);
->execute();

while ($dao->fetch()) { ... }
```

You can chain with other DAO functions like `fetchAll()`, `fetchValue()` or `fetchMap()`.

```php
$records = CRM_Utils_SQL_Select::from('mytable')
->select('...')
->execute()
->fetchAll();
```