使用 Propel 强制数据库刷新

发布于 2024-12-25 00:51:51 字数 1214 浏览 0 评论 0原文

这是我的表格的示例,

<table name="user">
  <column name="name" type="varchar" size="255" required="true" />
</table>

<table name="environment">
  <column name="user_id" type="integer" size="11" required="true" />
  <column name="insertion_time" type="timestamp" required="true" phpName="InsertionTime" />

  <foreign-key foreignTable="user" phpName="User">
    <reference local="user_id" foreign="id"/>
  </foreign-key>
</table>

我想插入一些与其用户链接的环境

function insertEnvironment($id) {
    $user = findUser($id);

    $env = new Environment();
    $env->setUser($user);
    $env->setInsertionTime(new DateTime());

    $env->save();
}

function findUser($id) {
    $user = UserQuery::create()->filterByUserId($id)->findOneOrCreate();
    return $user;
}

我可以在同一页面上插入多个环境,但我不想重新创建用户(如果它已存在)。

现在,每次我插入一个环境时,它都会创建一个用户(在同一页面执行期间,之后它会按预期工作)。

如何强制插入 user 以便下次我想要访问它时它已经存在?

有没有另一种方法可以在不强制“刷新”的情况下实现这一目标? 我不想手动跟踪用户。

编辑:我更改了创建用户的方式(上面已编辑的代码),但没有帮助。

Here is an example of my tables

<table name="user">
  <column name="name" type="varchar" size="255" required="true" />
</table>

<table name="environment">
  <column name="user_id" type="integer" size="11" required="true" />
  <column name="insertion_time" type="timestamp" required="true" phpName="InsertionTime" />

  <foreign-key foreignTable="user" phpName="User">
    <reference local="user_id" foreign="id"/>
  </foreign-key>
</table>

I want to insert some environment linked with their user.

function insertEnvironment($id) {
    $user = findUser($id);

    $env = new Environment();
    $env->setUser($user);
    $env->setInsertionTime(new DateTime());

    $env->save();
}

function findUser($id) {
    $user = UserQuery::create()->filterByUserId($id)->findOneOrCreate();
    return $user;
}

I can insert more than one environment on the same page but I don't want to recreate the user if it already exists.

Right now, it creates one user each time I insert an environment (during the same page execution, afterwards it works as expected).

How can I force the insertion of the user so that the next time I want to access it it already exists?

Is there another way to achieve this without forcing a "flush" ?
I don't want to keep track of the users by hand.

Edit: I changed the way I create the user (code above edited), but it does not help.

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

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

发布评论

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

评论(2

花期渐远 2025-01-01 00:51:51

我找到了一个解决方法,即禁用池化。

Propel::disableInstancePooling();

添加到setup.php文件中。
但它随后会完全禁用该功能,从而降低性能。

I found a workaround, which is to disable the pooling.

Propel::disableInstancePooling();

To add in the setup.php file.
But it then disable the feature completely, which drains performances down.

情场扛把子 2025-01-01 00:51:51

在 ActiveRecord 模型中添加新方法怎么样?

例如,类似的事情:

<?php

class User extends BaseUser
{
    // Override the generated method
    public function addEnvironment($env)
    {
        $env->setInsertionTime(new DateTime());

        parent::addEnvironment($env);
    }
}

然后,首先查找或创建您的用户:

<?php

$user = UserQuery::create()->filterByUserId($id)->findOneOrCreate();

for ($i = 0; $i < $nbEnvironments; $i++) {
    $user->addEnvironment(new Environment());
}

// Save all
$user->save();

您不会获得多个用户对象,并且您将只能使用一项事务来进行所有数据操作。当您想要向用户插入一些环境时,最好将此逻辑添加到 User ActiveRecord 模型中。

What about adding a new method in your ActiveRecord model ?

Something like that for instance:

<?php

class User extends BaseUser
{
    // Override the generated method
    public function addEnvironment($env)
    {
        $env->setInsertionTime(new DateTime());

        parent::addEnvironment($env);
    }
}

Then, start by finding or creating your user:

<?php

$user = UserQuery::create()->filterByUserId($id)->findOneOrCreate();

for ($i = 0; $i < $nbEnvironments; $i++) {
    $user->addEnvironment(new Environment());
}

// Save all
$user->save();

You won't get more than one user object, and you'll be able to use only one transaction for all your data manipulations. As you want to insert some environments to a user, it seems better to add this logic to the User ActiveRecord model.

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