我正在使用带有大量非规范化的 Cassandra,因此我不能使用某种通用类来删除/添加/更新/等对象,因为每种类型的对象都有自己的需要更改的表列表。
例如要删除User
,我需要触摸 3 个表,而不仅仅是一个。要删除Item
,我需要触摸 7 个表等。这意味着基于对象类型的逻辑完全不同。
方案 1
用户类仅包含我需要的字段(id、名称等)和静态函数来查找用户、删除用户等。
<?php
class User {
private $id;
private $name;
// Magic getters and setters removed to save space here
public static function delete($id) {
// find user by id
// delete that user
}
}
方案 2
用户类包含所有内容 - 字段(id、名称等)以及删除/编辑/创建/等特定用户的功能
<?php
class User {
private $id;
private $name;
// Magic getters and setters removed to save space here
public function delete() {
// find user by $this->id
// delete that user
}
}
哪种情况更好,也许还有其他更好的方法可以做到这一点?