Showing posts with label Tutorial Points. Show all posts
Showing posts with label Tutorial Points. Show all posts

Saturday, 20 April 2013

Yii Interview Question

1Q. What is meant by Yii Framework?

A. Yii is an open source and highly performed component-based PHP framework used for Web application development. This application was started on January and completed in December 2008.

2Q. Why do Yii run so FAST?

A. Yii is so much faster because it uses sluggish loading technique widely. Like it doesn’t include a class file until the class is used initially and it doesn’t create object until it is accessed for the first time. Some of the frameworks get problems because of the performance hit because they would enable functionality no matter it is used or not during its request.

3Q. tell about the file that gets loaded when you run an application using Yii?

A. index.php

4Q. Explain about model, view, controller?

A. Model represents the underlying data structure of a Web application. Models often share different sub-applications of a Web application. B. View is responsible in presenting models in the format where the end users desire. In general view contains presentational code. C. Controllers are the adhesive that binds models, views and other components composed into a runnable application. Controllers are responsible in dealing with the end user requests. Controllers access $_GET, $_POST and other PHP variables according to the user requests.

5Q. What about the naming convention in Yii?

- We define table prefix while using Gii. In your case you need to set it to tbl_. Then it should generate UserController instead of TblUserController. - The Yii Framework employees a class naming convention whereby the names of the classes directly plot to the directories in where they are stored. The root level directory of the Yii Framework is the “framework/” directory, where all classes are stored hierarchically. Class names contain alphanumeric characters.

6Q. what is the component, uses, how can we do and what is the better way?

A. A component is a piece of code written for specific task is used by calling controllers, helper is used for helping Yii in rendering the data to be shown to user with views, these adds to modularity in code or else same coding will be implemented in controllers.

7Q. How do you continue when you have to use Yii for any application?

A. If you have changed according to your needs… Proceed with basic software engg. concepts as requirement gathering etc. This is the basic Understanding Concept in Yii. - Yii based apps are driven by data logic. - Yii can generate the shebang of PHP codes automatically. - Yii will generate an essential app for you automatically. - Yii uses MySQL and SQLite. - Yii is an open-source.

8Q. What is the first function that gets loaded from a controller?

A. Index

9Q. What is meant habtm?

A. Habtm means HasAndBelongsToMany. The” has and belongs to many” is a kind of associations that defined in models for retrieving related data across different entities.

10Q. How to use ajax in Yii?

A. We use by calling ajax helper, then using it in controller for rendering.

11Q. What are the possible ways to validate a registrations module for a user?

A. This can be done in two ways by i) submission in controller, or ii) using javascript/ajax while user is still filling the data. But, comparatively Second option will be better.

12Q. List out some database related functions in Yii?

A. Query find, findAll , findByPk , find By

13Q. How to include a javascript menu through a site?

A. We can include by adding the javascript files in webroot and then call them in default views if they are needed everywhere or in the related views.

14Q.Who was the developer of the Yii and when was it build?

A. The developer of the Yii is Yii Software LLC. It was started in the year 2008 in December 3, with the version of 1.0 and continued to 1.1.13. in PHP language.

15Q. What are the requirements of the Yii?

A. To run a Yii Web application, we need a Web server which supports PHP 5.1.0. Version. The developers who want to use Yii, should understand the object-oriented programming (OOP) which is very helpful, because it is a pure OOP framework.

16Q. How does Yii Compare with Other Frameworks?

A. Comparatively to most of PHP frameworks, Yii is a MVC framework. Yii bests PHP frameworks for its efficient, feature-rich and clearly-documented. Yii is designed to be fit for serious Web application development. It is neither a consequence of some project nor a corporation of third-party work.

17Q. How do we extend Yii?

A. Extending Yii is a common action during web development. Like when we write a new controller, we extend Yii by inheriting its CController class; when we write a new widget, we will extend CWidget or an existing class. If the code is designed to be reused by third-party then we call it an extension.

18Q. How to connect to the database?

A. Most of the web applications are assisted by databases. test-drive application is not an exception. For database, we need to see the application where and how to connect it. This is done through the application configuration file WebRoot/testdrive/protected/config/main.php, the following code represent the connectivity to the database

return array(
    ‘components’=>array(
        ‘db’=>array(
            ‘connectionString’=>’sqlite:protected/data/testdrive.db’,
        ),
    ),
);

19Q. What is a Yiibase?

A. YiiBase is a helper class that serves functionalities of common framework. We should not use YiiBase directly. Instead, have to use its child class where we can customise the methods of YiiBase.

20Q. How to generate CRUD code?

A. After creating a model class file, we shall generate the code that implements the CRUD operations for user data. We choose the Crud Generator in Gii. In the model class field, we enter ‘User’. In the Controller ID field, we enter ‘user’ in lower case. Then press the preview button followed by the Generate button. Now we are done with the CRUD code generation.

Thursday, 21 March 2013

Yii Framework Simple Shopping Cart Tutorial for Beginners

Provides shopping cart functionality for models. Cart is a container object that holds items collection and have handy methods to work with it. It uses user session as a cart data storage. 

INSTALLING AND CONFIGURING

1 way: Registration in the config file


Add to protected/config/main.php: 

'import'=>array(
    'ext.yiiext.components.shoppingCart.*'
),
 
'components' => array(
  'shoppingCart' =>
    array(
        'class' => 'ext.yiiext.components.shoppingCart.EShoppingCart',
    ),
 
)


2 way: Registration by necessity

$cart = Yii::createComponent(array(
    'class' => 'ext.yiiext.components.shoppingCart.EShoppingCart'
));
//Important!
$cart->init();
 
$book = Book::model()->findByPk(1);
 
$cart->put($book);


PREPARING A MODEL

