PHP Doctrine:如何设置refClass额外的列?

时间:2011-02-03 16:30:49

标签: php doctrine associations

我有以下架构

User:
  columns:
      id:
           type: integer
           primary: true
      name: string
  relations:
     UserGroup:
      local: userGroup_id
      foreign: id
      refClass: User2Group

UserGroup:
  columns:
      id:
           type: integer
           primary: true
      name: string
  relations:
     User:
      local: user_id
      foreign: id
      refClass: User2Group

User2Group:
    columns:
        user_id:
           type: integer
           primary: true
        userGroup_id:
            type: integer
            primary: true
        extraColumn: string

我正在尝试

$user=new Model_User();
        $user->name='user';

        $user->UserGroup[0]->name='group';
        $user->UserGroup[0]->extraColumn='test';

        $user->save();

但它给了我一个例外“Doctrine_Record_UnknownPropertyException”,消息“未知记录属性/相关组件”extraColumn“on”UserGroup“”,我做错了什么? (顺便说一句,我试图将本地/外国改为其他任何东西,但它仍无效)

1 个答案:

答案 0 :(得分:3)

Doctrine并不真正支持连接表上的额外列。但您可以使用Doctrine_Query访问和更新值。

  Doctrine_Query::create()
    ->update('User2Group')
    ->set('extraColumn', 'Dude its working')
    ->where('user_id = ?', $user['id'])
    ->andWhere('userGroup_id = ?', $userGroup['id'])
    ->execute();

要获得此值,您需要执行选择查询。

  Doctrine::getTable('User')->createQuery('u')
    ->addSelect('u.*, ug.*, u2g.extraColumn')
    ->leftJoin('u.UserGroup ug')
    ->leftJoin('ug.User2Group u2g WITH u2g.user_id = ?', $user['id'])
    ->execute();