Monday 23 March 2015

Customize Content Blocks in Magento Layouts

The ‘View’ of Magento’s MVC pattern implementation is divided in two parts: Layout and Template. The template represents a HTML block while layout defines the location of the block on a web page. Magento provides ultimate flexibility and re-usability of design by layouts defined in XML.
Layout is the tool with which you can assign content blocks to each structural block you create. Magento’s layout files are in the form of XML text-files and by modifying the layout you are able to move blocks around in a page and assign templates to the content blocks to produce markup for the structural blocks. In fact, with the help of a few layout files alone, you are able to modify the visual layout of every page in your store.
How Layout Works
Layout is comprised of default layout and layout updates that are made up of easy-to-learn XML tags. With these layout commands, you can modify/assign content block-structural block relationships and also control store-front functionalities such as loading and unloading of block-specific Javascripts to a page.
Layout files are separated on a per-module basis, every module bringing with it its own layout file (for instance ‘catalog.xml‘ is a layout file for the catalog module, ‘customer.xml’ is for the customer module…etc).
These layout files are located in app/design/frontend/your_interface/your_theme/layout/ and each file is further separated by handles, each handle (with the exception of <default>) assigning its nested updates to the according specific page in the store.
Some layout files may contain the <default> handle. When parsing the layout files, Magento first grabs the layout updates assigned in the <default> handle of almost all layout files, reading them in the order as assigned in app/etc/modules/Mage_All.xml.
It then parses the page-specific layout update, finalizing the building of a store page. The system is built this way in order to allow seamless addition and removal of modules without effecting other modules in the system.

Layout XML
Layout XML files can be found in app/design/frontend/[package]/[theme]/layout. Each Magento module may define its own layout XML file in the config.xml file.
<frontend>
<layout>
<updates>
<mymodule>
<file>mymodule.xml</file>
</mymodule>
</updates>
</layout>
</frontend>

Each layout XML file represents its own design update. For example, catalog navigation is part of the Mage_Catalog module and Mage_Catalog module defines a layout update file: catalog.xml. So the block for the catalog navigation and its location on the page is defined in catalog.xml.
Before the page is rendered, Magento loads all configured layout update files and its design updates to determine which block is to be rendered at which location.
Layout Handles
First level child elements of the &lt;layout&gt; node are called layout handles. Each layout handle represents an update to the page layout. It may define new blocks to be included in a page at a specific location or remove a specific block from the page. It may also define the modifications in existing blocks already included in the page by other layout XML files.
After Magento loads all layout XML files, it determines which layout handles need to be processed. Normally, the layout handle is selected based on the controller action being executed. In most cases, Magento loads the layout handle with name: [module_front_name]_[controller_name]_[action_name].
For example, when the contact us page is requested, then index action of index controller of Mage_Contacts is executed. Module front name of Mage_Contacts is contacts. So the layout handle to be processed for the contact us page is contacts_index_index.
For any page, Magento always processes the default layout handle. So the updates defined in default handle are processed for every page regardless of which part of the site we are browsing.

Source: CodeWebber

Friday 20 March 2015

An Introduction to PostgreSQL


MySQL is much more commonly provided by web hosts. PostgreSQL is a much more mature product. Apparently, MySQL is fast when concurrent access levels are low, and when there are many more reads than writes. On the other hand, it exhibits low scalability with increasing loads and write/read ratios.
PostgreSQL is relatively slow at low concurrency levels, but scales well with increasing load levels, while providing enough isolation between concurrent accesses to avoid slowdowns at high write/read ratios. It goes on to link to a number of performance comparisons because these things are very sensitive to conditions.
So if your decision factor is, “which is faster?” Then the answer is “it depends on the usage”. If it really matters, test your application against both.”
And if you really, really care, you get in two DBAs (one who specializes in each database) and get them to tune the crap out of the databases, and then choose. It’s astonishing how expensive good DBAs are, and they are worth every cent.
When it matters.
Which it probably doesn’t, so just pick whichever database you like the sound of and go with it; better performance can be bought with more RAM and CPU, and more appropriate database design, and clever stored procedure tricks and so on – and all of that is cheaper and easier for random-website-X than agonizing over which to pick, MySQL or PostgreSQL, and specialist tuning from expensive DBAs.
PostgreSQL database is Open Source product and available without cost. Postgres, developed originally in the UC Berkeley Computer Science Department, pioneered many of the object-relational concepts now becoming available in some commercial databases.
It provides SQL92/SQL99 language support, transactions, referential integrity, stored procedures and type extensibility.
PostgreSQL is an open-source descendant of this original Berkeley code.
Postgres Pros:
  • Transactions
  • Foreign keys ( via refint )
  • Triggers
  • Subselects
  • Views ( mostly )
  • User-defined datatypes
  • User-defined functions in a variety of languages: sql, c, pl/pgsql, pl/tcl
  • Sequences
  • Proper date handling
