Model Phalcon:事务和复杂模型
在我的项目中,我有Model Phalcon:事务和复杂模型,model,phalcon,Model,Phalcon,在我的项目中,我有实体表,其中所有实体都应该存在(以支持复杂的外键),因此我需要在特殊表中插入额外的行(在此实体列表中),然后再将行插入到模型表中,我想问一下这样做的最佳方式是什么 所以,对于以下代码,我需要插入两行:在实体表中,然后为刚刚插入的行提取id,将其保存在当前模型中,并插入帐户表中: $account = new Account(); $account->name = 'John'; $account->save(); // performs two inserts wh
实体
表,其中所有实体都应该存在(以支持复杂的外键),因此我需要在特殊表中插入额外的行(在此实体列表中),然后再将行插入到模型表中,我想问一下这样做的最佳方式是什么
所以,对于以下代码,我需要插入两行:在实体
表中,然后为刚刚插入的行提取id,将其保存在当前模型中,并插入帐户
表中:
$account = new Account();
$account->name = 'John';
$account->save(); // performs two inserts while creating model
据我所知,我可以使用beforeCreate()方法在entity
表中插入行,并为新创建的行提取id,如下所示:
class Account
{
public function beforeSave()
{
$entity = new \Entity();
$entity->type = get_class($this);
$entity->save();
$this->id = $entity->id;
}
}
但通过这种方式,若不插入帐户行,则实体
表中的行将存在
然后我想使用文档中显示的事务
但我不知道,如果我对每个model::create()方法都有小事务,那么当我需要处理复杂操作时,它将如何工作
e、 g
很难想象它在大项目中会起什么作用。。嵌套事务这对数据库性能不是很好
我还考虑了3d实现方法,我在下面添加了代码,但它看起来像是一个黑客,我也不想使用它:
public function create($data = null)
{
// Create abstract entity instance
$entity = new \Entity();
$entity->type = get_class($this);
// Save abstract entity
if (!$entity->save()) {
return false;
}
// Save current entity
$this->id = $entity->id;
$result = parent::create($data);
// Remove abstract entity if current row was not saved
if (!$result) {
$entity->delete();
}
return $result;
}
支持此类复杂实体的最佳简便方法是什么?实现事务的最简单方法是使用0.9.0:
class Account
{
public function beforeCreate()
{
$entity = new \Entity();
$entity->type = get_class($this);
$this->entity = $entity;
}
public function initialize()
{
$this->belongsTo(array('entity_id', 'Entity', 'id'));
}
}
另一方面,事务管理器创建一个隔离连接,允许您查询在当前事务快照中修改的记录,以及查看非隔离记录
这里,对新文档中的不同交易场景进行了解释:Hmm。。。例如,如果$account->save()失败,是否有可能回滚插入“实体”行?如果帐户无法保存,则不会创建实体行,对吗?第二个问题:是否可以将隐式事务与手动事务(在控制器操作层)和独立事务一起使用?
class Account
{
public function beforeCreate()
{
$entity = new \Entity();
$entity->type = get_class($this);
$this->entity = $entity;
}
public function initialize()
{
$this->belongsTo(array('entity_id', 'Entity', 'id'));
}
}