【发布时间】:2016-12-23 17:52:27
【问题描述】:
有没有办法检查一个实体是否在外部表中被引用(检查外键关系是否存在)并从这些相关的外部表中获取实体或 id?
情况如下:我有一堆实体要删除。但其中一些可以在其他表中引用。我正在以一种通用的方式工作,所以我不知道我正在使用哪个模型,也不知道它有什么链接。我想要的是过滤这些无法删除的实体,并从与我正在工作的模型关联的外部表中获取实体或 ID,以便在处理后显示它们。
代码基本上是这样的:
public function removeEntities($table_alias, $data){
$data = $this->checkData($data); //do some work...
$table_object = TableRegistry::get($table_alias);
// here I'd like to get the entities or ids from tables related to $table_object that can't be deleted
$data = $this->check_relationship($table_object, $data);
$table_object->deleteAll(["common_index IN" => $data["to_delete"]]); //remove the guys that are related to nobody
return $data["cant_delete"]; //return the foreign entities/ids which are related to entities from $table_object
}
如果我不清楚,请在 cmets 帮助我。我很难提出这个问题。
假设我正在使用Companies,我必须删除其中一些:
我有一个要删除的公司列表,但我可以只删除与Workers 和Clients 无关的公司。另外,我想检索链接的Workers 和Clients。
【问题讨论】:
-
您可能想查看
information_schema。
标签: php mysql orm foreign-keys cakephp-3.0