Thursday, 1 June 2017

Symfony2 Interview Questions and Answers

Symfony2 Interview Questions and Answers


Question: What is Symfony?
Symfony is a PHP framework and a set of reusable components/libraries.



Question: What is current Stable version of Symfony?
Version: 3.2.7, Dated: 5 April 2017



Question: What is offical website URL of Symfony?
http://www.symfony.com



Question: What is offical Github URL of Symfony?
https://github.com/symfony/symfony



Question: What are the benefits of Symfony?
  1. Low performance overhead
  2. Robust Applications
  3. Speed up the creation and maintenance
  4. Unlimited flexibility



Question: What are the innovations in Symfony2?
  1. Symfony2 uses the Dependency Injection pattern.
  2. Symfony2 is packaged as Distributions
  3. Everything is a Bundle in Symfony2.
  4. Symfony2 eases the debugging of your application.
  5. Symfony takes Security very seriously



Question: How to install Symfony2?
Create a folder and Go to in that folder using cd command.
Execute Following command
php -r "readfile('https://symfony.com/installer');" > symfony



Question: How to create controller in Symfony2?
File Location: src/AppBundle/Controller/UserController.php
Format:
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class UserController extends Controller
{

}



Question: How to create Action of controller in Symfony2?
Add following code inside UserController.php
    public function indexAction()
    {
        return $this->render('user/index.html.twig', [ ]);
    }



Question: What is format of view file?
File Location: app/Resources/views/user/index.html.twig
{% extends 'base.html.twig' %}

{% block body %}
    <h1>
Welcome to Symfony2</h1>
{# ... #}
{% endblock %}



Question: How to get current route in Symfony?
$request = $this->container->get('request');
$currentRouteName = $request->get('_route');




Question: How to get current route in Symfony?
$request = $this->container->get('request');
$currentRouteName = $request->get('_route');



Question: How to get the request parameters in symfony2?
$request = $this->container->get('request');
$name=$request->query->get('name');



Question: How to var_dump variables in twig templates??
{{ dump(user) }};



Tuesday, 30 May 2017

What is mongoose? How to use mongoose with nodejs for mongodb?

What is mongoose? How to use mongoose with nodejs for mongodb?
 How to use mongoose with nodejs for mongodb?

Question: What is mongoose in node js?
mongoose is an object modeling package for Node that essentially works like an ORM.


Question: How to install mongoose in node js?
npm install mongoose



Question: What is the --save option for npm install?
When we do "npm install", then it install the modules.
If we need to update the version of module in package.json, then we have to do it manually.
BUT when we do "npm install --save", then it install the modules and update the package.json automatically.


Question: What is difference between dependencies and devDependencies?
dependencies are modules your project depends on, devDependencies are modules you use to develop your project.

Examples of dependencies are request, through2 and concat-stream.
Examples of devDependencies are grunt, mocha, eslint, tape, and browserify.


Question: How to connect to MongoDB database?
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/mongodb');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
   console.log("MongoDB connected Successfully.!");
});


Question: How to define the Schema in mongoose?
var userSchema = mongoose.Schema({
  name: String, //datatype is string
  username: { type: String, required: true, unique: true }, //type is string and its required field and its unique
  password: { type: String, required: true },
  admin: Boolean,
  location: String,  
  created_at: Date //datatype of date  
});



Question: How to compile the Schema with model?
var User = mongoose.model('User', userSchema);
module.exports = User;
Here User inside "mongoose.model" is a User model, where all the data will be managed.
var User is an Model Object which will be used to process on the document.



Question: What are different type of data types in mongodb?
String
Number
Date
Buffer
Boolean
Mixed
ObjectId
Array


Question: How to add a record in document with model?
var userDetails = new User({ name: 'Test user',username:'testuser',password:'test12@',location:'mohali' });
userDetails.save(function (err) {if (err) console.log ('Error on save!')});
console.log(userDetails); 



Question: How to add a record in document with model with FULL CODE?
//include the js
var mongoose = require('mongoose');

//database connection
mongoose.connect('mongodb://localhost:27017/mongodb');

//Create schema
var userSchema = mongoose.Schema({
  name: String, //datatype is string
  username: { type: String, required: true, unique: true }, //type is string and its required field and its unique
  password: { type: String, required: true },
  admin: Boolean,
  location: String,  
  created_at: Date //datatype of date  
});

//create User Object
var User = mongoose.model('User', userSchema);

//Save User details in database
var userDetails = new User({ name: 'Test user',username:'testuser',password:'test12@',location:'mohali' });
userDetails.save(function (err) {if (err) console.log ('Error on save!')});
console.log(userDetails);



Question: How to fetch all records?
User.find({}, function(err, users) {
  if (err) {throw err;}
  console.log(users);
});



Question: How to fetch one record?
User.find({ username: 'testuser' }, function(err, user) {
  if (err) throw err;

  // object of the user
  console.log(user);
});



Question: How to update record?
        User.update({username: 'testuser' }, { $set: { name: "New name" }}, {}, function(){
            //here comes after update the record
        })



Question: How to delete record?
  User.find({ username: 'testuser'  }).remove( function(){
  //write here your code after delete
 });