Bug with Criteria::fromInput and Model::columnMap?

Hi everyone,

I think I might have hit a bug in Phalcon with Criteria::fromInput and Model::columnMap.

I have a database table called "localized_languages" with fields "id", "language_id", "localized_language_id" and "name". In my controller I have the following code to search for localized languages:

    $this->view->pick("localized-languages/search");
         $numberPage = 1;
         if ($this->request->isPost()) {
             $query = Criteria::fromInput($this->di, 'My\Models\LocalizedLanguages', $this->request->getPost());
             echo var_dump($query);
             $this->persistent->searchParams = $query->getParams();
         } else {
             $numberPage = $this->request->getQuery("page", "int");
         }

         $parameters = array();
         if ($this->persistent->searchParams) {
             $parameters = $this->persistent->searchParams;
         }

         $localizedLanguages = LocalizedLanguages::find($parameters);
        (...)

Now in my model I have the following column mappings:

      public function columnMap()
      {
          return array(
              'id' => 'id',
              'language_id' => 'languageId',
              'localization_language_id' => 'localizationLanguageId',
              'name' => 'name'
          );
      }

If I execute this code, a var_dump of the $query object gives me the following:

object(Phalcon\Mvc\Model\Criteria)#138 (3) { ["_model":protected]=> string(34) "My\Models\LocalizedLanguages" ["_params":protected]=> NULL ["_hiddenParamNumber":protected]=> int(0) } 

As a result, $parameters remains an empty array and I get back the complete list of "Localized Languages" instead of a list filtered by localizationLanguageId.

The following query is generated:

[Fri, 10 Oct 14 14:56:05 +0200][INFO] SELECT `localized_languages`.`id`, `localized_languages`.`language_id`, `localized_languages`.`localization_language_id`, `localized_languages`.`name` FROM `localized_languages`

BUT: if I comment the columnMap function and delete the metadata, then var_dump($query) gives me the following:

object(Phalcon\Mvc\Model\Criteria)#138 (3) { ["_model":protected]=> string(34) "My\Models\LocalizedLanguages" ["_params":protected]=> array(2) { ["conditions"]=> string(51) "localization_language_id=:localization_language_id:" ["bind"]=> array(1) { ["localization_language_id"]=> string(2) "39" } } ["_hiddenParamNumber":protected]=> int(0) } 

Criteria now has parameters, so I get back the correct list. The following query is generated:

[Fri, 10 Oct 14 14:51:37 +0200][INFO] SELECT `localized_languages`.`id`, `localized_languages`.`language_id`, `localized_languages`.`localization_language_id`, `localized_languages`.`name` FROM `localized_languages` WHERE `localized_languages`.`localization_language_id` = :localization_language_id

Am I missing something very obvious here, or is this indeed a bug?



85.4k

What version of Phalcon are you using?



2.0k

Phalcon 1.3.2 on Windows x64 for PHP 5.5.0 (VC11)



85.4k

Could you try again using 1.3.3?



2.0k
edited Oct '14

Hi Phalcon,

I just tried with Phalcon 1.3.3. Now I get an Exception when the columnMap is there. Please see the generated metadata files attached. meta 1 meta 2

Here is a screenshot of the exception that is thrown in the debugger: debugger screenshot. As you can see, this makes no sense, because the column the exception complains about IS defined in the model, exists in the database, and is in the mapping files.

If I comment the columnMap function and delete the generated metadata files, it works again.



2.0k

And a bug in the forum too apparently :)

Using the "attachment" button, the underscores in the URL's get replaced by <em>. These links to the metadata files should work: https://dl.dropboxusercontent.com/u/30446781/webshots/phalcon/map-dragenda_models_localizedlanguages.php https://dl.dropboxusercontent.com/u/30446781/webshots/phalcon/meta-dragenda_models_localizedlanguages-localized_languages.php



2.0k

Hi Phalcon,

I am now 100% certain that is is indeed a bug. I have changed my model's variable names to match the database table field names, updated my controller and form, and now everything works as expected, with or without the columnMap function. So Criteria::fromInput + custom column mappings via Model::columnMap produces errors.

I can send you a demo project if you'd like, so that you can reproduce the problem.



85.4k

Thanks, please submit the description of the bug on Github https://github.com/phalcon/cphalcon/



2.0k

Will do, thanks!