I have a task to convert the standalone PHP files to Magento's MVC. These PHP files were created by another developer. The code in the PHP file accesses the database, converts the result into JSONP format and forward it to the frontend developer.
I don't have any knowledge of Magento's MVC. Is this task of conversion similar to the modules in the app/code/core/Mage
in the Magento folder?? How can I do this? Is the magento MVC the same as the PHP MVC?
I am including the php file that I need to convert to Magento MVC. So it will be easier for you to understand..
<?php header('content-type: application/json; charset=utf-8');
$link = mysqli_connect("localhost", "db_username", "password", "db_dbname");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$pid = $_REQUEST['prodid'];
/* Select queries return a resultset */
$result = mysqli_query($link, "SELECT round(rating_summary / 20) AS search_rating FROM review_entity_summary where store_id = 1 and entity_pk_value=" . $pid);
// printf("Select returned %d rows.\n" . "<br>\n", mysqli_num_rows($result)) . "<br>\n";
//$string = $_REQUEST['varname'];
$rows = array();
/* while ($row = $result->fetch_row()) {
printf("%s\n", $row[0]);
}*/
//while($r = mysql_fetch_assoc($result)) {
while ($r = mysqli_fetch_assoc($result)) {
$rows = $r;
//print_r ($rows) . "<br>\n";
}
$json_data = json_encode($rows);
print_r ($json_data);
/* free result set */
// mysqli_free_result($result)
mysqli_close($link);
?>
So how can I convert this file to Magento's MVC style?? IS this necessary to convert this file to magento MVC?
Interesting question. IMHO, you are in the position I was with regard to Magento some time ago. To write and use mySql queries against the Magento database requires nothing from the MVC model. In our world, I call these voodoo files. I create tools for my staff with very specific purpose and non-published URLs. You can put them anywhere in the file system, just make sure that the directory name and file name do not have any magic meaning within Magento or you might inadvertently disable some function. My sales tax reporting tool is exactly like yours, where you open a connection to the mySql database and make direct, manual queries against it.
RS's answer is a great primer for getting into the MVC. I have started using it for my voodoo tools as well. Making queries the "Magento way" via their controllers can make getting information out of the EAV much easier than trying to put together the info via manual queries because the infrastructure is already coded for you. An example is customer information! I figured out sales tax queries (which was no minor undertaking), but getting a complete name, address, and email was absurd.
I created an export tool which outputs JSON data. I then CURL it from the server at work to store the sales into a mysql database. From there, they get imported into quickbooks. This order export tool is 100% Magento objects like from the example below.
Here is the code I used in my voodoo customer info tool:
I do most of my learning by google, trial, and error; so please consider that "my way" is simply one that works for me and probably still isn't the best or easiest way. Also, I still don't completely understand how some of the objects work, hence debug output that's commented out.
My advice would be to not try too hard to make something for the "frontend" facing the customer. Getting pages to display and work correctly in Magento is a miserable experience. If you're able to do sneaky hidden voodoo tools, try to do that. If you're forced to make a module, there are a couple dozen step by step how-to's on the net that can guide you.
I think what they are asking you to do, is to convert code that look like
In to Magento MVC (module)
If you're new to OOP, take a look here: http://www.php.net/manual/en/language.namespaces.rationale.php
Name of new custom module - try to keep at least first letter capital, or there WILL BE truble with Magento's understanding
In classic MVC architecture, this represents View part of MVC
This is fairly easy to understand, if not, have fun: http://en.wikipedia.org/wiki/Model%E2%80%93View%E2%80%93Controller
Contains the most significant part in Magento's MVC architecture - the xml field that will connect all things together
Intended for files that contain repeatable routines or simple procedural methods
Same thing as for controller, take a look at the link above
This was interesting thing to find out what's it for, it's to define custom database tables and process any upgrades to your extension.
Contains all Modules included in Magento - here's where it all really begins for our module
see http://inchoo.net/ecommerce/magento/basic-folder-structure-for-new-magento-module/