我有一个软删除孩子的典型事件挂钩:
public static function boot()
{
parent::boot();
static::deleted(function($model1)
{
$model1->hasmanyrelation()->delete();
});
}
和
public function hasmanyrelation()
{
return $this->hasMany('Model2');
}
现在我使用:
$model0->model1->each(function($model1){$model1->delete();});
事情按预期工作,模型1的model2子项被(软)删除。但是当我使用:
$model0->model1()->delete();
然后删除所有相关的model1记录,但是model2及其所有记录保持不变。后者有什么问题,它删除了model1记录但跳过模型事件的简单语法?
答案 0 :(得分:3)
答案 1 :(得分:-1)
重构这个:
<?php foreach($products as $product) : ?>
<a href="<?php echo base_url() ?>/Product/details/<?php echo $product["product_id"]; ?>">
<div class="main-products">
<img id="cadeaufoto" src="<?php echo base_url(); ?>upload/<?php echo $product["product_foto_thumb"]; ?>">
<div class="product_naam"><?php echo $product["product_naam"]; ?></div>
<div class="ophaal_plaats"><?php echo $product["ophaal_plaats"]; ?></div>
</div>
</a>
<div class="aangeboden_door">
<p>
<tr>
<?php
//Here is the active record query which is getting the 'voorname' and other data
$userarray = $this->db->get_where('users', array('user_id'=>$product["user_id"]))->row_array();
// you can print_r($userarray); for see the array you get
?>
<a href="<?php echo base_url() . 'User/userdetails/'.$product['user_id'];?>">
<td><?php echo $userarray['voornaam'];?></td>
</tr>
</p>
</div>
<?php endforeach; ?>
到
$model1->hasmanyrelation()->delete();