During my work on Neutrino, I've encountered a small glitch in Cake's FormHelper automagic. Since I want every URL to be slugged, all my calls to FormHelper::create(...) have the first parameter set to null:
echo $form->create(null, ...);
Things went pretty fine when I used to write the code like the following:
echo $form->create ( null, array('url' => '/articles/edit/'.$slug) );
Several days ago, I've decided to rewrite all the links in the whole project to support the new reverse route style of writing links. So I wrote the following:
echo $form->create ( null, array ( 'url' => array ( 'controller' => 'articles', 'action' => 'edit', $slug ) ) );
One would expect the output to be something like this:
<form action="articles/edit/the-slug" method="post">
But instead you get this:
<form action="articles/edit/1/the-slug" method="post">
Obviously, the update will fail, your action will get "1" as slug instead of "the-slug". The value "1" is of course the value of primary key of my Articles model, and Cake seems to take in into account even though I've specified the URL as a parameter. Strange.
I thought to myself, maybe I need to pass the id in params, so I wrote this little poor attempt:
echo $form->create ( null, array ( 'url' => array ( 'controller' => 'articles', 'action' => 'edit', 'id' => $slug ) ) );
Long story short - no dice.
Apparently, the only way to hack this is to unset() the primary key in my controller. It's ugly, but it's working..
$this->data = $this->Article->findById($id); unset($this->data['Article']['id']);
Of course, I can do this because I'm not using the id anywhere, but someone who does is going to have to stick to the old style of writing URLs for their forms.
Still, one has to wonder why is FormHelper::create(...) messing around with the URL I've specified as a param in the first place. Maybe this is a Trac ticket material?