Обновление 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 ответ

Решение

Я предполагаю, что вы не можете ссылаться на другие таблицы, как это в вашем запросе.

Возможно, это не лучший способ, но вот что я делаю

$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();
Другие вопросы по тегам