2011-07-23 4 views
1

Zend Frameworkでモデルを取得しました。Zend_Db_Table$this->_name = 'tableA'です。別のテーブルからzendフレームワークを更新する

私はどれくらい長くしているのですか?insert()update()、またはdelete()です。どのように私は別のテーブルからの値に基づいてメインテーブルを更新することを実現することができます..?私はupdate()方法のためのparamsを構築することができますどのように

UPDATE tableA SET fieldA = tableB.newValue 
FROM tableB 
WHERE tableA.someValue = tableB.someIndex // it will be complicate manipulation 
    AND tableA.index = ..... 

:それはこのようになります可能性があり、生のSQLクエリで

parent::update($data, $where); 

答えて

1

ありますparent::update()メソッドのparamsをビルドして最終更新クエリを取得する方法の組み合わせはありません。その理由は、Dbテーブルupdateメソッドは、$data$whereという変数をDbアダプターのupdateメソッドに渡すためです。アダプタのupdateメソッドでは、追加情報を追加する余地がありません。 paramsをまったくハックすることはできません

カスケード更新でテーブル関係を使用できない場合は、最善の策は、Dbアダプターを拡張し、これらのタイプの更新を処理するための新しいメソッドを作成することです。これはうまくいくはずです。

/** 
* Add this method to you custom adapter 
* Direct copy of update method with integration of $from 
* @see Zend_Db_Adapter_Abstract::update 
**/ 
public function updateFrom($table, $from, array $bind, $where = '') 
{ 
    /** 
    * Build "col = ?" pairs for the statement, 
    * except for Zend_Db_Expr which is treated literally. 
    */ 
    $set = array(); 
    $i = 0; 
    foreach ($bind as $col => $val) { 
     if ($val instanceof Zend_Db_Expr) { 
      $val = $val->__toString(); 
      unset($bind[$col]); 
     } else { 
      if ($this->supportsParameters('positional')) { 
       $val = '?'; 
      } else { 
       if ($this->supportsParameters('named')) { 
        unset($bind[$col]); 
        $bind[':col'.$i] = $val; 
        $val = ':col'.$i; 
        $i++; 
       } else { 
        /** @see Zend_Db_Adapter_Exception */ 
        require_once 'Zend/Db/Adapter/Exception.php'; 
        throw new Zend_Db_Adapter_Exception(get_class($this) ." doesn't support positional or named binding"); 
       } 
      } 
     } 
     // Reason #1 you can't hack into $data array to pass reference to a table 
     $set[] = $this->quoteIdentifier($col, true) . ' = ' . $val; 
    } 

    $where = $this->_whereExpr($where); 

    /** 
    * Build the UPDATE statement 
    */ 
    $sql = "UPDATE " 
     . $this->quoteIdentifier($table, true) 
     . ' SET ' . implode(', ', $set) 
     . ' FROM ' . $this->quoteIdentifier($from, true) // My only edit 
     . (($where) ? " WHERE $where" : ''); // Reason #2 no room in where clause 

    /** 
    * Execute the statement and return the number of affected rows 
    */ 
    if ($this->supportsParameters('positional')) { 
     $stmt = $this->query($sql, array_values($bind)); 
    } else { 
     $stmt = $this->query($sql, $bind); 
    } 
    $result = $stmt->rowCount(); 
    return $result; 
} 

/** Add this to your extended Zend_Db_Table **/ 
public function update(array $data, $where) 
{ 
    $tableSpec = ($this->_schema ? $this->_schema . '.' : '') . $this->_name; 
    $from  = 'schema.name'; // Get from table name 
    return $this->_db->updateFrom($tableSpec, $from, $data, $where); 
} 

注:私はこれをテストしていないが、私は期待通りに動作しますかなり確信しています。何か問題があれば、私に知らせてください。アダプターの更新メソッドをコピーしたので、私は先に進み、それらのパラメーターをハックすることができない理由のメモを追加しました。

EDIT 私はほとんど忘れてしまった。すべてのアダプターはユニークなので、アダプターの更新メソッドで確認する必要があります。私はちょうどZend_Db_Abstractからコピーしました。

関連する問題