多个外键到同一个表Gas Orm [英] Multiple Foreign Key to same table Gas Orm

查看:210
本文介绍了多个外键到同一个表Gas Orm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于这个mornong我面临一个非常大的问题。我使用CodeIgniter来开发一个网站,并为数据库提供GAS ORM。
我基本上有两个表。一个名为池,一个名为伙伴。我有这两个表之间的两个关联,所以我有两个外键在我的表合作伙伴引用表池。



池(#id:整数,名称:varchar )
合作伙伴(#id:integer,associated_pool_id => Pool,futur_associated_pool_id => Pool)。

由于我有两个引用同一个表,不要命名外键pool_id。所以在与Gas ORM的关系中,我必须指定列的名称。我这样做,但它不工作...
这是我做的:

 类合作伙伴扩展ORM {

public $ primary_key ='id';
public $ foreign_key = array('\\Model\\Pool'=>'associated_pool_id','\\Model\\Pool'=>'future_associated_pool_id');

函数_init()
{

//关系定义
self :: $ relationships = array(
'associated_pool'=> ; ORM :: belongs_to('\\Model\\\Pool'),
'future_association_pool'=> ORM :: belongs_to('\\Model\\Pool'),
);

self :: $ fields = array(
'id'=> ORM :: field('auto [11]'),
'name'=> ORM :: field('char [255]'),
'associated_pool_id'=> ORM :: field('int [11]'),
'future_associated_pool_id'=> ORM :: field 'int [11]')
);


$ / code $ / pre

在我的Pool类中:

  class Pool extends ORM {

public $ primary_key ='id';

函数_init()
{
//关系定义
self :: $ relationships = array($ b $ associated_pa​​rtner'=> ORM :: has_many('\\Model \\Partner'),
'future_associated_pa​​rtner'=> ORM :: has_many('\\Model \\Partner'),
) ;

self :: $ fields = array(
'id'=> ORM :: field('auto [11]'),
'name'=> ORM :: field('char [50]'),
);




$ b我有一个测试控制器测试是否一切正常: / b>

 类欢迎扩展CI_Controller {
public function index()
{

$ pool = \Model\Pool :: find(1);
echo $ pool-> name;
$ partners = $ pool-> associated_pa​​rtner();
var_dump($ partners);

$ / code>

但是我有个错误说:


错误编号:1054

Champ'partner.pool_id'inconnu dans where where clause
$ b

SELECT * FROM 伙伴 WHERE 伙伴 pool_id IN(1)



我不知道如何指定Gas ORM,它不应该把pool_id 但是associated_pool_id....

感谢您的帮助!!!!!!!!!!!!
<解决方案我不知道,如果这个话题仍然是最新的,有趣的一些你,但一般来说,我有完全相同的问题。

我决定将Gas ORM与CodeIgniter结合使用。由于我的数据库结构是给定的,并没有遵循Gas的table_pk约定,所以我必须自己定义一个外键,它指的是我的自定义数据库外键。然而,它的定义对任何事物都没有影响。像上面的错误一样,映射程序无法构建正确的SQL语句。该语句与您的类似:

  SELECT * FROM partner WHERE partner.pool_id IN(1)
orm.php 的构造函数处理实体中定义的每个主键和外键。 在第191行中,代码调用if子句与功能PHP。由于主键总是被定义的,并且在语句中没有否定,所以每次跳过子句的内部部分。但是,内部部分负责自定义外键。

长话短说,我在orm.php的第191行中添加了一个否定符(!),它使我转向下面的代码:

  if(!empty($ this-> foreign_key))
{
//验证外键的一致性命名约定识别器
$ foreign_key = array();
$ b foreach($ this-> foreign_key as $ namespace => $ fk)
{
$ foreign_key [strtolower($ namespace)] = $ fk;
}

$ this-> foreign_key = $ foreign_key;
}
else
{
//如果到目前为止我们还没有任何键,
//那么希望有人真的遵循Gas convention
/ / $定义他的实体关系(是的,你!)
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->表;
$ child_key = $ child_instance-> primary_key;

$ this-> foreign_key [strtolower($ child_name)] = $ child_table。'_'。$ child_key;






,这个小小的修复帮了我很多,我希望你们中的一些人也可以利用这个提示。

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", and one named "partners". I am having two associations between these two tables, so I have two foreign keys in my table Partners referencing the table pool.

Pool(#id:integer, name:varchar) Partners(#id:integer, associated_pool_id=>Pool, futur_associated_pool_id=>Pool).

As I have two references to the same table, I can't name the foreign keys "pool_id". So in my relationships with Gas ORM, I have to specify the names of the columns. I do it, but it doesn't work... Here is what I do:

class Partner extends ORM {

public $primary_key = 'id';
public $foreign_key = array('\\Model\\Pool' => 'associated_pool_id', '\\Model\\Pool' => 'future_associated_pool_id');

function _init()
{

    // Relationship definition
    self::$relationships = array(
            'associated_pool' => ORM::belongs_to('\\Model\\Pool'),
            'future_association_pool'  => ORM::belongs_to('\\Model\\Pool'),
    );

    self::$fields = array(
        'id' => ORM::field('auto[11]'),
        'name' => ORM::field('char[255]'),
        'associated_pool_id' => ORM::field('int[11]'),
        'future_associated_pool_id' => ORM::field('int[11]')
    );

}

and in my Pool class :

class Pool extends ORM {

public $primary_key = 'id';

function _init()
{
    // Relationship definition
    self::$relationships = array(
            'associated_partner' => ORM::has_many('\\Model\\Partner'),
            'future_associated_partner'  => ORM::has_many('\\Model\\Partner'),
    );

    self::$fields = array(
        'id' => ORM::field('auto[11]'),
        'name' => ORM::field('char[50]'),
    );

}

I have a test controller testing if everything is okay:

class Welcome extends CI_Controller {
public function index()
{

    $pool = \Model\Pool::find(1);
    echo $pool->name;
    $partners = $pool->associated_partner();
    var_dump($partners);
}

But I have an error saying:

Error Number: 1054

Champ 'partner.pool_id' inconnu dans where clause

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

I don't know how to specify to Gas ORM that it shouldn't take "pool_id" but "associated_pool_id"....

Thank you for your help!!!!!!!!!!!!

解决方案

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.

这篇关于多个外键到同一个表Gas Orm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