开发者

Using a different Connection within Doctrine_Table in symfony 1.4 / sfMasterSlavePlugin

开发者 https://www.devze.com 2023-01-05 14:32 出处:网络
In my symfony project i\'m working with doctrine for my data models. Also i have the sfMasterSlavePlugin installed to use different connections for different kinds of queries (writes vs. reads).

In my symfony project i'm working with doctrine for my data models. Also i have the sfMasterSlavePlugin installed to use different connections for different kinds of queries (writes vs. reads).

Because of the small delay in mysql's replication my c开发者_运维问答ode is failing to fetch a freshly inserted record. To get around this problem i want to force the master connection for this read query. But also i want to have the context of Doctrine_Table to work with my model in a proper way.

Is there a way to force the master connection within a Doctrine_Table method ? And how can this be done ?

My class currently looks like this:

class UserTable extends Doctrine_Table
{

public static function getInstance()
{
    return Doctrine_Core::getTable('User');
}

public function fetchByLoginFromMaster($login)
{
    $q = $this->createQuery()
                ->from('User')
                ->where('login = ?', $login)
                ->fetchOne();

    return $q;
  }
}


What you can do is this:

public function fetchByLoginFromMaster($login)
{
  $conn = ProjectConfiguration::getActive()->getMasterConnection();
  $q = Doctrine_Query::create($conn)
                ->from('User')
                ->where('login = ?', $login)
                ->fetchOne();
  return $q;
}


i also tried to set the connection explicitly to master before doing the query. But again, the slave connection was used to execute the query.

I tried the following:

public function fetchByLoginFromMaster($login)
{
    $masterConn = ProjectConfiguration::getActive()->getMasterConnection();

    $this->setConnection($masterConn);

    $q = $this->createQuery()
                ->from('User')
                ->where('login = ?', $login)
                ->fetchOne();

    return $q;
}

Thank you, Stephan

0

精彩评论

暂无评论...
验证码 换一张
取 消