Skip to main content

CAKEPHP Interview Questions

 What’s wrong with this multiple Model Validation rule? Will both, one, or neither rule be executed and why? How might this code be fixed?'email' => array( 'rule' => array( 'rule' => 'notEmpty', 'message' => 'Email address is required.' ), 'rule' => array( 'rule' => 'email', 'message' => 'The email address you entered is invalid.' ))


Ans :- 
The key 'rule' needs to be unique when calling multiple validation rules. In the case above, the notEmpty rule will never be called, as the email rule will simply overwrite it (since the multidimensional array has the same key). Each key should be unique, e.g: 'email' => array( 'rule-1' => array( 'rule' => 'notEmpty', 'message' => 'Email address is required. ), 'rule-2' => array( 'rule' => 'email', 'message' => 'The email address you entered is invalid.' ))

What is the purpose of $this->set(); when used in the Controller actions, e.g:

$this->set('articles', $articles);
$this->set(compact('articles'));
$this->Region->find('all');
Ans :-
The Containable behavior allows the developer to specify which associated models (if any) are retrieved from the find query.
So to ensure that no other associated models are returned in the above example, it could simply be re-written as:
$this->Region->find('all', array('contain' => false));
4. What is the difference between a Component, a Behavior, and a Helper? Provide an example of where and when each might be used.
Ans :-


  • A shopping cart Component might offer functionality that can be used and shared across multiple Controllers.
  • A custom upload Behavior could be used to extend a Model, such as this example for uploading images. Another common example of a Behavior would be to add extra validation functionality beyond that which CakePHP offers by default.
  • A Helper can be used to assist with View functionality. A good example here would be the CK Editor helper that makes it easy to display a CK Editor.
$this->Region->find('all', array('recursive' => 0));
Ans :-
The use of recursive is incorrect in the above example (if we don’t want to pull any associated data) since recursive works as follows:
-1 – No associated data is retrieved with the find query.
0 – Retrieves any BelongsTo associated data.
1 – Retrieves any directly related associations (i.e., BelongsTo, HasMany, HasOne, HasAndBelongsToMany).
2 – Retrieves any directly related associations, and their associations’ associations.
So in the above, example, -1 should have been used (rather than 0) to avoid pulling any associated data.
It is also important to note that recursive is slated to be phased out in CakePHP 3 and replaced solely with the Containable behavior.

Also, compare the above line of code to the following:
What are the relative advantages of each? Which would you use and why?

Ans :-
The set() method is used to create a variable in the view file. 
 In the example above, the variable $articles will then be available to use in the view template file for that action.
An advantage of the first example (i.e., $this->set('articles', $articles); is that it allows the variable name on the view to be different from the variable name on the controller. For example, if you wanted them to be different, you could do something like $this->set('articlesData', $articles);. The variable on the view file would then be $articlesData.
The advantage of the second approach (i.e., $this->set(compact('articles'));, on the other hand, is that it is somewhat neater, and it also is arguably a bit less error-prone. It is also shorter and easier to write, especially where we are setting several variables to the view.

3. In the following line of code, how could the Containable behavior be used to optimize the find query so that only the region data is returned (i.e., without any model associations):
All 3 are similar because they act to extend existing CakePHP functionality, but the differ on what they extend:
For example:
5. A current alternative to the Containable behavior is to use the recursive function. Why is the use of the recursive function below incorrect in a case where we want to only retrieve the region data without any model associations?

Comments

Popular posts from this blog

How to Create a PDF file in Cakephp 2.0 using Fpdf

Step 1: Download FPDF folder from  http://www.fpdf.org/  . Step 2: Unzip the downloaded Fpdf file and name it “fpdf” or however you require and make sure that you use the same name while calling it. Step 3: Move the “fpdf” unzipped files to  your /app/Vendor directory within Cakephp. Now you should have the directory path as   /app/Vendor/fpdf. Step 4: Create a new Cakephp layout file for the pdfs. We will use this layout when serving a pdf to the client. Create a file called pdf.ctp inside of /app/View/Layouts. Add the following code to /app/View/Layouts/pdf.ctp Layout: /app/View/Layouts/pdf.ctp 1 2 3 4 <?php      header ( 'Content-Disposition: attachment; filename="downloaded.pdf"' ) ;      echo $content_for_layout ; ?> The header function above tells the browser that it is going to receive a file called download.pdf. If you want to change the name

Setup CakePHP Using Xampp On Windows

Step 1: Download XAMPP  and  CakePHP .   Step 2: Install Xampp Once you have installed Xampp (version 1.7.3) on your Windows with the default option, all your files will be located in the C:\xampp folder. Step 3: Mod Rewrite Module Once Xampp is installed as the local server, you can then proceed to enable mod_rewrite. To do so, you will have to open the httpd.conf file that is located in C:\xampp\apache\conf and uncomment by removing # from the following line: # LoadModule rewrite_module modules/mod_rewrite.so Step 4: Place CakePHP Files in a New Folder Extract the CakePHP (version 1.3.8) zip file and copy all its contents to your local web server, which in this instance is C:\xampp\htdocs\cakephp . I have decided to name the CakePHP folder as cakephp, and in it, you will find many files and folders for the framework, including app, cake, docs, vendors, .htaccess, and index.php. Step 5: Set Up Virtual Host Open the httpd-vhosts.conf file from the C:\xampp\apa

Installing Wamp on Windows

WAMP is an abbreviated name for the software stack Windows, Apache, MySQL, PHP. It is  derived from LAMP which stands for Linux, Apache, MySQL, and PHP. As the name implies, while LAMP is used on Linux servers, WAMP is used on Windows servers.  The “A” in WAMP stands for Apache.  Apache  is server software that is used to serve webpages. Whenever someone types in your WordPress website’s URL, Apache is the software that “serves” your WordPress site. The “M” in WAMP stands for MySQL.  MySQL  is a database management system. It’s job in the software stack is to store all of your website’s content, user profiles, comments, etc. The “P” in WAMP stands for PHP. PHP is the programming language that WordPress is written in. It is also the piece that holds the entire software stack together. It runs as a process in Apache and communicates with the MySQL database to dynamically build your webpages. Download the wamp for the url  http://www.wampserver.com/en/download.php .  You

Dynamic Sitemap Generation plugin in Cakephp

Here for the SEO implementation we need to generate the sitemap.xml in our application which is accessed by the webmaster tool. So here i am outlined the steps to generate the Xml file . 1. Lets think we have controller by name sitemap,Inside that create an action by name sitemap and paste the following code    public function sitemap(){     $this->layout='ajax';     $this->RequestHandler->respondAs('xml');     $listData = $this->Sitemap->find('all',/*Conditions if you have any*/);     $this->set(compact('listData')); } I through variable $listData to render all data( Keywords,Title,Url,etc ...) that will be shown in sitemap.xml.  This   depends   on the   dynamic link  what   we want to   show  in sitemap.xml.For request handler to work include the RequestHandler component public $components = array('RequestHandler'); Step 2. Create View based on action sitemap On structure MVC as default we need create sitemap.