Exemplo n.º 1
0
 public function afterSave($created, $options = array())
 {
     $this->loadModel('Form.PMFormData');
     $sql_field = sprintf(FieldTypes::getSqlTypes($this->data['PMFormField']['field_type']), $this->id);
     $created = $created || isset($options['forceCreate']) && $options['forceCreate'];
     $sql = 'ALTER TABLE ' . $this->PMFormData->getTableName() . ($created ? ' ADD ' : ' MODIFY ') . $sql_field;
     fdebug($sql . "\r\n", 'form_field.log');
     $this->query($sql);
 }
Exemplo n.º 2
0
 public function update4()
 {
     $this->loadModel('Form.FormField');
     $this->loadModel('Form.PMFormValue');
     $this->loadModel('Form.PMFormField');
     $this->loadModel('Form.PMFormData');
     $this->loadModel('SiteArticle');
     // пересохраняем поля, чтобы создать form_data
     $aRowset = Hash::combine($this->FormField->find('all'), '{n}.FormField.id', '{n}.FormField');
     foreach ($aRowset as $formField) {
         if (!FieldTypes::getSqlTypes($formField['field_type'])) {
             echo 'Error! Unexisted field type: ' . $formField['id'] . ' - ' . $formField['field_type'] . '<br>';
             $formField['field_type'] = FieldTypes::STRING;
         }
         $this->PMFormField->save($formField, array('forceCreate' => true));
     }
     echo 'Processed ' . count($aRowset) . ' form fields<br>';
     $conditions = array('object_type' => 'Product');
     $page = 1;
     $limit = 10;
     $order = 'SiteArticle.id';
     $count = array('Product' => 0, 'Param' => 0);
     while ($articles = $this->SiteArticle->find('all', compact('conditions', 'page', 'limit', 'order'))) {
         $page++;
         foreach ($articles as $article) {
             $count['Product']++;
             $id = $article['SiteArticle']['id'];
             $formValues = $this->PMFormValue->getValues('ProductParam', $id);
             $formData = Hash::combine($formValues, '{n}.PMFormValue.field_id', '{n}.PMFormValue.value');
             $data = array('object_type' => 'ProductParam', 'object_id' => $id);
             foreach ($aRowset as $id => $field) {
                 $count['Param']++;
                 if (isset($formData[$id])) {
                     $data['fk_' . $id] = $formData[$id];
                     $data['fk_' . $id] = str_replace('&nbsp;', '', $data['fk_' . $id]);
                     if ($field['field_type'] == FieldTypes::INT) {
                         $data['fk_' . $id] = intval($data['fk_' . $id]);
                     } elseif ($field['field_type'] == FieldTypes::FLOAT) {
                         if ($id == 10) {
                             if (trim($data['fk_' . $id]) === '') {
                                 $data['fk_' . $id] = 0.01;
                             }
                         }
                         $data['fk_' . $id] = floatval(str_replace(',', '.', $data['fk_' . $id]));
                     }
                 }
             }
             $this->PMFormData->clear();
             $this->PMFormData->save(array('PMFormData' => $data));
         }
     }
     echo 'Processed ' . $count['Product'] . ' products, ' . $count['Param'] . ' params <br>';
 }