5个基本的CakePHP面试问题 *

最好的CakePHP开发人员和工程师可以回答的基本问题. 在我们社区的推动下,我们鼓励专家提交问题并提供反馈.

Hire a Top CakePHP Developer Now
Toptal logo是顶级自由软件开发人员的专属网络吗, designers, finance experts, product managers, and project managers in the world. 顶级公司雇佣Toptal自由职业者来完成他们最重要的项目.

Interview Questions

1.

这个多模型验证规则有什么问题? 两个、一个还是两个都不执行?为什么? 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.'
    )
)
View answer

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 规则将简单地覆盖它(因为多维数组具有相同的键).

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

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

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

同样,将上述代码行与以下代码行进行比较:

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

各自的相对优势是什么? Which would you use and why?

View answer

The set() 方法用于在视图文件中创建变量. In the example above, the variable $articles 然后可以在视图模板文件中用于该操作吗.

An advantage of the first example (i.e., $this->set('articles', $articles); 它是否允许视图上的变量名与控制器上的变量名不同. 例如,如果你想让它们不同,你可以这样做 $this->set('articlesData', $articles);. 视图文件上的变量将是 $articlesData.

第二种方法的优点是.e., $this->set(compact('articles'));另一方面,它更整洁,也更不容易出错. It is also shorter and easier to write, 特别是在我们为视图设置几个变量的地方.

3.

在下面的代码行中,如何能够 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');
View answer

The Containable behavior 允许开发人员指定从对象中检索哪些关联模型(如果有的话) find query.

因此,要确保在上面的示例中没有返回其他相关模型, it could simply be re-written as:

$this->Region->find('all', array('contain' => false));

申请加入Toptal的发展网络

and enjoy reliable, steady, remote Freelance CakePHP Developer Jobs

Apply as a Freelancer
4.

A current alternative to the Containable behavior is to use the recursive function. Why is the use of the recursive 下面的函数在我们只想检索区域数据而没有任何模型关联的情况下是不正确的?

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

The use of recursive 是不正确的在上面的例子(如果我们不想拉任何相关的数据),因为 recursive works as follows:

  • -1 —没有关联数据被检索到 find query.
  • ` 0 – Retrieves any BelongsTo` associated data.
  • ` 1 -检索任何直接相关的关联(例如.e., BelongsTo, HasMany, HasOne, HasAndBelongsToMany`).
  • ' 2 ' -检索任何直接相关的关联,以及它们的关联的关联.

So in the above, example, 应该使用-1(而不是0)来避免提取任何相关数据.

It is also important to note that recursive 将在CakePHP 3中被淘汰,取而代之的是 Containable behavior.

5.

组件、行为和Helper之间的区别是什么? 提供一个示例,说明在何处和何时可以使用每种方法.

View answer

这三个都是相似的,因为它们的作用是扩展现有的CakePHP功能, but the differ on what they extend:

For example:

  • A shopping cart Component 可能会提供可以在多个控制器之间使用和共享的功能.
  • A custom upload Behavior 可以用来扩展模型,例如添加图片上传. Another common example of a Behavior 是在CakePHP默认提供的功能之外添加额外的验证功能吗.
  • A Helper 可以用来辅助视图功能吗. A good example here would be the CK Editor helper that makes it easy to display a CK Editor.

面试不仅仅是棘手的技术问题, 所以这些只是作为一个指南. 并不是每一个值得雇佣的“A”候选人都能回答所有的问题, 回答所有问题也不能保证成为A级考生. At the end of the day, 招聘仍然是一门艺术,一门科学,需要大量的工作.

Why Toptal

Tired of interviewing candidates? 不知道该问什么才能让你得到一份好工作?

Let Toptal find the best people for you.

Hire a Top CakePHP Developer Now

我们的CakePHP开发者独家网络

希望找到一份作为CakePHP开发人员的工作?

Let Toptal find the right job for you.

Apply as a CakePHP Developer

Job Opportunities From Our Network

Submit an interview question

提交的问题和答案将被审查和编辑, 并可能会或可能不会选择张贴, at the sole discretion of Toptal, LLC.

* All fields are required

Looking for CakePHP Developers?

Looking for CakePHP Developers? Check out Toptal’s CakePHP developers.

Jason Funk

Freelance CakePHP Developer

United StatesToptal Member Since August 24, 2015

Jason是一位经验丰富的web开发人员,拥有广泛的专业和个人技术成就. 他是Tough Space Consulting的创始人, 一家基于云计算的商业软件咨询公司,通过改进内部业务流程,帮助许多客户提高效率并节省资金. Jason是一个很强的团队合作者,但同时也喜欢接受个人挑战.

Show More

Chady Kassouf

Freelance CakePHP Developer

CanadaToptal Member Since February 17, 2017

Chady是一名全栈web和iOS开发者. 他精通Objective-C和Swift, 并且在使用LAMP堆栈运输产品方面有着很长的记录. 他在所有前端方面都有丰富的经验,可以与设计师和UX团队密切合作.

Show More

Andu Fratu

Freelance CakePHP Developer

SpainToptal Member Since December 11, 2018

Since 2007, Andu一直以程序员的身份谋生,他既是一名自由职业者,也是大大小小的公司各种团队的一员. 始终如一的是,他总是追求卓越,在他的下一个项目中超越自己. 他还拥有网络和移动技术方面的经验,并且总是准备迎接挑战. 安杜非常习惯于在不同的环境中与来自世界各地的人一起工作.

Show More

Toptal Connects the Top 3% of Freelance Talent All Over The World.

Join the Toptal community.

Learn more