简体   繁体   中英

PHP Doctrine : how to set refClass extra columns?

I have the following schema

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

and I am trying to do

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

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

        $user->save();

but it gives me an exception "Doctrine_Record_UnknownPropertyException" with message "Unknown record property / related component "extraColumn" on "UserGroup"", what am I doing wrong ? (I have by the way tried to change the local/foreign to anything else it still won't work)

Doctrine doesn't really support extra columns on the join table. But you can access and update the values using Doctrine_Query.

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

To get this value you need to do a select query.

  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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM