I am working on a magento admin module and currently I am running a database query in a sloppy fashion, by directly loading a php file and connecting with a php file outside of the module:
<?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);
?>
Can someone please explain a better way to write this into the module? I know I can make the connection much easier (more secure?) using magentos methods. Can I put this whole query directly in the controller for the module? Something like this? :
public function queryAction()
{
$readConnection = $resource->getConnection('core_read');
$query = ("SELECT cpsl.parent_id AS 'foo'
...
}
Yes, you can do what you propose.
I have something like the following:
edited to add
You can make the call directly from the controller by defining the methods in the controller and calling them with something like
$this->doAQuery();I’m a pretty big fan of putting things in the right place for easier maintainability, though, so I’ll outline the steps needed to do that.I’m going to assume you know how to/can read the docs on how to create a skeleton module, but I may end up talking down a bit. Apologies in advance.
For the sake of argument, I’m going to call our example module Zac_Example. So we’ll pretend we have a module in app/code/local/Zac/Example. Any further paths will assume we’re starting in that directory.
First, you need to define a model (I guess you could use a helper if you prefer) and controller, so we define those in etc/config.xml
Now we define our model in Model/Query.php, which is
Foofrom above, but using the Magento naming convention:Now, having a model, we can set up a controller. We’ll call the controller test, so the following goes in controllers/TestController.php. The actions, we’ll call foo and bar.
Given that particular set of facts, the URLs in question would be
http://yourserver/example/test/fooandhttp://yourserver/example/test/bar. If we had named the controller fileIndexController.php, they would behttp://yourserver/example/index/fooandhttp://yourserver/example/index/bar.If you only have one action you need to make available, you can name the controller file IndexController.php and the method in the controller indexAction and use the URL
http://yourserver/example/.I’m shooting from the hip, so don’t be surprised if there’s at least one braino or typo somewhere.