Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Strict in_array for mixed id variable types #1075

Merged
merged 1 commit into from

5 participants

@johnymonster

Setting the in_array check to strict, as this would return true
incorrectly when and if values are of mixed type.

Issue: http://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/3545

@johnymonster johnymonster Strict in_array for mixed id variable types
Setting the in_array check to strict, as this would return true
incorrectly when and if values are of mixed type.
84b10ba
@dereuromark
Collaborator

It would probably be wise to add a test case here - to prevent this from happening again in the future.

@ADmad
Collaborator

As commented on the ticket I am curious how you got an id value as integer since PDO returns all values as string (even for integer columns)

@johnymonster

When you say PDO, are you talking about using cakephp to retrieve the rows or are you just using PDO directly.

Performing a $Model->find('list'); on a table with rows that contain integers will return (int) values as the key not strings. Without looking at the model code for list, im assuming that maybe its doing a type cast if the value is a possible int value?

@ADmad
Collaborator

I meant through cakephp but PDO's behavior isn't going to change even if you use directly.

Anyways I found out that it's array_combine which converts numeric string keys to integers.

@johnymonster

Good to know, thanks!

@markstory
Owner

Once a test is added this looks good to go as far as I can see.

@markstory markstory referenced this pull request from a commit
@markstory markstory Add tests for #3545
Fix included in GH-1075

Closes #3545
0b659d5
@markstory markstory merged commit 84b10ba into from
@augusto-cdxs

This is making trouble with HABTM relations and multiple select/checboxes:

  • Let's suppose Model A HABTM Model B.
  • If we create a form for Model A, shown options for Model B field, being a multiple select or multiple checkboxes, come an array like [id => displayField], being extracted, e.g., from a query like $Model2->find('list'). Because find('list') makes use of array_combine, the resulting keys of the array (the primary key) has an int type.
  • If we want the FormHelper to show currently associations, we must fetch the Model A with the associated Models B. A query like find('all') containing Model B, will have all of its field in a string type.
  • Finally, when FormHelper compares in _selectOptions both data in a strict way, it won't find any matches to mark as selected/checked.

If I'm not making myself clear, please make me know...

@markstory
Owner

Yes, it has since been fixed. Try downloading a new release of CakePHP or using the master branch, as it should be fixed.

@augusto-cdxs

Indeed, it was fixed. Some days ago I looked for it I must have chosen the wrong tag or branch...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 17, 2013
  1. @johnymonster

    Strict in_array for mixed id variable types

    johnymonster authored
    Setting the in_array check to strict, as this would return true
    incorrectly when and if values are of mixed type.
This page is out of date. Refresh to see the latest.
Showing with 1 addition and 1 deletion.
  1. +1 −1  lib/Cake/View/Helper/FormHelper.php
View
2  lib/Cake/View/Helper/FormHelper.php
@@ -2471,7 +2471,7 @@ protected function _selectOptions($elements = array(), $parents = array(), $show
if ($name !== null) {
if (
(!$selectedIsArray && !$selectedIsEmpty && (string)$attributes['value'] == (string)$name) ||
- ($selectedIsArray && in_array($name, $attributes['value']))
+ ($selectedIsArray && in_array($name, $attributes['value'], true))
) {
if ($attributes['style'] === 'checkbox') {
$htmlOptions['checked'] = true;
Something went wrong with that request. Please try again.