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).

Friday, November 30, 2012

DOT NET Interview Questions


       In which event are the controls fully loaded?
Page load event guarantees that all controls are fully loaded. Controls are also accessed in Page_Init events but you will see that view state is not fully loaded during this event.

How can we identify that the Page is Post Back?
Page object has an "IsPostBack" property, which can be checked to know that is the page posted back.

What is the lifespan for items stored in ViewState?
The items stored in ViewState live until the lifetime of the current page expires including the postbacks to the same page.

How information about the user's locale can be accessed?
The information regarding a user's locale can be accessed by using the System.Web.UI.Page.Cultureproperty.

Thursday, November 29, 2012

SQL INTERVIEW QUESTIONS

Q: What does the acronym SQL stand for?
Ans: STRUCTURED QUERY LANGUAGE

Q: What are the six main categories of SQL commands?
Ans:
Data definition language
Data Manipulation language
Data Query Language
Data Control Language
Data administration commands
Transactional control commands

Q: What are the four transactional control commands?
Ans:
Commit
Rollback
SAVE POINT
SET TRANSACTION

Q: Identify the categories in which the following SQL commands fall:
Ans:
1. CREATE TABLE =DDL
2. DELETE =DML
3.SELECT=DQL
4.INSERT=DML
5. ALTER TABLE=DCL
6. UPDATE=DML

Q:What is a database Schema?
Ans:
A database Schema is a set of interrelated database objects which are owned by a group or person.

Q: Difference between Unique and Primary Key?
Ans:
No two columns in a table can have a Primary Key
Two Columns can have a Unique Key in a table

Primary Key is referenced to Join Tables
Unique Key cannot be referenced

Unique Key can be Null
Primary Key is not Null

For Normalization use

http://programmerschoice.blogspot.in/2012/01/normalizing-database.html

Q
Write DML to accomplish the following:
a. Correct Billy Pierce's SSN to read 310239857.
UPDATE `emp_new` SET ssn =310239857 WHERE ssn =310239856
b. Add Ben Moore, PHONE is 317-5649880, ssn is 313456789.
INSERT INTO emp_new VALUES ('Moore', 'Ben', 313456789, 3175649880)
c. John Smith quit; remove his record.
DELETE FROM emp_new WHERE `FIRST_NAME` = 'JOHN' AND `LAST_NAME` = 'SMITH'

Sunday, August 26, 2012

Yii Validator Classes

Yii provides predefined validator classes for you which are

boolean
captcha
compare
email
default
exist
file
filter
in
length
match
numerical
type

Wednesday, February 1, 2012

Third Highest Salary

Here is a database question asked usually during Interviews which I would like to share

Name Salary
A          50
B          20
C          30
D          40
E          30
F          40

Now out of these find employees with third highest salary

Here are steps to do so first let us sort in descending order and limit the same to third row

Select DISTINCT(Salary) from emp order by Salary DESC limit 2,1

Now this gives us 30 but we need salary of C and D as rows we take this as subquery

Select * from Emp where salary in( Select DISTINCT(Salary) from emp order by Salary DESC limit 2,1);
which is the correct answer

Monday, January 23, 2012

Deploying an ASP .Net Application

XCopy Deployment
This is the simplest type of deployment.You simple copy the files from your development server to the production server.

Precompiled Deployment
The entire application is complied and then the compiled assemblies are copied to the production server.

Setup Project
Create a setup for the website and then it is deployed by running the setup at the production server.

Normalizing the database

Normalization refers to removal of redundant information from a database Design
Following are the basic normal Forms

1st Normal Form
When each table row is free of repeated data.So a relation is in first normal form if and only if all underlying domains contain atomic values

2nd Normal Form
Applies to tables that have composite keys i.e. the primary key is made up of 2 or more columns.When a table has a composite key then every column must depend on the whole key instead of a particular column.

A relation must be in 1st NF and every nonkey attribute must be fully dependent on the primary key.

3rd Normal Form
When every column in a table depends on entire primary key and no nonkey columns depend on each other.

2nd NF + nonkey attribute is non transitively dependent on primary key

Boyce-Codd Normal Form
A table is in Boyce–Codd normal form if and only if for every one of its nontrivial dependencies X → Y, X is a superkey—that is, X is either a candidate key or a superset thereof.

4th Normal Form
A Table is in 4NF if and only if, for every one of its non-trivial multivalued dependencies X \twoheadrightarrow Y, X is a superkey—that is, X is either a candidate key or a superset thereof.

5th Normal Form
Every join dependency in R is implied by the candidate keys of R