S0ul3r
S0ul3r

Reputation: 153

Can I block one table from updating?

I have two tables in my database one practitioner and the second one practitioner_specialty, both of them have fields effective_date and expiry_date. When I use the form to update both if those tables the last effective_date and expiry_date are being saved to both of the tables instead of two separate ones. Is there a way to block one table from updating so I can update only the second one, or maybe is there a way to save it using different id/name so they will be unique ones for practitioner and practitioner_specialty?

Here are my form and controller used for updating tables.

Controller:

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!');
    }

update form in 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>

Upvotes: 1

Views: 76

Answers (2)

Hrishikesh Shukla
Hrishikesh Shukla

Reputation: 61

You can use LOCK query here. lock is a flag associated with a table. MySQL allows a client session to explicitly acquire a table lock for preventing other sessions from accessing the same table during a specific period. A client session can acquire or release table locks only for itself. It cannot acquire or release table locks for other sessions.

You can read more here: http://mysqltutorial.org/mysql-table-locking

Upvotes: 0

nakov
nakov

Reputation: 14248

Well you use the same array for both of the tables, hence the reason to update all the fields. You can try to exclude the ones for one form and add them to the other, for example:

$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.

Upvotes: 3

Related Questions