You are browsing a version that is no longer maintained.

Slave Okay Queries

slaveOkay was deprecated in 1.2 - please use Read Preference instead.

Documents

You can configure an entire document to send all reads to the slaves by using the slaveOkay flag:

1<?php /** @Document(slaveOkay=true) */ class User { /** @Id */ private $id; }
2
3
4
5
6
7
8

Now all reads involving the User document will be sent to a slave.

Queries

If you want to instruct individual queries to read from a slave you can use the slaveOkay() method on the query builder.

1<?php $qb = $dm->createQueryBuilder('User') ->slaveOkay(true); $query = $qb->getQuery(); $users = $query->execute();
2
3
4
5
6

The data in the query above will be read from a slave. Even if you have a @ReferenceOne or @ReferenceMany resulting from the query above it will be initialized and loaded from a slave.

1<?php /** @Document */ class User { /** @ReferenceMany(targetDocument="Account") */ private $accounts; }
2
3
4
5
6
7
8

Now when you query and iterate over the accounts, they will be loaded from a slave:

1<?php $qb = $dm->createQueryBuilder('User') ->slaveOkay(true); $query = $qb->getQuery(); $users = $query->execute(); foreach ($users as $user) { foreach ($user->getAccounts() as $account) { echo $account->getName(); } }
2
3
4
5
6
7
8
9
10
11
12