Welcome
Welcome to the second edition of the CakePHP weekly summaries.
Its quite embarrassing that it had to take so long time to get the second editon out. Too long time has passed and the amount of things that has happend during that period is waaay too big to be covered here.
Therefor I present this half-done 2nd edition of the weekly summaries, with a promise that every week from now on I will give you the latest and greatest from the CakePHP world.
So with no further ado, please enjoy this old and probably rather outdated edition of the weeklymonthly summaries. See you at the 3rd edition soon
Trac changes
Google group topics
Why doesn't Find* return a group of model objects?
This topic has been discussed many times before, but Deane felt that it was required to ask yet again: “Why doesn't Find* return a group of model objects?“. He wanted to be able to work with his models in the view (!BAD DESIGN!), and was confused why he only got an array back from the find* methods. Gwoo replied, and I quote: “Speed, simplicity, clarity” and pointed out that Cake provide view helpers to work with the arrays in almost any way you can imagine, and that the models provide callback functions (afterFind, afterSave, beforeFind, beforeFind) to format your data.
Deane thanked gwoo for the reply, but “We couldn’t disagree more strenuously, and we think you’re wrong beyond belief in your reasoning, but we appreciate the response”…. didnt like gwoo’s reply at all.
Gwoo replied in a calmly matter that “Everyone is entitled to their opinion and their choice of frameworks.”
rtconner joined the discussion, offering a solution based on some old code by gwoo that would enable the models to return objects (StdClass, not models classes)instead of arrays.
Nick also joined the thread, backing up Deaen’s point of view (Cake is wrong, symfony lover is right) that its proper OO to use objects instead of arrays for the view data, but also reasoned that cake does indeed have the needed hooks and concepts to achieve Deane’s initial idea with a minimal effort. He explained that in a project he had created with CakePHP was that relationships (belongsTo, hasMany ect.) get more complicated with the OO styled approach, since the queries often fetched much more data than was really needed in the view. The combination of the controller didnt know wich fields the view required and the complex relation ships was in his opinion, not ideal at all. Their work a round was to create a “LazyData” class to handle it.
Robby Anderson dropped in his 2 cents, and agreed that the cake was isnt 100% pure OO, but that the concept of “having the view incapable of performing any meaningful business logic” is bad, and will sooner or later lead to an unmanagelbe code base.
Deane, the creater of the thread, posted a public apoligy to gwoo, explaining that he was fustrated at the time when he posted his previous post.
So, the summary must be that while some people dont like the design decission that CakePHP models return arrays instead of objects, others agree that its the right way to encapsulate the buisness logic in a correct Model View Control manner, and if you want to break theese convensions, its rather easy to do with minimal effort.
Passing Data To A Form
Travis asked how you are Passing Data To A Form. A bit unexpted (or perhaps expected given the flood of duplicate / newbie questions hitting the groups daily) nobody answered his question. The answer is pretty simple, and covered in any tutorial aviable on cakephp website AND in the documenation / manual.
1
2
3
function update($employeeID) {
$this->data = $this->Employee->findAllById($employeeID);
}
The Show Is Back!
Jeff Loiselle aka phishy announced that the show is back. No replies, no thing, its sad… the show rocks !
I18n in 1.2 model validation message
Athies asked how does i18n in 1.2 model validation message work. His iniital attemp was to use the i18n method ‘__()’ in the model $validate array, but he was getting syntax errors when doing that.
Since PHP does not support useage of functions in instance delclarations its not possible to use i18n directly in the validate array.
Francky06l pointed at his solution in another google post i18n: static language references with __("My lang key", true) in models and controllers, and while the solution will work, its far from ideal when working with i18n.
A CakePHP google group regular, Dr. Tarique Sani, pointed out that the correct way to handle i18n validation messages was in the view, not at the model level. Amit Badkas backed up Tarique up and provided a simple example on how to do it:
1
2
3
4
5
< ?php echo
$form->input('username', array('error' => array(
'alphanumeric' => __('Username must contain alpha-numeric characters', true),
'between' => __('Username must be between 8 to 20 characters', true))));
?>
Francky06l seemed to know that, his consern was the if he multiple views where the validation errors should show, it would be convenient to do it at model level.
Robby Anderson wasnt sure why you just couldnt do both, create the validation errors in the model, and wrap them in __() in the view.
And sure, that will work – but – you will loose some cake magic with that. The console application
cake i18n extract
will not add the i18n messages to the pot file automagic. Also, the i18n extract command will not even search your models for any i18n magic. It will only look in controllers and views. You can choose to call this a limit or a feature, but thats how cakephp has decided to do it, to enforce their paradime on how an application should be build and structured.
FormHelper Labels
Travis asked how it was possible to disable the automagic label in the FormHelper. Robby Anderson provided the correct solution for this issue, use ‘label’ => false:
< ?php echo $form->input('Employee.name',array('label' => false));?>
Using model objects in the controllers
Deane from earlier posted another question on Using model objects in the controllers. I dont really want to dwell much on this post, since its really attemp to take CakePHP in another direction than its supposed to. Deane clearly shows a lack of insight on how cakephp is supposed to work, and failed to provide any usefull examples that could show the group what he wanted to do.
Tags: Cakephp, english, weekly summaries