Могу ли я заблокировать обновление одной таблицы? - PullRequest
1 голос
/ 28 июня 2019

У меня есть две таблицы в моей базе данных: одна practitioner и вторая practitioner_specialty, обе они имеют поля effective_date и expiry_date.Когда я использую форму для обновления обеих таблиц, последние effective_date и expiry_date сохраняются в обеих таблицах вместо двух отдельных.Есть ли способ заблокировать обновление одной таблицы, чтобы я мог обновить только вторую, или, может быть, есть способ сохранить ее, используя другой идентификатор / имя, чтобы они были уникальными для practitioner и practitioner_specialty?

Вот моя форма и контроллер, используемый для обновления таблиц.

Контроллер:

public function updatePractitioner(Request $request, $id)
    {

        $this->validate($request, [
                'effective_date' => 'required',
            ]
        );

        $fields = $request->all();
        $primary_key = $this->PractitionerRepository->getIdName();
        $primary_key_specialty = $this->PractitionerSpecialtyRepository->getIdName();
        $practitioner_specialty_id = PractitionerSpecialty::where('practitioner_id', $id)->value('practitioner_specialty_id');
        $fields[$primary_key] = $id;
        $this->PractitionerRepository->update($fields);
        $fields[$primary_key_specialty] = $practitioner_specialty_id;
        $this->PractitionerSpecialtyRepository->update($fields);
        return back()->with('successUpdate', 'Practitioner has been updated!');
    }

форма обновления в blade.php:

<div class="edit-practitioner" style="display: none;">
    <form style="box-shadow: none;" action="/practitioner/update/{{$practitioner->practitioner_id}}" method="post"
          class="j-pro" id="update-practitioner-form">
        {{ csrf_field() }}
        <div class="j-content">
            <div id="j-row-id" class="j-row">
                <div class="row">
                    <div class="col-sm-12 col-lg-12 col-xl-5">
                        <div class=" j-unit">
                            <div class="j-divider-text j-gap-top-20 j-gap-bottom-45">
                                <span>Practitioner Information</span>
                            </div>
                            <label class="j-label">{{trans('personalData.effectiveDate')}}</label>
                            <div class="j-input">
                                <input type="date" value="{{$practitioner->effective_date}}"
                                       name="effective_date" id="effective_date">
                            </div>

                            <label class="j-label">{{trans('personalData.expiryDate')}}</label>
                            <div class="j-input">
                                <input type="date" value="{{$practitioner->expiry_date}}"
                                       name="expiry_date" id="expiry_date">
                            </div>

                            <label class="j-label">{{trans('personalData.phoneNumber')}}</label>
                            <div class="j-input">
                                </label>
                                <input type="tel" value="{{$practitioner->phone}}"
                                       name="phone" id="phone">
                            </div>

                            <label class="j-label">{{trans('personalData.mobileNumber')}}</label>
                            <div class="j-input">
                                <input type="tel" value="{{$practitioner->mobile}}"
                                       name="mobile" id="mobile">
                            </div>

                            <label class="j-label">{{trans('personalData.email')}}</label>
                            <div class="j-input">
                                <input type="email" value="{{$practitioner->email}}"
                                       name="email" id="email">
                            </div>
                        </div>
                    </div>
                    <div class="col-xl-1 j-unit"></div>
                    <div class="col-sm-12 col-lg-12 col-xl-6">
                        <div class="j-divider-text j-gap-top-20 j-gap-bottom-45">
                            <span>{{trans('settings.specialty')}}</span>
                        </div>
                        <select name="practitioner_specialty_id_update"
                                id="practitioner_specialty_id_update"
                                class="form-control-practitioner required">
                            @foreach($specialties as $specialty)
                                <option
                                    value="{{$specialty->specialty_id}}">{{$specialty->name}}</option>
                            @endforeach
                        </select>
                        <label class="j-label">{{trans('personalData.effectiveDate')}}</label>
                        <div class="j-input">
                            @isset($practitioner_specialty->practitioner_specialty_id)
                                <input type="date" value="{{$practitioner_specialty->effective_date}}"
                                       name="effective_date" id="effective_date">
                            @endisset
                            @empty($practitioner_specialty->practitioner_specialty_id)
                                <input type="date" name="effective_date" id="effective_date">
                            @endempty
                        </div>

                        <label class="j-label">{{trans('personalData.expiryDate')}}</label>

                        <div class="j-input">
                            @isset($practitioner_specialty->practitioner_specialty_id)
                                <input type="date" value="{{$practitioner_specialty->expiry_date}}"
                                       name="expiry_date" id="expiry_date">
                            @endisset
                            @empty($practitioner_specialty->practitioner_specialty_id)
                                    <input type="date" name="expiry_date" id="expiry_date">
                            @endempty
                        </div>
                    </div>
                </div>
            </div>
            <div class="j-divider j-gap-bottom-45 j-gap-top-10"></div>
            <button type="submit"
                    class="btn btn-editpanel btn-success btn-round">Save changes
            </button>
            <!-- end /.footer -->
            <button id="update-cancel-button-practitioner" href="javascript:window.location.href=window.location.href" type="button"
                    class="btn btn-editpanel btn-danger btn-round">Cancel
            </button>
        </div>
    </form>
</div>

Ответы [ 2 ]

3 голосов
/ 28 июня 2019

Хорошо, вы используете один и тот же массив для обеих таблиц, поэтому есть причина обновить все поля. Вы можете попытаться исключить те из них для одной формы и добавить их в другую, например:

$practictionerFields = $request->except('expiry_date', 'effective_date');
$practictionerFields[$primary_key] = $id;
$this->PractitionerRepository->update($practictionerFields);

// and use the other $fields array for the PractitionerSpecialty model.
0 голосов
/ 28 июня 2019

Вы можете использовать запрос LOCK здесь. Блокировка - это флаг, связанный с таблицей. MySQL позволяет клиентскому сеансу явно получать блокировку таблицы для предотвращения доступа других сеансов к той же таблице в течение определенного периода. Сеанс клиента может получить или снять блокировку таблицы только для себя. Он не может получить или снять блокировку таблицы для других сеансов.

Вы можете прочитать больше здесь: http://mysqltutorial.org/mysql-table-locking

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...