AlgoliaSearch\Index::batchObjects PHP Méthode

batchObjects() public méthode

Perform batch operation on several objects.
public batchObjects ( array $objects, string $objectIDKey = 'objectID', string $objectActionKey = 'objectAction' ) : mixed
$objects array contains an array of objects to update (each object must contains an objectID attribute)
$objectIDKey string the key in each object that contains the objectID
$objectActionKey string the key in each object that contains the action to perform (addObject, updateObject, deleteObject or partialUpdateObject)
Résultat mixed
    public function batchObjects($objects, $objectIDKey = 'objectID', $objectActionKey = 'objectAction')
    {
        $requests = array();
        $allowedActions = array('addObject', 'updateObject', 'deleteObject', 'partialUpdateObject', 'partialUpdateObjectNoCreate');
        foreach ($objects as $obj) {
            // If no or invalid action, assume updateObject
            if (!isset($obj[$objectActionKey]) || !in_array($obj[$objectActionKey], $allowedActions)) {
                throw new \Exception('invalid or no action detected');
            }
            $action = $obj[$objectActionKey];
            // The action key is not included in the object
            unset($obj[$objectActionKey]);
            $req = array('action' => $action, 'body' => $obj);
            if (array_key_exists($objectIDKey, $obj)) {
                $req['objectID'] = (string) $obj[$objectIDKey];
            }
            $requests[] = $req;
        }
        return $this->batch(array('requests' => $requests));
    }