Idiorm Documentation
Each of these methods takes the same set of arguments. The following description will use the basic join method as
an example, but the same applies to each method.
The first two arguments are mandatory. The first is the name of the table to join, and the second supplies the conditions
for the join. The recommended way to specify the conditions is as an array containing three components: the first
column, the operator, and the second column. The table and column names will be automatically quoted. For example:
<?php
$results = ORM::for_table('person')->join('person_profile', array('person.id', '=',
˓→'person_profile.person_id'))->find_many();
It is also possible to specify the condition as a string, which will be inserted as-is into the query. However, in this case
the column names will not be escaped, and so this method should be used with caution.
<?php
// Not recommended because the join condition will not be escaped.
$results = ORM::for_table('person')->join('person_profile', 'person.id = person_
˓→profile.person_id')->find_many();
The join methods also take an optional third parameter, which is an alias for the table in the query. This is
useful if you wish to join the table to itself to create a hierarchical structure. In this case, it is best combined with the
table_alias method, which will add an alias to the main table associated with the ORM, and the select method
to control which columns get returned.
<?php
$results = ORM::for_table('person')
->table_alias('p1')
->select('p1.
*
')
->select('p2.name', 'parent_name')
->join('person', array('p1.parent', '=', 'p2.id'), 'p2')
->find_many();
4.10.1 Raw JOIN clauses
If you need to construct a more complex query, you can use the raw_join method to specify the SQL fragment for
the JOIN clause exactly. This method takes four required arguments: the string to add to the query, the conditions is
as an array containing three components: the first column, the operator, and the second column, the table alias and
(optional) the parameters array. If parameters are supplied, the string should contain question mark characters (?) to
represent the values to be bound, and the parameter array should contain the values to be substituted into the string in
the correct order.
This method may be used in a method chain alongside other
*
_join methods as well as methods such as offset,
limit and order_by_
*
. The contents of the string you supply will be connected with preceding and following
JOIN clauses.
<?php
$people = ORM::for_table('person')
->raw_join(
'JOIN (SELECT
*
FROM role WHERE role.name = ?)',
array('person.role_id', '=', 'role.id'),
'role',
array('role' => 'janitor'))
->order_by_asc('person.name')
->find_many();
(continues on next page)
4.10. Joins 23