在 symfony 1.4 / sfMasterSlavePlugin 中的 Doctrine_Table 中使用不同的连接

发布于 2024-09-08 05:55:06 字数 648 浏览 2 评论 0原文

在我的 symfony 项目中,我正在为我的数据模型使用原则。我还安装了 sfMasterSlavePlugin,以使用不同的连接来执行不同类型的查询(写入与读取)。

由于 mysql 复制的小延迟,我的代码无法获取新插入的记录。为了解决这个问题,我想强制此读取查询的主连接。但我也希望 Doctrine_Table 的上下文能够以正确的方式与我的模型一起工作。

有没有办法在 Doctrine_Table 方法中强制主连接?如何才能做到这一点?

我的班级目前如下所示:

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;
  }
}

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 code 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;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

始终不够爱げ你 2024-09-15 05:55:06

你可以做的是:

public function fetchByLoginFromMaster($login)
{
  $conn = ProjectConfiguration::getActive()->getMasterConnection();
  $q = Doctrine_Query::create($conn)
                ->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;
}
落花浅忆 2024-09-15 05:55:06

我还尝试在执行查询之前将连接显式设置为 master 。但同样,从属连接用于执行查询。

我尝试了以下方法:

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

    $this->setConnection($masterConn);

    $q = $this->createQuery()
                ->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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文