1

我试图创建一对多的连接,但它的工作原理很奇怪。

我怀疑用户类有一个国家,而国家类有很多用户。但是 User->Country 会返回一个包含一个 Country 的数组(Doctrine Collection,而不是 Record)。

有谁知道为什么?

  • 我需要 CountryUser 对象,而且我知道,这种关系可以在没有其他对象的情况下建立。
  • 我没有为 Doctrine 使用 YAML 格式,类是手动制作的。

    类用户扩展 sfDoctrineRecord {

    public function setTableDefinition()
    {
        $this->setTableName('user');
        $this->hasColumn('id', 'integer', 5, array(
             'type' => 'integer',
             'primary' => true,
             'unsigned' => true,
             'autoincrement' => true,
             'length' => 5,
             ));
        $this->hasColumn('fbid', 'string', 40, array(
             'type' => 'string',
             'length' => 40,
             #'notnull' => true,
             #'unique' => true,
             ));
    }
    
    public function setUp()
    {
        parent::setUp();
        $this->hasOne('Country', array(
             'local' => 'user_id',
             'foreign' => 'country_id',
             'refClass' => 'CountryUser'
        ));
    
        $timestampable0 = new Doctrine_Template_Timestampable(array(
             ));
        $this->actAs($timestampable0);
    }
    

    }

    类 Country 扩展 sfDoctrineRecord { public function setTableDefinition() { $this->setTableName('country');

        $this->hasColumn('id', 'integer', 5, array(
             'type' => 'integer',
             'primary' => true,
             'unsigned' => true,
             'autoincrement' => true,
             'length' => 5,
             ));
    
        $this->hasColumn('name', 'string', 10, array(
          'type' => 'string',
          'length' => 10,
          'unique' => true,
          #'notnull' => true,
        ));
    }
    
    public function setUp()
    {
        parent::setUp();
    
        $this->hasMany('User as Users', array(
             'local' => 'country_id',
             'foreign' => 'user_id',
             'refClass' => 'CountryUser'
         ));
    
        $timestampable0 = new Doctrine_Template_Timestampable(array(
             ));
        $this->actAs($timestampable0);
    }
    

    }

    类 CountryUser 扩展 sfDoctrineRecord {

    公共函数 setTableDefinition() {

    $this->setTableName('country_user');
    
    $this->hasColumn('user_id', 'integer', 5, array(
      'notnull' => true,
      'unsigned' => true,
      'length' => 5,
      'type' => 'integer',
      'primary' => true,
    ));
    
    $this->hasColumn('country_id', 'integer', 5, array(
      'type' => 'integer',
      'notnull' => true,
      'unsigned' => true,
      'length' => 5,
      'primary' => true,
    ));
    

    } }

4

1 回答 1

1

我需要 CountryUser 对象,而且我知道,这种关系可以在没有其他对象的情况下建立。

但这就是你问题的答案。您通过使用这样的方式将其设置为多对多,refClass因此您在访问关系时总是会获得 Doctrine_Collection。这就是它的工作原理,我不确定为什么你会期望一个对象。

如果集合中只有一条记录,您可能能够覆盖访问器以仅返回一条记录,但这很可能会破坏事情,因为一切都将期望从该访问器返回 Doctine_Collection:

class User extends sfDoctrineRecord {
    public function getCountry(
       $coll = $this->_get('Country');
        if($coll->count() == 1){
          return $coll->getFirst();
        }
        else
        {
          return $coll;
        }
    }
}

真正的解决方案是使用正确的关系类型......

另外...如果您使用的是 Symfony,为什么您不使用 YAML 定义?它更易于使用和管理。

于 2011-01-16T22:16:34.037 回答