Is there a way to check whether a entity is referenced in a foreign table (check foreign key relationship existence) and get the entities or ids from these foreign tables that are related?
Here is the situation: I have a bunch of entities to delete. But some of them can be referenced in other tables. I'm working in a generic way, so I don't know which model I'm working with neither what are the linkings It has. What I want is to filter these entities that can't be deleted and get the entities or ids from the foreign tables that are associated to the model I'm working so I can show them after the proccess.
The code would be basically in this way:
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
}
If I wasn't clear, please help me at comments. It was very hard for me to develop this question.
Suppose I'm working with Companies
and I have to delete some of them:
I have a list of companies to remove, but I can remove just the ones that are not related to Workers
and Clients
. Also, I want to retrieve the Workers
and Clients
that are linked.
Here's the function I wrote for doing this:
/**
* Use model associations to determine whether a record can be deleted.
*
* @param mixed $id The id of the record to delete
* @param array $ignore Optional list of models to ignore
* @param array $ignoreDeep Optional list of models to ignore IF they themselves have no dependencies
* @return mixed Text list of dependencies found, or false if none
*/
public function dependencies($id, array $ignore = [], array $ignoreDeep = []) {
if ($id === null) {
return false;
}
$dependencies = [];
$associations = $this->associations();
foreach ($associations->type('BelongsToMany') as $association) {
$class = $association->name();
$foreign_key = $association->foreignKey();
$through = $association->junction()->alias();
$dependent = $association->junction()->find()->where(["$through.$foreign_key" => $id]);
$association_conditions = $association->conditions();
if (!empty($association_conditions)) {
$dependent->andWhere($association_conditions);
}
if (in_array($class, $ignoreDeep) || array_key_exists($class, $ignoreDeep)) {
foreach ($dependent->extract($association->targetForeignKey())->toArray() as $deepId) {
if (array_key_exists($class, $ignoreDeep)) {
$deep = $association->dependencies($deepId, $ignoreDeep[$class]);
} else {
$deep = $association->dependencies($deepId);
}
if ($deep) {
$dependencies[] = __('{0} {1} (with {2})', __(Inflector::delimit(Inflector::singularize($class), ' ')), $deepId, $deep);
}
}
} else if (!in_array($class, $ignore)) {
if ($dependent->count() > 0) {
$dependencies[] = $dependent->count() . ' ' . __(Inflector::delimit($class, ' '));
}
}
// BelongsToMany associations also create HasMany associations for the join tables.
// Ignore them when we get there.
$ignore[] = $through;
}
foreach ($associations->type('HasMany') as $association) {
$class = $association->name();
$foreign_key = $association->foreignKey();
$dependent = $association->target()->find()->where(["$class.$foreign_key" => $id]);
$association_conditions = $association->conditions();
if (!empty($association_conditions)) {
$dependent->ansWhere($association_conditions);
}
if (in_array($class, $ignoreDeep) || array_key_exists($class, $ignoreDeep)) {
foreach ($dependent->extract($association->primaryKey())->toArray() as $deepId) {
if (array_key_exists($class, $ignoreDeep)) {
$deep = $association->dependencies($deepId, $ignoreDeep[$class]);
} else {
$deep = $association->dependencies($deepId);
}
if ($deep) {
$dependencies[] = __('{0} {1} (with {2})', __(Inflector::delimit(Inflector::singularize($class), ' ')), $deepId, $deep);
}
}
} else if (!in_array($class, $ignore)) {
if ($dependent->count() > 0) {
$dependencies[] = $dependent->count() . ' ' . __(Inflector::delimit($class, ' '));
}
}
}
if (!empty($dependencies)) {
return implode(', ', $dependencies);
}
return false;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.