Models that you are planning to put into the cart should implement IECartPosition interface: 

class Book extends CActiveRecord implements IECartPosition {
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }
 
    function getId(){
        return 'Book'.$this->id;
    }
 
    function getPrice(){
        return $this->price;
    }
}


API

EShoppingCart::put($position, $quantity)



Adds $quantity items to the cart. If item is already in the cart, item data is being updated and item quantity is summed with $quantity. 

$book = Book::model()->findByPk(1);
Yii::app()->shoppingCart->put($book); //1 item with id=1, quantity=1.
Yii::app()->shoppingCart->put($book,2); //1 item with id=1, quantity=3.
$book2 = Book::model()->findByPk(2);
Yii::app()->shoppingCart->put($book2); //2 items with id=1 and id=2.
EShoppingCart::update($position, $quantity)



Updates cart item. If item is already in the cart, item data is being updated and item quantity is set to $quantity. If there is no such item yet it will be added. If $quantity<1 then item will be deleted. 

$book = Book::model()->findByPk(1);
Yii::app()->shoppingCart->put($book); //1 item with id=1, quantity=1.
Yii::app()->shoppingCart->update($book,2); //1 item with id=1, quantity=2.
EShoppingCart::remove($key)


Removes item from the cart. 

$book = Book::model()->findByPk(1);
Yii::app()->shoppingCart->put($book,2); //1 item with id=1, quantity=2.
Yii::app()->shoppingCart->remove($book->getId()); //no items
EShoppingCart::clear()
Clears all cart items.


Clears all cart items. 

Yii::app()->shoppingCart->clear();
EShoppingCart::itemAt($key)


Returns item at key $key. 

$position = Yii::app()->shoppingCart->itemAt(1);
EShoppingCart::contains($key)


Tells if cart contains item with id=$key. 

$position = Yii::app()->shoppingCart->itemAt();
EShoppingCart::isEmpty()


Tells if cart is empty. 

$position = Yii::app()->shoppingCart->isEmpty(1);
EShoppingCart::getCount()


Returns positions count. 

Yii::app()->shoppingCart->put($book,2);
Yii::app()->shoppingCart->put($book2,3);
Yii::app()->shoppingCart->getCount(); //2
EShoppingCart::getItemsCount()


Returns items count. 

Yii::app()->shoppingCart->put($book,2);
Yii::app()->shoppingCart->put($book2,3);
Yii::app()->shoppingCart->getItemsCount(); //5
EShoppingCart::getCost($withDiscount)


Returns cart total. 

Yii::app()->shoppingCart->put($book,2); //price=100
Yii::app()->shoppingCart->put($book2,1); //price=200
Yii::app()->shoppingCart->getCost(); //400
EShoppingCart::getPositions()


Returns an array with all positions. 

$positions = Yii::app()->shoppingCart->getPositions();
foreach($positions as $position) {
...
}
IECartPosition::getPrice()


Returns a price for a single item for this position. 

$positions = Yii::app()->shoppingCart->getPositions();
foreach($positions as $position) {
    $price = $position->getPrice();
}
IECartPosition::getSumPrice($withDiscount)


Returns position price = single item price*items count 

$book = Book::model()->findByPk(1); //price = 100
Yii::app()->shoppingCart->put($book,2); //putting 2 items
$positions = Yii::app()->shoppingCart->getPositions();
foreach($positions as $position) {
    $price = $position->getSumPrice(); //200 (2*100)
}
IECartPosition::getQuantity()


Returns position items quantity 

$book = Book::model()->findByPk(1); //price = 100
Yii::app()->shoppingCart->put($book,2); //putting 2 items
$positions = Yii::app()->shoppingCart->getPositions();
foreach($positions as $position) {
    $price = $position->getQuantity(); //2
}


DISCOUNTS


There is a single discount system built in. It allows to apply a set of rules that will change cart total or a single position price. Discount is a class that implements IEDiscount and defines apply() method that describes how exactly discount is applied. Discount is calculated by applying Position::addDiscountPrice/Position::setDiscountPrice position's method or EShoppingCart::addDiscountPrice/EShoppingCart::setDiscountPrice cart-wide method. This methods are getting one parameter that holds a value of cart total reduction or individual position reduction. Discount class example: 

class TestDiscount extends IEDiscount {
    /**
     * % discount
     */
    public $rate = 30;
 
    public function apply() {
        foreach ($this->shoppingCart as $position) {
            $quantity = $position->getQuantity();
            if ($quantity > 1) {
                $discountPrice = $this->rate * $position->getPrice() / 100;
                $position->addDiscountPrice($discountPrice);
            }
        }
    }
 
}


This example discount works like this: if there is more than one item for a single position, there will be $rate % discount for one item and it will be applied to position price. You can apply unlimited discount rules that will be called one by one: 

'shoppingCart' =>
    array(
        'class' => 'ext.yiiext.components.shoppingCart.EShoppingCart',
        'discounts' =>
        array(
            array(
                'class' => 'ext.yiiext.components.shoppingCart.discounts.TestDiscount',
                'rate' => 40),
            array('class' => 'otherDiscount'),
        ),
 
    ),



EVENTS

There are 2 events in ShoppingCart implemented in a standard Yii way: 
  1. onUpdatePosition - is triggered when position is added or updated. 
  2. onRemovePosition - is triggered when position is deleted.
Usage: 
$book = Book::model()->findByPk(1);
Yii::app()->shoppingCart[] = $book; //adding new position.
 
//iterating over all positions
foreach(Yii::app()->shoppingCart as $position)
{
...
 
}


When onUpdatePosition event is fired, call center will be notified. 
WORKING WITH A CART AS CMAP
ShoppingCart is a child of CMap so you can work with the cart as an array. 


Yii Interview Question