Join table class for DataTables Editor.
The Join class can be used with DataTables\Editor\Editor::join to allow Editor to
obtain joined information from the database.
For an overview of how Join tables work, please refer to the Editor manual as it is useful to understand how this class represents the links
between tables, before fully getting to grips with it's API.
Please note that the DataTables\Editor\Join
class is not available in the trial version
of Editor, but is included, with full source, in the licensed download
package.
Namespace: DataTables\
Editor
Example:
Join the parent table (the one specified in the DataTables\Editor\Editor::table method)
with the table access, with a link table user__access, which
allows multiple properties to be found for each row in the parent table.
Join::inst( 'access', 'array' )
->join(
array( 'id', 'user_id' ),
array( 'id', 'access_id' ),
'user_access'
)
->field(
Field::inst( 'id' )->validator( 'Validate::required' ),
Field::inst( 'name' )
)
Example:
Single row join - here we join the parent table with a single row in the
child table, without an intermediate link table. In this case the child table is
called extra and the two fields give the columns that Editor will read
from that table.
Join::inst( 'extra', 'object' )
->join( 'id', 'user_id' )
->field(
Field::inst( 'comments' ),
Field::inst( 'review' )
)
Located at Editor/Join.php
Methods summary
public
|
|
#
__construct( $table = null, $type = 'object' )
Join instance constructor.
Join instance constructor.
Parameters
- $table
string
$table Table name to get the joined data from.
- $type
string
$type Work with a single result ('object') or an array of results ('array')
for the join.
|
public
|
string|self
|
#
aliasParentTable( $_ = null )
Get / set parent table alias.
Get / set parent table alias.
When working with a self referencing table (i.e. a column in the table
contains a primary key value from its own table) it can be useful to set an
alias on the parent table's name, allowing a self referencing Join. For example:
<code> SELECT p2.publisher FROM publisher as p2 JOIN publisher on
(publisher.idPublisher = p2.idPublisher) <code> Where, in this case,
publisher is the table that is used by the Editor instance, and you
want to use Join to link back to the table (resolving a name for
example). This method allows that alias to be set. Fields can then use standard
SQL notation to select a field, for example p2.publisher or
publisher.publisher .
Parameters
- $_
string
$_ Table alias to use
Returns
string|self
Table alias set (which is null by default), or self if used as a setter.
|
public
|
DataTables\Editor\Field[] |self
|
#
field( $_ = null )
Get / set field instances.
Get / set field instances.
The list of fields designates which columns in the table that will be read
from the joined table.
Parameters
Returns
See
|
public
|
DataTables\Editor\Field[] |self
|
#
fields( $_ = null )
Get / set field instances.
|
public
|
boolean|self
|
#
get( $_ = null )
Get / set get attribute.
When set to false no read operations will occur on the join tables.
Parameters
Returns
|
public
|
|
#
join( $parent = null, $child = null, $table = null )
Get / set join properties.
Get / set join properties.
Define how the SQL will be performed, on what columns. There are basically
two types of join that are supported by Editor here, a direct foreign key
reference in the join table to the parent table's primary key, or a link table
that contains just primary keys for the parent and child tables (this approach
is usually used with a DataTables\Editor\Join::type() of 'array' since you can often have multiple
links between the two tables, while a direct foreign key reference will
typically use a type of 'object' (i.e. a single entry).
Parameters
- $parent
string|string[]
$parent Parent table's primary key names. If used with a link table (i.e.
third parameter to this method is given, then an array should be used, with the
first element being the pkey's name in the parent table, and the second element
being the key's name in the link table.
- $child
string|string[]
$child Child table's primary key names. If used with a link table (i.e. third
parameter to this method is given, then an array should be used, with the first
element being the pkey's name in the child table, and the second element being
the key's name in the link table.
- $table
{string}
$table Join table name, if using a link table
Example
// Using a link table Join::inst( 'access', 'array' ) ->join( array( 'id',
'user_id' ), array( 'id', 'access_id' ), 'user_access' )
// Using a direct reference (no link table) Join::inst( 'extra', 'object' )
->join( 'id', 'user_id' )
|
public
|
String|self
|
#
name( $_ = null )
Get / set name.
The name of the Join is the JSON property key that is used when
'getting' the data, and the HTTP property key (in a JSON style) when 'setting'
data. Typically the name of the db table will be used here, but this method
allows that to be overridden.
Parameters
Returns
|
public
|
boolean|self
|
#
set( $_ = null )
Get / set set attribute.
When set to false no write operations will occur on the join tables. This can
be useful when you want to display information which is joined, but want to only
perform write operations on the parent table.
Parameters
Returns
|
public
|
String|self
|
#
table( $_ = null )
Get / set join table name.
Get / set join table name.
Please note that this will also set the DataTables\Editor\Join::name() used by the Join as
well. This is for convenience as the JSON output / HTTP input will typically use
the same name as the database name. If you want to set a custom name, the DataTables\Editor\Join::name() method must be called after this one.
Parameters
- $_
string
$_ Name of the table to read the join data from
Returns
String|self
Name of the join table, or self if used as a setter.
|
public
|
String|self
|
#
type( $_ = null )
Get / set the join type.
The join type allows the data that is returned from the join to be given as
an array (i.e. working with multiple possibly results for each record from the
parent table), or as an object (i.e. working which one and only one result for
each record form the parent table).
Parameters
- $_
string
$_ Join type ('object') or an array of results ('array') for the join.
Returns
String|self
Join type, or self if used as a setter.
|
public
|
string[]|self
|
#
where( $key = null, $value = null, $op = '=' )
Where condition to add to the query used to get data from the database. Note
that this is applied to the child table.
Where condition to add to the query used to get data from the database. Note
that this is applied to the child table.
Can be used in two different ways, as where( field, value ) or as an array of
conditions to use: where( array('fieldName', ...), array('value', ...) );
Parameters
- $key
string|string[]
$key Single field name, or an array of field names.
- $value
string|string[]
$value Single field value, or an array of values.
- $op
string
$op Condition operator: <, >, = etc
Returns
string[]|self
Where condition array, or self if used as a setter.
|
public
|
boolean
|
#
whereSet( $_ = null )
Get / set if the WHERE conditions should be included in the create and edit
actions.
Get / set if the WHERE conditions should be included in the create and edit
actions.
This means that the fields which have been used as part of the 'get' WHERE
condition (using the where() method) will be set as the values
given.
This is default false (i.e. they are not included).
Parameters
- $_
boolean
$_ Include (true ), or not (false )
Returns
|