Pages

Friday, December 7, 2012

Interview Question on relations in cakephp

Recently I was asked on different types of relations in between models in a cakephp framework

Relationship Association Type Example
one to one hasOne A user has one profile.
one to many hasMany A user can have multiple recipes.
many to one belongsTo Many recipes belong to a user.
many to many hasAndBelongsToMany Recipes have, and belong to many ingredients.

Then I was given 4 tables as follows

Employee:
Eid,fname,lname

Department:
Deptid,deptname

Salary: 
Sid,salary

EmpSalDeptrelation:
eid,sid,deptid

To these I was told to determine how relations will be represented in EmpSalDeptrelation table so that if I fetch 1 employee I get his salary department along

Answer is to the EmpSalDeptrelation model add the relationships as

eid belongsTo Eid of employee,sid belongs to Sid of salary,deptid belongs to Deptid of Department

and then in the respective controller which uses($uses) this model
add
$this->empsaldeptrelation->find();
set the conditions where eid equaks to concerned employee id; 

Basic requirements to create a plugin in wordpress

To create a plugin in wordpress we need to create a folder and php file in it with same name and place it under WP-content/plugins directory

Then in the file add the following comments that describe the plugin

<?php
/*
Plugin Name: Name Of The Plugin
Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
Description: A brief description of the Plugin.
Version: The Plugin's Version Number, e.g.: 1.0
Author: Name Of The Plugin Author
Author URI: http://URI_Of_The_Plugin_Author
License: A "Slug" license name e.g. GPL2
*/
?>
 
The minimum information WordPress needs to recognize your Plugin is the Plugin Name line.

To add admin menu side use following hook:

add_action('admin_menu', 'gvp_add_menu_page');
function gvp_add_menu_page() {
    add_menu_page("ORDERS TILL NOW", "View Orders", "activate_plugins", "view_orders", "orders_admin_page");
}

function orders_admin_page()
{
    include_once dirname(__FILE__) . '/adminend.php';
}


To perform actions at initialization use :

register_activation_hook(__FILE__, 'initialize_me');

function initialize_me() {
    global $wpdb;
    $jsrm_menu_table = $wpdb->prefix . "jsrm_menus";
    $jsrm_item_table = $wpdb->prefix . "jsrm_items";
    $gav_order_table = $wpdb->prefix . "gav_orders";
    $gav_order_items_table = $wpdb->prefix . "gav_orders_items";
    $gav_users_table= $wpdb->prefix . "gav_users";
   
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

    if (!empty($wpdb->charset))
        $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
    if (!empty($wpdb->collate))
        $charset_collate .= " COLLATE $wpdb->collate";

    if ($wpdb->get_var("SHOW TABLES LIKE $gav_users_table") != $gav_users_table) {
        $sql1 = "CREATE TABLE IF NOT EXISTS $gav_users_table (
                id mediumint(9) NOT NULL AUTO_INCREMENT,
                fname text NOT NULL,
                lname text NOT NULL,
                address text NOT NULL,
                                email text NOT NULL,
                phone varchar(255) NOT NULL,
                ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
                UNIQUE KEY id (id)
                ) $charset_collate;";
        dbDelta($sql1);
    }
   
     if ($wpdb->get_var("SHOW TABLES LIKE $gav_order_table") != $gav_order_table) {

        $sql3 = "CREATE TABLE IF NOT EXISTS $gav_order_table (
                id mediumint(9) NOT NULL AUTO_INCREMENT,
                custid mediumint(9) NOT NULL,
                                status tinyint(1) NOT NULL,
                UNIQUE KEY id (id),
                                CONSTRAINT `OrderOfCustomer` FOREIGN KEY (`custid`) REFERENCES `$gav_users_table` (`id`)
                ) $charset_collate;";
        dbDelta($sql3);
    }

    if ($wpdb->get_var("SHOW TABLES LIKE $gav_order_items_table") != $gav_order_items_table) {

        $sql2 = "CREATE TABLE IF NOT EXISTS $gav_order_items_table (
                id mediumint(9) NOT NULL AUTO_INCREMENT,
                orderid mediumint(9) NOT NULL,
                itemid mediumint(9) NOT NULL,
                                qty mediumint(9) NOT NULL,
                                amt int(15) NOT NULL,
                UNIQUE KEY id (id),
                                CONSTRAINT `ItemsofOrder` FOREIGN KEY (`orderid`) REFERENCES `$gav_order_table` (`id`)
                ) $charset_collate;";
        dbDelta($sql2);
    }
   
    add_option("gav_order_version", $xap_db_version);
}

To add Shortcode: 

add_shortcode('gav-show-menu', 'gav_showmenu');
function gav_showmenu($atts) {
}

To add stylesheet:
function gav_stylesheet() {
    $gavStyleUrl = WP_PLUGIN_URL . '/gav-retail-order/css/main-stylesheet.css';
    $gavStyleFile = WP_PLUGIN_DIR . '/gav-retail-order/css/main-stylesheet.css';
    //echo file_exists($gavStyleUrl);
    if (file_exists($gavStyleFile)) {
        wp_register_style('gavStyleSheet', $gavStyleUrl);
        wp_enqueue_style('gavStyleSheet');
    }
}

add_action('wp_print_styles', 'gav_stylesheet');

result of statement "echo 'momo's'"

Expression:

<?php
echo 'momo's'
?>

Result:




Saturday, December 1, 2012

Running a transaction in phpmyadmin


The problem with PHPmyAdmin is that all the lines have to be one command so it is not easy to demonstrate.

But if you run a transaction as follows it will not commit though the response says a different story

BEGIN;# MySQL returned an empty result set (i.e. zero rows).

UPDATE PRODUCTS_TMP
SET COST = 70
WHERE PROD_ID = '11235';# 1 row affected.

rollback;# MySQL returned an empty result set (i.e. zero rows).