예제 #1
0
 static function getContactReferencesById($id)
 {
     $db = Doctrine_Manager::connection();
     $contact_arr = array();
     //get addresses this entity is in
     $sql = 'SELECT DISTINCT a.id FROM address a WHERE a.entity_id = ?';
     $stmt = $db->execute($sql, array($id));
     $addressIds = $stmt->fetchAll(PDO::FETCH_COLUMN);
     if (count($addressIds)) {
         $contact_arr['Address'] = '(r.object_model = ? AND r.object_id IN (' . implode(',', $addressIds) . '))';
     }
     $sql = 'SELECT DISTINCT e.id FROM email e WHERE e.entity_id = ?';
     $stmt = $db->execute($sql, array($id));
     $emailIds = $stmt->fetchAll(PDO::FETCH_COLUMN);
     if (count($emailIds)) {
         $contact_arr['Email'] = '(r.object_model = ? AND r.object_id IN (' . implode(',', $emailIds) . '))';
     }
     $sql = 'SELECT DISTINCT p.id FROM phone p WHERE p.entity_id = ?';
     $stmt = $db->execute($sql, array($id));
     $phoneIds = $stmt->fetchAll(PDO::FETCH_COLUMN);
     if (count($phoneIds)) {
         $contact_arr['Phone'] = '(r.object_model = ? AND r.object_id IN (' . implode(',', $phoneIds) . '))';
     }
     $refs = array();
     //get references for the relationships
     if (count($phoneIds) || count($emailIds) || count($addressIds)) {
         $sql = 'SELECT r.* FROM reference r WHERE ' . implode(' OR ', $contact_arr) . ' ORDER BY r.updated_at DESC';
         $stmt = $db->execute($sql, array_keys($contact_arr));
         $refs = array_merge($refs, $stmt->fetchAll(PDO::FETCH_ASSOC));
         $refs = ReferenceTable::consolidate($refs);
     }
     return $refs;
 }