3

I'm connecting to a remote database that is configured only for reading. If I use:

$connection = new \MongoClient($server);                               
$dbname = $connection->selectDB("db");
$dbname->setReadPreference(\MongoClient::RP_SECONDARY);

It works fine, but if I try:

$dm = $this->getDocumentManager(); //using remote db connection in config
$result = $dm->getRepository('Repository')->method();

I get this error:

not master and slaveOk=false

How can I specify the Read Preference to the $dm, same way as it works for the MongoClient?

Thanks in advance...

Alex
  • 51
  • 2

2 Answers2

1

Found it! We have to specify the readPreference property in the config.yml file. Hope this can help anyone else...

conn2:
   server: "%conn2_server%"
     options:
      connect: true
      db: "%conn2_options_db%"
      readPreference: secondary
Alex
  • 51
  • 2
1

It's better use:

readPreference: secondaryPreferred

or

readPreference: primaryPreferred

Instead of simply primary or secondary, because if primary or secondary it's not aviable with the preferred option you can still retrieve data from other node.