CakePHP - проблема со слиянием добавления и редактирования - PullRequest
0 голосов
/ 15 октября 2019

Я пытаюсь «объединить» функцию добавления и редактирования, так сказать, в одну функцию «сохранения», которая, независимо от того, предоставляется ли $ id модели, обновляет или добавляет новый экземпляр модели. Начав кодировать это, я понял, что эта идея приносит больше сложностей, чем встроенные в CakePHP методы добавления и редактирования. Дело в том, что мой наставник настаивает на том, чтобы я слил это, поэтому я попробую, даже если лично я считаю, что это не лучший подход.

ItemTypesController.php

class ItemTypesController extends AppController {
public function save($id = null) {
    if($this->request->is(array('post', 'put'))){
        $this->ItemType->set($this->request->data);
        //if i put an exit() funct here, it exits on this spot
        if($this->ItemType->validates()){
            if(!$id){
                $this->ItemType->create();
                if ($this->ItemType->save($this->request->data)) {
                    $this->Flash->success(__('The item type has been saved.'));
                    return $this->redirect(array('action' => 'index'));
                } else {
                    $this->Flash->error(__('The item type could not be saved. Please, try again.'));
                }
            }
            else{
                if ($this->ItemType->save($this->request->data)) {
                    $this->Flash->success(__('The item type has been saved.'));
                    return $this->redirect(array('action' => 'index'));
                } else {
                    $this->Flash->error(__('The item type could not be saved. Please, try again.'));
                }
            }
        } else {
            $this->Flash->warning($this->ItemType->validationErrors, array(
                'key' => 'negative'
            ));
        }

    } else {
        $options = array('conditions' => array('ItemType.' . $this->ItemType->primaryKey => $id));
        $this->request->data = $this->ItemType->find('first', $options);
    }
    $this->set('classes', $this->ItemType->classes);
}
}

Таким образом, в основном, функция будет вводиться, если блок не указан, если $ id не указан, то есть, если я ввожу ссылку для нового ItemType. И он прекрасно работает для создания, но, когда я пытаюсь обновить его, он всегда требует, чтобы любое поле было изменено, из-за правила «isUnique», я устанавливаю это правило только для создания, но CakePHP всегда думает о своей функции создания, хотя здесь используется моя функция save (), и она, вероятно, думает, что ей нужно ввести это правило для моей функции.

ItemType.php

class ItemType extends AppModel {

public $classes = array(
    'product' => 'Proizvod',
    'kit' => 'Kit (bundle)'
);

public $validate = array(
    'code' => array(
        'required' => array(
            'rule' => 'notBlank',
            'message' => 'A code is required'
        ),
        'alphanum' => array(
            'rule' => 'alphanumeric',
            'message' => 'A code must be an alphanumeric value'
        ),
        'unique' => array(
            'rule' => 'isUnique',
            'message' => 'This code already exists!',
            'required' => 'create'
        ),
        'between' => array(
            'rule' => array('lengthBetween', 3, 7),
            'message' => 'Code must be between 3 and 7 characters long'
        )
    ),
    'name' => array(
        'required' => array(
            'rule' => 'notBlank',
            'message' => 'A name is required'
        ),
        'unique' => array(
            'rule' => 'isUnique',
            'message' => 'This name already exists!',
            'required' => 'create'
        ),
        'between' => array(
            'rule' => array('lengthBetween', 3, 30),
            'message' => 'Name must be between 3 and 30 characters long'
        )
    ),
    'class' => array(
        'valid' => array(
            'rule' => array('inList', array('product', 'material', 'kit', 'semi_product', 'service_product', 'service_supplier','consumable','inventory','goods','other')),
            'message' => 'Please enter a valid class',
            'allowEmpty' => false
        )
    ),
    'tangible' => array(
        'bool' => array(
            'rule' => 'boolean',
            'message' => 'Incorrect value for the checkbox'
        )
    ),
    'active' => array(
        'bool' => array(
            'rule' => 'boolean',
            'message' => 'Incorrect value for the checkbox'
        )
    )
    );

public $hasMany = array(
    'Item' => array(
        'className' => 'Item',
        'foreignKey' => 'item_type_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
);
}

Это представление, с которого яввод нового или обновление:

index.ctp

