dongwen1909 2016-12-23 17:52 采纳率: 0%
浏览 31
已采纳

检查实体是否在外表中引用并获取实体

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:

enter image description here

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.

  • 写回答

1条回答 默认 最新

  • douan1893 2016-12-24 01:33
    关注

    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;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 GD32 SPI通信时我从机原样返回收到的数据怎么弄?
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?