2012-03-01 11 views
0

私はmagento管理モジュールで作業していますが、現在、PHPファイルを直接ロードし、モジュール外のPHPファイルに接続することによってデータベースクエリを実行しています:コントローラから直接sqlクエリを実行

<?php 
header("Content-type: text/xml"); 
$host   = "localhost"; 
$user   = "root"; 
$pw    = "foo"; 
$database  = "db"; 
$link   = mysql_connect($host,$user,$pw) or die ("Could not connect."); 
$db_selected = mysql_select_db($database, $link); if (!$db_selected) { 
    die ('Can\'t use ' . $database . ' : ' . mysql_error()); } 


// connect to database 
$link = mysql_connect($host . ":" . $port,$user,$pw) or die ("Could not connect."); 

// Select DB 
$db_selected = mysql_select_db($database, $link); if (!$db_selected) { 
    die ('Can\'t use ' . $database . ' : ' . mysql_error()); } 

// Database query 
$query = ("SELECT cpsl.parent_id AS 'foo' 
    , cpe.type_id AS 'bar' 
    , LEFT(cpe.sku, 10) AS 'color' 
    .... 
GROUP BY 1,2,3,4,5,6,7,8,9 
ORDER BY 1,2,3,4,5,6,7,8,9 
;"); 

// Execute query 
$result = mysql_query($query, $link) or die("Could not complete database query"); 

// Populate array 
while(($resultArray[] = mysql_fetch_assoc($result)) || array_pop($resultArray)); 

    $doc = new DOMDocument(); 
    $doc->formatOutput = true; 

    $r = $doc->createElement("DATA"); 
    $doc->appendChild($r); 

    foreach($resultArray as $product) 
    { 
    $b = $doc->createElement("ITEM"); 

    // MagentoID 
    $magento_id = $doc->createElement("MAGENTO_ID"); 
    $magento_id->appendChild(
    $doc->createTextNode($product['MagentoID']) 
); 
    $b->appendChild($magento_id); 
.... 

} 

// Save XML 
    echo $doc->saveXML(); 

// Close connection 
mysql_close($link); 

?> 

誰かがこれをモジュールに書き込むためのより良い方法を説明できますか?私はmagentosメソッドを使用して、接続をもっと簡単に(より安全に)できることを知っています。この全体のクエリをモジュールのコントローラに直接入れることはできますか?このようなもの? :

public function queryAction() 
    { 
$readConnection = $resource->getConnection('core_read'); 
$query = ("SELECT cpsl.parent_id AS 'foo' 
... 
} 
+0

"Populate array"行では、すべてのレコードがポップされるので、 '$ resultArray'のように見えます。これは実際には機能しますか? – clockworkgeek

+0

実際に動作します。私はちょうどこのphpファイルからmagentoに移動したいと思います。私はwierdosから私は正しい軌道にある答えを参照してくださいしかし、私はまだそれがコントローラになったら、それを呼び出す方法を理解していない。 – Zac

答えて

3

はい、あなたが提案することができます。

私は、次のようなものがあります:あなたは、コントローラのメソッドを定義し、$this->doAQuery();のようなものでそれらを呼び出すことにより、コントローラから直接電話をかけることができ

を追加するために編集した

class Foo { 
    protected $db; 

    public function _construct() { 
     /* Change core_write to core_read if you're just reading */ 
     $this->db = Mage::getSingleton('core/resource')->getConnection('core_write'); 
    } 

    private function doAQuery() { 
     $sql = "SELECT * FROM foobar f;"; 
     $data = $this->db->fetchAll($sql); 
     /* do something with the data here */ 
    } 

    private function doAQueryADifferentWay() { 
     $sql = $this->db->select(); 
     $sql->from(array('f' => 'foobar')); 
     $data = $this->db->fetchAll($sql); 
     /* do something with the data */ 
    } 
} 

を私はメンテナンスを容易にするために物事を適切な場所に置くのはかなり大好きですが、そうするために必要な手順を概説します。

スケルトンモジュールを作成する方法についてドキュメントを読む方法/読むことができますが、少し話が終わることがあります。事前にお詫び申し上げます。

議論のために、サンプルモジュールZac_Exampleを呼び出します。ですから、app/code/local/Zac/Exampleにモジュールがあるとしましょう。それ以上のパスは、そのディレクトリで開始するとみなします。

まず、あなたは、モデル(私はあなたが好む場合は、ヘルパーを使うことができると思います)とコントローラを定義する必要がありますので、我々は今、私たちが私たちのモデルを定義など/ config.xmlにそれら

... 
    <frontend> 
    <routers> 
     <zac_example> 
     <use>standard</use> 
     <args> 
      <module>Zac_Example</module> 
      <!-- Mind the capital N, it gets me every time --> 
      <frontName>example</frontName> 
     </args> 
     </zac_example> 
    </routers> 
    </frontend> 
    <global> 
    <models> 
     <zac_example> 
     <class>Zac_Example_Model</class> 
     </zac_example> 
    </models> 
    </global> 
... 

を定義しますモデル/上からFooあるQuery.php、しかし、Magentoの命名規則を使用しては:

class Zac_Example_Model_Query extends Mage_Core_Model_Abstract { 
    protected $db; 

    /* you don't have to do this, as you can get the singleton later if you prefer */ 
    public function __construct() { 
     $this->db = Mage::getSingleton('core/resource')->getConnection('core_write'); 
    } 

    public function doAQuery() { 
     /* If you chose not to do this with the constructor: 
     * $db = Mage::getSingleton('core/resource')->getConnection('core_write'); 
     */ 

     $sql = "SELECT * FROM foobar f;"; 
     /* or $db->fetchAll($sql); */ 
     $this->db-fetchAll($sql); 
     /* do something with the data here */ 
     return $response 
    } 

    public function doAQueryADifferentWay($somerequestdata) { 
     $sql = $this->db->select(); 
     $sql->from(array('f' => 'foobar')); 
     $sql->where('f.somedata', array('eq' => $somerequestdata)); 

     $data = $this->db->fetchAll($sql); 
     /* do something with the data */ 
    } 
} 

今、モデルを持つ、我々はコントローラを設定することができます。コントローラーをテストと呼ぶので、以下はcontrollers/TestController.phpに入ります。アクションは、fooバーと呼びます。

class Zac_Example_TestController extends Mage_Core_Controller_Front_Action { 

    public function fooAction() { 
     $model = Mage::getModel('zac_example/query'); 
     $result = $model->doAQuery(); 
     $this->getResponse()->setBody(Zend_Json::encode($result)); 
     $this->getResponse()->sendResponse(); 
     exit; // We're done, right? 
    } 

    /* This assumes the request has a post variable called 'whatever' */ 
    public function barAction() { 
     $model = Mage::getModel('zac_example/query'); 
     $param = $this->getRequest()->getParam('whatever'); 
     $result = $model->doAQueryADifferentWay($param); 
     $this->getResponse()->setBody(Zend_Json::encode($result)); 
     $this->getResponse()->sendResponse(); 
     exit; // We're done, right? 
    } 

は事実の特定のセットは、問題のURLがhttp://yourserver/example/test/foohttp://yourserver/example/test/barだろうことを考えます。コントローラーファイルの名前をIndexController.phpとすれば、http://yourserver/example/index/foohttp://yourserver/example/index/barとなります。

あなただけが利用できるようにする必要がある1つのアクションを持っている場合は、コントローラファイルIndexController.phpとコントローラindexActionのメソッドに名前を付け、URL http://yourserver/example/を使用することができます。

私は股関節から撃っています。だからこそどこかの脳やタイプミスがあれば驚かないでください。

+0

ありがとうございました。しかし、私はまだ私はどのようにクエリを呼び出すかを取得しないでください。たとえば、私は現在、ajax、url:theurl.phpから読み込んでいますが、あなたのメソッドでそれを行うと、これを実行するパスは何でしょうか?それは/ doAQueryですか? – Zac

+0

これを行う「正しい」方法は、このようなコードをモデルまたはおそらくヘルパーに置き、それをコントローラから呼び出すことです。コントローラでは、モジュールのIndexController.phpでdoQueryActionのようなメソッド名を付けます。あなたのモジュールのfrontNameが 'theurl'と設定されていれば、' http:// yoursite/theurl/index/doQueryAction'を使ってアクセスします。 – wierdo

+0

本当にありがとう、本当にありがとうございます。私はこれらのマゼンタの森で迷子になり、これは私にとって有益なリソースになるはずです。 – Zac

関連する問題