PostgreSQL feels quite a bit like Oracle. There is no SHOW TABLES, it’s \dt (IIRC). To quit it’s not QUIT or EXIT, it’s \q.
PostgreSQL didn’t have built in replication until recently.
Postgres doesn’t support ‘UPDATE a,b SET’ syntax. This would need to be translated into:
UPDATE a SET a.id=b.id FROM b WHERE a.f2 = b.f2; to work on Postgres.
Postgres does not provide a way to order columns inside the db.
Altering columns:
ALTER TABLE a ALTER COLUMN b TYPE integer;
ALTER TABLE a ALTER COLUMN b SET NOT NULL;
Postgres has no auto_increment option. Instead, use the type ‘serial’ For example:
CREATE TABLE a (
b INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT
);
would become
CREATE TABLE a (
b SERIAL PRIMARY KEY
);
plain INDEX’s cannot be added during table creation, Instead, you must issue a second query:
CREATE INDEX indexname ON tablename(columnname);
the syntax for defining constraints (such as UNIQUE or FOREIGN KEY) varies between the two databases.
For example, in your table definition if you had:
UNIQUE INDEX a1 (f1, f2) this would be changed to:
CONSTRAINT a1 UNIQUE (f1, f2) to be compatible with Postgres. Note that Postgres creates indices by default for
UNIQUE and FOREIGN KEY constraints.
There are many data types that do not exist in Postgres that you may be used to using in MySQL.
These include: blob, tinyint, integer unsigned
http://www.postgresql.org/docs/9.0/static/

Source: Codewebber

Thursday 19 March 2015

Store Procedures with CakePHP


I have always wondered how to store procedures with CakePHP, as I knew it will reduce the DBhits and will definitely make the application faster and usable.
I have searched on google if any class was available. Everyone was talking about $this->query(“call yourpeocedure()”); and they are getting results as desired, hence, it is fine and we can get the desired result.
But in case where stored procedure will have multiple select query, $this->query() will give an error. Here is a very small class for CakePHP by which we can integrate our MySQL stored procedure with CakePHP.
For now class is very abstract and if needed then we can modify the class. This class is handling the stored procedure in two forms:
1) If our MySQL stored procedure have any select query just like Select * from our table then it will simply return result after calling the function like $this->Model->procedure(“YourProcuedure” , $input_parameter).
2) If our stored procedure is dealing with output procedure then it will just return by calling another function name
$this->Model->getOutData().
Example:
$this->Model->Procedure(“YourProcedure” , $input_parameter , $outputparameter);
$data = $this->Model->GetOutData();
Class for the CakePHP call MySQL stored procedure
For more detail please go through the step-by-step implementation given below:
 Paste the following code in AppModel.php
<br /> <strong>&lt;?php</strong>
class AppModel extends Model
{
var $outputParams = array();
function Procedure($name , $inputParameter = array(), $outputParameter = array() )
{
$this->outputParams = $outputParameter;
if (class_exists(‘DATABASE_CONFIG’)) {
$this->config =& new DATABASE_CONFIG();
}
if($this->config->default[‘driver’] == “mysql”)
{
trigger_error(“OOps error occure, Please go in your database.php of your config folder and set driver name = <b>mysqli</b> to execute stored procuedure. Currently it is set to mysql”);
exit;
}
//Create parameter
$parameter = “”;
foreach($inputParameter as $params)
{
$parameter .= $parameter == “” ? ” ‘$params’ ” : ” , ‘$params’ “;
}
if(count($outputParameter)> 0)
{
foreach($outputParameter as $prm)
{
$parameter .= $parameter == “” ? ” @$prm ” : ” , @$prm “;
}
}
$procuedure = ” call `$name`($parameter) “;
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$db->connection;
mysqli_multi_query($db->connection,$procuedure) or die(mysqli_error($db->connection));
$final_data = array();
do {
/* store first result set */
$mid_data = array();
if ($result = mysqli_store_result($db->connection)) {
while ($row = mysqli_fetch_array($result)) {
$mid_data[] =  $row;
}
mysqli_free_result($result);
}
$final_data[] = $mid_data;
/* print divider */
} while (mysqli_next_result($db->connection));
return $final_data;
}
function getOutData()
{
$outputParameter = $this->outputParams;
if(count($outputParameter)> 0)
{
$parameter = “”;
foreach($outputParameter as $prm)
{
$parameter .= $parameter == “” ? ” @$prm  ” : ” , @$prm  “;
}
$SQL = ” SELECT $parameter “;
$data = $this->query($SQL);
return $data;
}
else
{
trigger_error(“OOPS!!! no resource for select query here”);
}
}
function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
$parameters = compact(‘conditions’);
$this->recursive = $recursive;
$count = $this->find(‘count’, array_merge($parameters, $extra));
if (isset($extra[‘group’])) {
$count = $this->getAffectedRows();
}
return $count;
}
}
Now make sure that in every model you have extended(inherited AppModel class, which is very basic if not the inherited then before using these function please do it)
Now in your model or controller you can use use procedure function to execute the execute stored procedure. Following is the way to execute the stored procedure from CakePHP.
If you are using from controller then you can use following way:
<br />
<strong>&lt;?php</strong><br />
<strong>
$input_prameter = array(“username” , “search_keyword” , 5 , 3);
//Define input parameter into array in the same order in which your procedure require input
</strong><br />
<strong>
$output&nbsp; = array(“@search_result” , “@location”);
//output parameter of the stored procedure in the same way as define in your mysql procedure
</strong><br />
<strong>
$sel_data=$this-&gt;Model-&gt;Procedure(“GetUserDetailProcedure”, $input_parameter&nbsp; , $output);
</strong><br />
<strong>
print_r($sel_data);
//it will return data if there will be any select query used in stored procedure
</strong><br />
<strong>
$data_of_output_parameter = $this-&gt;Model-&gt;getOutData();
// to get data of output parameter, if you do not have any output parameter in your mysql stored procedure then no need to call this function.
</strong>

Source: Codewebber