Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
3 | class TrainingSignupForm extends Form |
||
|
|||
4 | { |
||
5 | public function __construct($controller, $name, $title = "Training") |
||
48 | |||
49 | public function doSave($data, $form) |
||
85 | |||
86 | /* |
||
87 | |||
88 | I don't think saveInto can deal with saving many_many relations |
||
89 | currently, unless your Post class has a customised method called |
||
90 | saveCategories($data) in which you simply add the code like this: |
||
91 | |||
92 | $this->Categories->add($item, $extraFields), |
||
93 | where $item should be the category's ID (you can get it from $data), |
||
94 | depending on how your Form is stuctured, your $extraFields should be |
||
95 | either from $data, or assigned from backend by you before you call |
||
96 | saveInto in doPostForm or before you call $this->Categories->add |
||
97 | saveCetegories. |
||
98 | |||
99 | The name of saving many_many relation (here refer to saveCategories) |
||
100 | must bond with your field name, ie, if your field is called: |
||
101 | new DropdownField('GoneWithWind',"Category", $catOptions), the save |
||
102 | method must name as saveGoneWithWind(). |
||
103 | |||
104 | if PrimeCategory need to be also submitted from from, your your can |
||
105 | get CategoryID and PrimeCategory in an array from $data, try this: |
||
106 | new DropdownField('Categories[CetegoryID]',"Category", $catOptions), |
||
107 | new CheckboxField('Categories[PrimeCategory]', "Prime Category?"), |
||
108 | The $data[Categories] submitted from from is a array. |
||
109 | |||
110 | In addition, dropdown field is not supposed to simulate many_many |
||
111 | relation, CheckboxSetField is aim to submit multiple IDs from a form |
||
112 | (maybe you have you special consideration here?). If this is the case, |
||
113 | $this->Categories->add need to change to $this->Categories- |
||
114 | |||
115 | */ |
||
116 | } |
||
117 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.