Skip to content

Commit d3d0d42

Browse files
committed
Bug fixes.
1 parent 80a12a4 commit d3d0d42

File tree

4 files changed

+66
-15
lines changed

4 files changed

+66
-15
lines changed

Academe/SugarRestApi/Api/Api.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,10 @@ public function parseRelationshipList($entry_list)
637637
// No 'link_list' in the wrapper wrapper here.
638638
// The format of the linked list data varies depending on whether we are
639639
// fetching for a single entry or multiple entries.
640-
$master_sequence = 0;
640+
// And it also varies according to which API function is called. Th lack of
641+
// documentation is infuriating.
642+
643+
//$master_sequence = 0;
641644
foreach($link_list_wrapper as $list_sequence => $list) {
642645
if (!empty($list['records']) && is_array($list['records'])) {
643646
$relationship_name = $list['name'];
@@ -656,7 +659,7 @@ public function parseRelationshipList($entry_list)
656659
? $this->relationship_aliases[$relationship_name]
657660
: $relationship_name
658661
);
659-
$linked_data[$master_sequence][$alias][] = $record_data;
662+
$linked_data[$list_sequence][$alias][] = $record_data;
660663
}
661664
}
662665
}
@@ -702,6 +705,9 @@ public function arrayToMultiSelectValue($value)
702705
return '^' . implode('^,^', $value) . '^';
703706
}
704707

708+
// TODO: rather than newFoo(), perhaps we need makeFoo().
709+
// We are making a model object, and not necessarily a new record at this point.
710+
705711
// Return a new entry object.
706712

707713
public function newEntry($module)
@@ -735,6 +741,9 @@ public function newModule($module)
735741
// Create a generic object.
736742
// constructor_args is an array of arguments that are passed into the constructor
737743
// of the class as separate argumens.
744+
// TBH The constructor args are pretty useless ATM, as the first argument on all
745+
// of the models is the API anyway. Perhaps here we can pass it in as the first of
746+
// the $constructor_args rather than through setApi()?
738747
739748
protected function newObject($module, $class_name, $constructor_args = array())
740749
{

Academe/SugarRestApi/Api/v4.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,11 +463,23 @@ public function getRelationships(
463463
'limit' => $limit,
464464
);
465465

466+
//var_dump($this->apiPost('get_relationships', $parameters));
466467
return $this->apiPost('get_relationships', $parameters);
467468
}
468469

469470
// Set a single relationship between two beans. The items are related by module name and id.
470-
public function setRelationship($moduleName, $beanId, $linkFieldName, $relatedIds, $data, $delete = 0)
471+
// To remove the links instead of adding them, set $delete to 1.
472+
// Relationship links are added and removed in a cumulative manner, i.e. only those
473+
// relatedIds supplied will be touched.
474+
475+
public function setRelationship(
476+
$moduleName,
477+
$beanId,
478+
$linkFieldName,
479+
$relatedIds,
480+
$data,
481+
$delete = 0
482+
)
471483
{
472484
$parameters = array(
473485
'session' => $this->getSessionId(),
@@ -484,7 +496,15 @@ public function setRelationship($moduleName, $beanId, $linkFieldName, $relatedId
484496

485497
// Set a single relationship between two beans. The items are related by module name and id.
486498
// @todo Description is wrong.
487-
public function setRelationships($moduleNames, $beanIds, $linkFieldNames, $relatedIds, $data, $delete = array())
499+
500+
public function setRelationships(
501+
$moduleNames,
502+
$beanIds,
503+
$linkFieldNames,
504+
$relatedIds,
505+
$data,
506+
$delete = array()
507+
)
488508
{
489509
$parameters = array(
490510
'session' => $this->getSessionId(),

Academe/SugarRestApi/Model/Entry.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ public function getRelationshipFields($relationship_name = null)
108108
}
109109

110110
// Get a single relationship as an array.
111+
// TODO: name this relationshipToArray() for a little more consistency.
112+
// Also similar renaming in EntryList would be good.
111113

112114
public function getRelationshipAsArray($relationship_name = null)
113115
{
@@ -231,6 +233,7 @@ public function setField($name, $value)
231233

232234
// Update the values of multiple fields.
233235
// Supply a key=>value array.
236+
// TODO: maybe change this to "fill()"?
234237
235238
public function updateFields($fields)
236239
{
@@ -245,13 +248,21 @@ public function getFields()
245248
return $this->getAsArray();
246249
}
247250

251+
// Get the Entry properties as an array.
252+
// CHECKME: it would be great if we could catch get_object_vars() and feed it through
253+
// this method, but I cannot see a way to do that yet.
254+
// ** DEPRECATED ** use toArray() instead.
248255
public function getAsArray()
256+
{
257+
return $this->toArray();
258+
}
259+
public function toArray()
249260
{
250261
// Add in any relationship data if there is any.
251262
if (!empty($this->_relationships)) {
252263
return array_merge(
253264
$this->_fields,
254-
array('_relationships' => $this->getRelationshipFields())
265+
array('_relationships' => $this->getRelationshipAsArray())
255266
);
256267
} else {
257268
return $this->_fields;

Academe/SugarRestApi/Model/EntryList.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,18 @@ public function __construct($api = null, $entry_list = array())
142142
public function setEntryList($entry_list) {
143143
if (empty($entry_list) || !is_array($entry_list)) return;
144144

145-
// xxx
145+
// Set the entry data.
146+
$this->parseEntryList($entry_list);
147+
148+
// Set this data set as complete so we don'y attempt to fetch more
149+
// from the CRM.
150+
$this->list_complete = true;
151+
152+
// Set the total count.
153+
$this->total_count = count($this->entry_list);
154+
$this->result_count = $this->total_count;
155+
156+
return $this;
146157
}
147158

148159
// Return the first Entry on the EntryList.
@@ -343,8 +354,14 @@ public function getFields($id = null)
343354
}
344355

345356
// Get a single entry or all entries as an array.
357+
// Actually, this should be toArray()
346358

347359
public function getAsArray($id = null)
360+
{
361+
return $this->toArray($id);
362+
}
363+
364+
public function toArray($id = null)
348365
{
349366
$result = array();
350367

@@ -385,23 +402,17 @@ public function parseEntryList($entry_list)
385402

386403
// Take each entry returned from the API, and turn them into a separate entry class.
387404
if (!empty($entry_list['entry_list'])) {
388-
// Keep a count of the entries, as it is the only way to match them up to
389-
// the relationship records.
390-
$entry_count = 0;
391-
392-
foreach ($entry_list['entry_list'] as $entry) {
405+
foreach ($entry_list['entry_list'] as $record_sequenec => $entry) {
393406
$Entry = new $this->api->entry_classname($this->api, $entry);
394407
$Entry->setModule($this->module);
395408

396409
// If there is relationshnip data for this entry, then add it to the object.
397-
if (!empty($linked_data[$entry_count])) {
398-
$Entry->setRelationshipFields($linked_data[$entry_count]);
410+
if (!empty($linked_data[$record_sequenec])) {
411+
$Entry->setRelationshipFields($linked_data[$record_sequenec]);
399412
}
400413

401414
//$this->entry_list[$Entry->id] = $Entry;
402415
$this->addEntry($Entry->id, $Entry);
403-
404-
$entry_count++;
405416
}
406417
} elseif (is_array($entry_list)) {
407418
// Not API data - just arrays of entry arrays.

0 commit comments

Comments
 (0)