# UserSynchronizer

> Reads explicitly mapped LDAP user attributes in batches and passes them to application-owned persistence.

[API reference](/docs/api/index.md) › [LDAP](/docs/extensions/ldap/reference/index.md) › UserSynchronizer

| final class | `yii2\extensions\ldap\UserSynchronizer` |
| --- | --- |
| Source | [`src/UserSynchronizer.php`](https://github.com/yii2-extensions/ldap/blob/main/src/UserSynchronizer.php#L19) |

Reads explicitly mapped LDAP user attributes in batches and passes them to application-owned persistence.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Creates a synchronizer restricted to the directory component's registered read connections. | `UserSynchronizer` |
| [`sync()`](#method-sync) | Upserts each row using the configured connection name and the directory's stable user identifier. | `UserSynchronizer` |

## Method details

### __construct() <span id="method-__construct"></span>

```php
public function __construct(yii2\extensions\ldap\Directory $directory)
```

Creates a synchronizer restricted to the directory component's registered read connections.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$directory` | [`Directory`](/docs/extensions/ldap/reference/directory/index.md) |  |

### sync() <span id="method-sync"></span>

```php
public function sync(LdapRecord\Query\Model\Builder $query, array<non-empty-string, non-empty-string> $attributes, Closure(string, string, array<string, mixed>): void $upsert, int $pageSize = 500): int
```

Upserts each row using the configured connection name and the directory's stable user identifier.

Missing or unreadable attributes are passed as null and all available values retain their native model
representation. No local users are deleted. If a read or upsert fails, earlier successful upserts are not rolled
back.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$query` | `LdapRecord\Query\Model\Builder` | User query built on an explicitly named read connection. |
| `$attributes` | `array<non-empty-string, non-empty-string>` | Map of application field to LDAP attribute. |
| `$upsert` | [`Closure`](https://www.php.net/manual/en/class.closure.php)`(string, string, array<string, mixed>): void` | Persists one user using the connection and GUID as the key. |
| `$pageSize` | `int` | LDAP page size; the server must support paged results. |

**Returns**

`int`: Number of users passed successfully to the persistence callback.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if the mapping, query, or page size is invalid.
- [`DirectoryUnavailableException`](/docs/extensions/ldap/reference/directoryunavailableexception/index.md): if a result lacks a stable identifier or paging does not complete.
- `LdapRecord\LdapRecordException`: if LdapRecord cannot bind or query the directory. Upsert exceptions propagate unchanged.