带关系的 DQL 更新

发布于 2024-09-07 00:35:56 字数 1150 浏览 1 评论 0原文

以下模型:

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'

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

Following Models:

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'
        ));
    }
}

And I try following DQL:

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

Result:

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

Basicly I just want to update the "iron" attribute from the Field-Owner according to the fields' value

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

马蹄踏│碎落叶 2024-09-14 00:35:56

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

这可能不是最好的方法,但是,这是我所做的

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

I am guessing you can't reference other tables like that in your query.

This may not be the best way but, here is what I do

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

EDIT:
Actually I don't know if that will work... this is more like what I do

$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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文