What is the best way to provide a NULL option for the ownTo field in CakePHP 1.3?

I have a table called "users", and each user can belong to one group, described in a table called "categories". An example of use is “Alice belongs to the red category” and “Bob belongs to the category“ Blue ”, etc.

I use the CakePHP form helper, and on the page http: // localhost / users / edit / [user_id] he made a drop-down list to select a category (using JOIN in the model to get category names).

But I want to add an option to select NULL, because not all users must be part of a category. Adding a NULL entry to the category table will not work because the foreign key in the users table is category_id. I want "category_id" for a user who did not select a category as NULL, and not 1, which in another table is NULL.

Sorry if this was a little incoherent. Hope someone can follow him enough to tell me if there is a way for cakePHP, or should I come up with another way to do this.

+3
source share
2 answers

@Stevelove's answer is correct, but I would use the following function:

$this->Form->input('category_id', array('empty'=>true, 'options'=>$categories));

. , "

$this->Form->input('category_id', array('empty'=>'Select category', 'options'=>$categories));
+5

, "" ? ?

$this->Form->select('group', $options, null, array('empty' => 'None'));
+4

Source: https://habr.com/ru/post/1773097/


All Articles