使用 Propel 强制数据库刷新
这是我的表格的示例,
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了一个解决方法,即禁用池化。
添加到setup.php文件中。
但它随后会完全禁用该功能,从而降低性能。
I found a workaround, which is to disable the pooling.
To add in the setup.php file.
But it then disable the feature completely, which drains performances down.
在 ActiveRecord 模型中添加新方法怎么样?
例如,类似的事情:
然后,首先查找或创建您的用户:
您不会获得多个用户对象,并且您将只能使用一项事务来进行所有数据操作。当您想要向用户插入一些环境时,最好将此逻辑添加到 User ActiveRecord 模型中。
What about adding a new method in your ActiveRecord model ?
Something like that for instance:
Then, start by finding or creating your user:
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.