// Sample snippet to illustrate inner joins with xPDO.
// Make sure you've created some records in the person, phone and person_phone tables.
global $modx;
include_once ($modx->config['base_path'].'assets/snippets/' . 'xpdo/xpdo.class.php');
include_once ($modx->config['base_path'].'assets/snippets/' . 'xpdo/table/maketable.class.php');
// set these as appropriate
$xpdo= new xPDO('mysql:host=localhost;dbname=modx', "dbuser", "password");
$xpdo->setPackage('sample');
$people = array ();
$personTbl = $xpdo->getTableName('Person');
$phoneTbl= $xpdo->getTableName('Phone');
$ppTbl = $xpdo->getTableName('PersonPhone');
$personColumns= $xpdo->getSelectColumns('Person', 'p');
$phoneColumns= $xpdo->getSelectColumns('Phone', 'ph');
$ppColumns = $xpdo->getSelectColumns('PersonPhone', 'pp');
if ($stmt= $xpdo->prepare("SELECT $personColumns,$phoneColumns,$ppColumns FROM $personTbl AS p, $phoneTbl AS ph, $ppTbl AS pp WHERE p.id=pp.person AND ph.id=pp.phone")) {
if ($stmt->execute()) {
foreach ($stmt->fetchAll(PDO_FETCH_ASSOC) as $row) {
$person= $xpdo->newObject('Person');
$person->fromArray($row, '', true);
$phone= $xpdo->newObject('Phone');
$phone->fromArray($row, '', true);
$personPhone = $xpdo->newObject('PersonPhone');
$personPhone->fromArray($row, '', true);
$personPhone->addOne($phone);
$person->addMany($personPhone);
$people[]= $person;
}
}
}
$table = new MakeTable($xpdo);
$tableData = false;
foreach($people as $person)
{
$ppSet = $person->getMany('PersonPhone');
foreach ($ppSet as $pp)
{
$number = $pp->getOne('Phone');
$tableData[] = array('name' => $person->get('first_name') . ' ' . $person->get('last_name'),
'dob' => $person->get('dob'), 'number' => $number->get('number'));
}
}
return $table->create($tableData);
<?php
$person = $xpdo->getObject('Person', 1); // get the Person object with primary key value == 1
$pps = $person->getMany('PersonPhone'); // get all PersonPhone objects related to this Person
$phone; // declare this here so we can use it after the foreach loop
foreach ($pps as $k => $v) // loop on each PersonPhone object
{
$phone = $v->getOne('Phone'); // get the Phone object related to this PersonPhone
// do what you want here, e.g. find a phone number with a given id passed in via a GET variable
if ($phone->get('id') == $_GET['id'])
break;
}
// do stuff with $phone...
?>
<?php $person = $xpdo->getObject('Person', 1); // get the Person object with primary key value == 1 ?>
Right, basically a criteria can be any of the following:
* a primary key value
* an array of primary key values in the correct column order (for compound primary keys)
* an array expression in the form array( ’[tableAlias.]fieldName[:operator]’ => value)
* an xPDOCriteria instance (or xPDOQuery, since it is a derivative class of xPDOCriteria)
xPDO knows what the primary key fields are and if you just pass a scalar or an array of scalars without associative keys, xPDO assumes that’s what you are searching by.
<?php
$person= $xpdo->getObjectGraph('Person', '{PersonPhone:{id:{Phone:{phone:{}}}}}', 1);
<?php
$xpdo->config['hydrate_rel_obj_vars']= true;
$person_id= 1;
$phone_id= 1;
$person= $xpdo->getObjectGraph(
'Person',
'{PersonPhone:{id:{Phone:{phone:{}}}}}',
array ('Person.id' => $person_id, 'PersonPhone.phone' => $phone_id)
);
$compoundKey= $person_id . '-' . $phone_id;
$phone= & $person->PersonPhone[$compoundKey]->Phone;
if ($phone) {
print_r($phone->toArray());
// ... do stuff with just the specific $phone we wanted ...
}
$query = <<<SQL
select if(mp.member_id is null, "", ’selected="selected"’) selected, p.*
from position p
left join member_position mp
on mp.member_id = ’$member_id’
and mp.position_id = p.position_id
order by selected desc, p.name ASC
SQL;
$stmt = $modx->query( $query );
if ($stmt) {
$positions = $stmt->fetchAll(PDO::FETCH_OBJ);
}
foreach ($positions as $position) {
$options .= ’<option value="’ . $position->position_id . ’" ’ . $position->selected . ’>’ .
$position->name .
’</option>’;
}