5 Essential 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.'
    )
)

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.'
    )
)
Fields marked with an asterisk (*) are required
Comment submitted succesfully. Thank you.
We are going to review the comment and get back to you as soon as possible.

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

$this->set('articles', $articles);

Also, compare the above line of code to the following:

$this->set(compact('articles'));

What are the relative advantages of each? Which would you use and why?

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.

Fields marked with an asterisk (*) are required
Comment submitted succesfully. Thank you.
We are going to review the comment and get back to you as soon as possible.

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

$this->Region->find('all');

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));
Fields marked with an asterisk (*) are required
Comment submitted succesfully. Thank you.
We are going to review the comment and get back to you as soon as possible.

Find top CakePHP talent today. Toptal can match you with the best developers to finish your project.

Hire Toptal’s CakePHP Developers

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?

$this->Region->find('all', array('recursive' => 0));

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.

Fields marked with an asterisk (*) are required
Comment submitted succesfully. Thank you.
We are going to review the comment and get back to you as soon as possible.

What is the difference between a Component, a Behavior, and a Helper? Provide an example of where and when each might be used.

All 3 are similar because they act to extend existing CakePHP functionality, but the differ on what they extend:

For example:

  • 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, for example to add images uploads. 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.
Fields marked with an asterisk (*) are required
Comment submitted succesfully. Thank you.
We are going to review the comment and get back to you as soon as possible.
* There is more to interviewing than tricky technical questions, so these are intended merely as a guide. Not every “A” candidate worth hiring will be able to answer them all, nor does answering them all guarantee an “A” candidate. At the end of the day, hiring remains an art, a science — and a lot of work.
Submit an interview question
Submitted questions and answers are subject to review and editing, and may or may not be selected for posting, at the sole discretion of Toptal, LLC.
All fields are required
Thanks for submitting your question.
Our editorial staff will review it shortly. Please note that submitted questions and answers are subject to review and editing, and may or may not be selected for posting, at the sole discretion of Toptal, LLC.
Bryce Ott
United States
With more than 13 years working as an engineer, architect, director, vice president, and chief technology officer, Bryce brings a deep understanding of enterprise software, management, and technical strategy to any project. His specialties include real-time systems, business intelligence, big data, enterprise web apps, scalability, and open-source software.
Guilherme Fernando Ferreira
Brazil
Guilherme is an Agile developer with a BS in Computer Science. He is a passionate programmer with over 10 years of experience in Ruby, JavaScript, Python, PHP, Java, and more. He has spoken at conferences including Campus Party, Agile Brazil, and Agile Tour.
Ivan Bernat
Croatia
Ivan is a full stack web developer who has shipped over 400 front-end projects, a dozen smaller web apps, and several WordPress themes. Ivan has experience working both as a standalone freelancer for clients from around the globe and as an in-house developer in large teams.