Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions Exception/Error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Ddeboer\Salesforce\MapperBundle\Exception;

class Error
{
/**
* An array of Ddeboer\Salesforce\ClientBundle\Response\Error objects
*
* @var array
*/
public $errors;

/**
* A mapper model
*
* @var mixed
*/
public $model;

/**
* getErrors
*
* @return array
*/
public function getErrors()
{
return $this->errors;
}

/**
* setErrors
*
* @param array $errors
*/
public function setErrors(array $errors)
{
return $this->errors = $errors;
}

/**
* getModel
*
* @return mixed
*/
public function getModel()
{
return $this->model;
}

/**
* setModel
*
* @param mixed $model
*/
public function setModel($model)
{
$this->model = $model;
}
}
57 changes: 57 additions & 0 deletions Exception/SaveException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
namespace Ddeboer\Salesforce\MapperBundle\Exception;

/**
* SaveException is thrown if saving models fails
*/
class SaveException extends \Exception
{
/**
* Models that were saved succesfully
* @param array
*/
protected $okModels = array();

/**
* Error objects containing the failed model and errors
* @param array
*/
protected $errors = array();

/**
* getOkModels
* @return array
*/
public function getOkModels()
{
return $this->okModels;
}

/**
* setOkModels
* @param array $okModels
*/
public function setOkModels(array $okModels)
{
$this->okModels = $okModels;
}

/**
* getErrors
* @return array
*/
public function getErrors()
{
return $this->errors;
}

/**
* setErrors
* @param array $errors
*/
public function setErrors(array $errors)
{
$this->errors = $errors;
}
}
?>
63 changes: 60 additions & 3 deletions Mapper.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use Ddeboer\Salesforce\MapperBundle\Event\BeforeSaveEvent;
use Doctrine\Common\Cache\Cache;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Ddeboer\Salesforce\MapperBundle\Exception\SaveException;
use Ddeboer\Salesforce\MapperBundle\Exception\Error;
use Ddeboer\Salesforce\ClientBundle\Exception\SaveException as ClientSaveException;

/**
* This mapper makes interaction with the Salesforce API using full objects
Expand Down Expand Up @@ -245,42 +248,96 @@ public function save($model)
$objectsToBeCreated = array();
$objectsToBeUpdated = array();
$modelsWithoutId = array();
$modelsToBeUpdated = array();

foreach ($models as $model) {
$object = $this->annotationReader->getSalesforceObject($model);
$sObject = $this->mapToSalesforceObject($model);
if (isset($sObject->Id) && null !== $sObject->Id) {
$objectsToBeUpdated[$object->name][] = $sObject;
$modelsToBeUpdated[$object->name][] = $model;
} else {
$objectsToBeCreated[$object->name][] = $sObject;
$modelsWithoutId[$object->name][] = $model;
}
}

$results = array();
$checkModels = array();
foreach ($objectsToBeCreated as $objectName => $sObjects) {
$reflClass = new \ReflectionClass(current(
$modelsWithoutId[$objectName]
));
$reflProperty = $reflClass->getProperty('id');
$reflProperty->setAccessible(true);

$saveResults = $this->client->create($sObjects, $objectName);
try
{
$saveResults = $this->client->create($sObjects, $objectName);
} catch( ClientSaveException $e )
{
$saveResults = $e->getResults();
}
for ($i = 0; $i < count($saveResults); $i++) {
$newId = $saveResults[$i]->id;
$model = $modelsWithoutId[$objectName][$i];
$reflProperty->setValue($model, $newId);
}

$results[] = $saveResults;
$checkModels = array_merge($checkModels, $modelsWithoutId[$objectName]);
$results = array_merge($results, $saveResults);
}

foreach ($objectsToBeUpdated as $objectName => $sObjects) {
$results[] = $this->client->update($sObjects, $objectName);
try
{
$updateResults = $this->client->update($sObjects, $objectName);
} catch( ClientSaveException $e )
{
$updateResults = $e->getResults();
}
$checkModels = array_merge($checkModels, $modelsToBeUpdated[$objectName]);
$results = array_merge($results, $updateResults);
}

$this->checkResult($results,$checkModels);

return $results;
}

/**
* Checks client results and throws SaveException if errors are found
*
* @param array $results
* @param array $models
* @return bool
* @throws Ddeboer\Salesforce\MapperBundle\Exception\SaveException
*/
private function checkResult(array $results, array $models)
{
$okModels = array();
$errors = array();
foreach($results as $key => $result)
{
if ($result->success)
$okModels[] = $models[$key];
else
{
$error = new Error();
$error->model = $models[$key];
$error->errors = $result->errors;
$errors[] = $error;
}
}
if (count($errors) > 0)
{
$saveException = new SaveException($errors[0]->errors[0]->message);
$saveException->setOkModels($okModels);
$saveException->setErrors($errors);
throw $saveException;
}
return true;
}

/**
* Map a Salesforce object to a domain model object
Expand Down