Multiple Foreign Key to same table Gas Orm

前端 未结 1 1764
醉梦人生
醉梦人生 2021-01-16 13:00

Since this mornong i am facing a very big problem. I am using CodeIgniter to develop a website, and GAS ORM for the database. I have basically two tables. One named \"pool\"

相关标签:
1条回答
  • 2021-01-16 13:33

    I don't know, if this topic is still up to date and interesting to some of you, but in general, I had the exact same problem.

    I decided Gas ORM to be my mapper in combination with CodeIgniter. As my database structure was given and it was not following the table_pk convention of Gas, I had to define a foreign key by myself which shall refer to my custom database foreign key. However, the definition of it had no impact on anything. Like your error above, the mapper was not able to build the right SQL-statement. The statement looked similar to yours:

       SELECT * FROM partner WHERE partner.pool_id IN (1)
    

    Well, it seems like Gas ignores the self-defined foreign keys and tries to use the default table_pk convention. This means, it takes the table (in your case: pool) and the primary key (id) by merging it with a underscore character.

    I figured out, that the constructor of orm.php handles every primary and foreign key defined within the entities. In line 191, the code calls an if clause combined with the empty function of php. As the primary key is defined always and there is no negation in the statement, it skips the inner part of the clause every time. However, the inner part takes care of the self-defined foreign keys.

    Long story short, I added a negation (!) in line 191 of orm.php which leads me to the following code:

    if ( ! empty($this->primary_key))
        {
            if ( ! empty($this->foreign_key))
            {
                // Validate foreign keys for consistency naming convention recognizer
                $foreign_key = array();
    
                foreach($this->foreign_key as $namespace => $fk)
                {
                    $foreign_key[strtolower($namespace)] = $fk;
                }
    
                $this->foreign_key = $foreign_key;
            }
            else
            {
                // If so far we didnt have any keys yet, 
                // then hopefully someone is really follow Gas convention
                // while he define his entity relationship (yes, YOU!)
                foreach ($this->meta->get('entities') as $name => $entity)
                {
                    if ($entity['type'] == 'belongs_to')
                    {
                        $child_name     = $entity['child'];
                        $child_instance = new $child_name;
                        $child_table    = $child_instance->table;
                        $child_key      = $child_instance->primary_key;
    
                        $this->foreign_key[strtolower($child_name)] = $child_table.'_'.$child_key;
                    }
                }
            }
        }
    

    Well, this little fix helped me out a lot and I hope some of you can take advantage of this hint as well.

    0 讨论(0)
提交回复
热议问题