this is not about databases, and it is not a php thing. And it's not specifically about IDs, it was just an example, and the situation where I became aware of the bug/issue. I am fully aware of the boolean comparison pecularities of php, this is not an issue in this case - or rather - it actually may seem to be but it shouldn't.
The value of zero is fully accessible through normal php methods. It is even available through phalcon itself with a little hacky trick. This is about how phalcon takes parameters/arguments delivered through the documented mechanisms and transfer them to the internal delivering mechanism. For some reason 0 is treated differently to all other numeric values.
simply put if I need to pass a value into a controller method:
/mycontroller/action/param
phalcon should not care what value param has, it should certainly not through it away all-together if is zero. That is
/mycontroller/action/5
Should be just as valid as
/mycontroller/action/0
EDIT here is an example that illustrates the issue (just put it in a class method):
Well, the original example was wrong, correcting it - which reveal an even more surprising detail: the last 0 is only removed if it's the only parameter. If there are two parameters, the last of the two is not removed even if it's 0.
Imagine a method with 2 optional parameters
public function editAction($var1=null,$var2=null)
{
$result[] = $this->router->getMatches();
$result[] = $this->router->getParams();
echo "<pre>",print_r($result,true),"</pre>";
// ... code dealing with none, one or two parameters
}
Now consider the following three urls:
/namespace/controller/edit
/namespace/controller/edit/0
/namespace/controller/edit/0/0
// /namespace/controller/edit
// /namespace/controller/edit/0
// (both lead to the same result)
Array
(
[0] => Array
(
[0] => /all/usrstat/edit
[1] => usrstat
[2] => edit
)
[1] => Array
(
)
)
// /namespace/controller/edit/0/0
Array
(
[0] => Array
(
[0] => /all/usrstat/edit/0/0
[1] => usrstat
[2] => edit
[3] => /0/0
)
[1] => Array
(
[0] => 0
[1] => 0
)
)
My current workaround for this method (if someone should be interested):
public function editAction($var1=null,$var2=null)
{
if(is_null($var1)) {
if(isset($this->router->getMatches()[3])) {
// $var1 exists alone and must be 0
// remove first character (leading /)
$var1 = substr($this->router->getMatches()[3],1);
}
}
// ... code dealing with none, one or two parameters
}