    <div class="itemTypes index">
    <h2><?php echo __('Item Types'); ?></h2>
    <table cellpadding="0" cellspacing="0">
    <thead>
    <tr>
            <th><?php echo $this->Paginator->sort('id'); ?></th>
            <th><?php echo $this->Paginator->sort('code'); ?></th>
            <th><?php echo $this->Paginator->sort('name'); ?></th>
            <th><?php echo $this->Paginator->sort('class'); ?></th>
            <th><?php echo $this->Paginator->sort('tangible'); ?></th>
            <th><?php echo $this->Paginator->sort('active'); ?></th>
            <th><?php echo $this->Paginator->sort('created'); ?></th>
            <th><?php echo $this->Paginator->sort('modified'); ?></th>
            <th class="actions"><?php echo __('Actions'); ?></th>
    </tr>
    </thead>
    <tbody>
    <?php foreach ($itemTypes as $itemType): ?>
    <tr>
        <td><?php echo h($itemType['ItemType']['id']); ?>&nbsp;</td>
        <td><?php echo h($itemType['ItemType']['code']); ?>&nbsp;</td>
        <td><?php echo h($itemType['ItemType']['name']); ?>&nbsp;</td>
        <td><?php echo h($itemType['ItemType']['class']); ?>&nbsp;</td>
        <td><?php if($itemType['ItemType']['tangible']) echo "Yes"; else echo "No" ?></td>
        <td><?php if($itemType['ItemType']['active']) echo "Yes"; else echo "No" ?></td>
        <td><?php echo h($itemType['ItemType']['created']); ?>&nbsp;</td>
        <td><?php echo h($itemType['ItemType']['modified']); ?>&nbsp;</td>
        <td class="actions">
            <?php echo $this->Html->link(__('View'), array('action' => 'view', $itemType['ItemType']['id'])); ?>
            <?php echo $this->Html->link(__('Edit'), array('action' => 'save', $itemType['ItemType']['id'])); ?>
            <?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $itemType['ItemType']['id']), array('confirm' => __('Are you sure you want to delete # %s?', $itemType['ItemType']['id']))); ?>
        </td>
    </tr>
    <?php endforeach; ?>
    </tbody>
    </table>
    <p>
        <?php
        echo $this->Paginator->counter(array(
        'format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}')
    ));
    ?>  </p>
    <div class="paging">
    <?php
        echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
        echo $this->Paginator->numbers(array('separator' => ''));
        echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
    ?>
    </div>
</div>

    <div class="actions">
            <h3><?php echo __('Actions'); ?></h3>
            <ul><li><?php echo $this->Html->link(__('New Item Type'), array('action' => 'save'));         ?>
</li>
</ul>
</div>

save.ctp

<div class="itemTypes form">
    <?php echo $this->Form->create('ItemType'); ?>
    <fieldset>
        <legend><?php echo __('Add Item Type'); ?></legend>
        <?php
        echo $this->Form->input('code');
        echo $this->Form->input('name');
        echo $this->Form->input('class', array('options' => $classes));
        echo $this->Form->input('tangible');
        echo $this->Form->input('active');
        ?>
    </fieldset>
    <?php echo $this->Form->end(__('Submit')); ?>
</div>

    <div class="actions"><h3><?php echo __('Actions'); ?></h3><ul><li><?php echo     $this->Html->link(__('List Item Types'), array('action' => 'index')); ?></li></ul></div>

1 Ответ

0 голосов
/ 16 октября 2019

Функция сохранения не кодируется должным образом, поскольку идентификатор модели никогда не задавался, поэтому она не знала, обновлялась она или нет.

public function save($id = null) {
        if($this->request->is(array('post', 'put'))){
            if($id){
                $this->request->data['ItemType']['id'] = $id;
            }
            $this->ItemType->set($this->request->data);
            if($this->ItemType->validates()){
                if(!$id){
                    $this->ItemType->create();
                }
                if ($this->ItemType->save($this->request->data)) {
                    $this->Flash->success(__('The item type has been saved.'));
                    return $this->redirect(array('action' => 'index'));
                } else {
                    $this->Flash->error(__('The item type could not be saved. Please, try again.'));
                }
            } else {
                $this->Flash->warning($this->ItemType->validationErrors, array(
                    'key' => 'negative'
                ));
            }

        } else {
            $options = array('conditions' => array('ItemType.' . $this->ItemType->primaryKey => $id));
            $this->request->data = $this->ItemType->find('first', $options);
        }
        $this->set('classes', $this->ItemType->classes);}

Теперь вот как должна выглядеть функция.

...