DQL更新与关系

时间:2010-06-28 20:46:13

标签: php doctrine dql

以下型号:

class User extends Doctrine_Record {
    public function setTableDefinition() {
        $this->hasColumn ( 'iron', 'integer', 4 );
    }

    public function setUp() {
        $this->hasMany ('Field as Fields', array(
            'local' => 'id',
            'foreign' => 'owner_id'
        ));
    }
}

class Field extends Doctrine_Record {
    public function setTableDefinition() {
        $this->hasColumn('owner_id','integer',4);
        $this->hasColumn('ressource_id','integer',4);
        $this->hasColumn('ressource_amount','integer','2');
    }

    public function setUp() {
        $this->hasOne('User as Owner',array(
                'local' => 'owner_id',
                'foreign' => 'id'
        ));
    }
}

我尝试遵循DQL:

$sqlRessourceUpdate = Doctrine_Query::create()
->update('Field f')
->set('f.Owner.iron','f.Owner.iron + f.ressource_amount')
->where('f.ressource_id = ?',1);

结果:

'Doctrine_Query_Exception' with message 'Unknown component alias f.Owner'

基本上我只是想根据字段的值更新字段所有者的“铁”属性

1 个答案:

答案 0 :(得分:1)

我猜您无法在查询中引用其他类似的表。

这可能不是最好的方法,但是,这就是我的工作

$q = Doctrine_Query::create()
    ->select('*')
    ->from('Field')
    ->where('ressource_id = ?',1); //btw resource has one 's'

$field = $q->fetchone();

$field->Owner['Iron'] += $field->ressource_amount;
$field->save();

编辑: 实际上我不知道这是否有用......这更像我的工作

$q = Doctrine_Query::create()
    ->select('*')
    ->from('Field')
    ->where('ressource_id = ?',1); //btw resource has one 's'

$field = $q->fetchone();

$user = $field->Owner;
$user['Iron'] += $field->ressource_amount; // I have never used a += like this, but in theory it will work.
$user->save();