2010-06-28 78 views
0

以下型號: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' 

Basicly我只是想更新的 「鐵」 屬性根據字段的值來自字段所有者

回答

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

謝謝。我現在正在做類似的事情。 應該注意的是,save()方法非常緩慢,並且會根據您的模型踢出很多查詢。 在我上面的例子中,它導致每個用戶大約有100個查詢。如果您想要演奏,請手動保存。 thx爲「s」校正順便說一句。總是弄錯了;) – SkaveRat 2010-06-30 01:51:09