I had 2 tables. driver and part_time_available in the same form, when I select driver type = parttime, it'll show part_time_available field(day, start_time, end_time).
How to make condition if user choose fulltime. it didn't store part_time_available field to database.
here's my savehandler code so far :
public function saveHandler(Request $request, $obj)
{
try {
DB::beginTransaction();
$obj->fill($request->all());
if (!$obj->save()) {
throw new ValidationException($obj->errors());
}
foreach($request->parttimeAvailabilities as $pta) {
\Log::info($pta);
if (empty($pta['id'])) {
$parttimeAvailability = new PartTimeAvailability();
}
else {
$parttimeAvailability = PartTimeAvailability::find($pta['id']);
}
$parttimeAvailability->driver()->associate($obj);
$pta['driver_id'] = isset($pta['driver_id']);
$parttimeAvailability->day = $pta['day'];
$parttimeAvailability->start_time = isset($pta['start_time']) ? $pta['start_time'] : '00:00:00';
$parttimeAvailability->end_time = isset($pta['end_time']) ? $pta['end_time'] : '00:00:00';
$parttimeAvailability->available = isset($pta['available']);
$parttimeAvailability->save();
};
$obj->save();
if (!$parttimeAvailability->save()) {
throw new ValidationException($parttimeAvailability->errors());
}
DB::commit();
return $this->sendSuccessResponse($request);
} catch (ValidationException $e) {
DB::rollback();
\Log::error($e->errors);
return $this->sendErrorResponse($request, $e->errors);
} catch (Exception $e) {
DB::rollback();
\Log::error($e->getMessage());
return $this->sendErrorResponse($request,'Unable to process. Please contact system Administrator');
}
}
I mean before running foreach, it needs to check it's parttime or not.
any idea ?
You can give a condition before the whole foreach loop. such as:
if($request->get('driver_type') != 'full_time'){
foreach loop
}
Related
I have a problem with this code :
foreach($commandes as $commande)
$conn = $this->em->getConnection();
$conn->beginTransaction();
$conn->setAutoCommit(false);
try {
$tmp = $this->checkCommande($commande); // in this function there is multiple relationship created and pushed
if ($tmp) {
$result = $tmp['result'];
if ('OK' === $result) {
$commande->setCompleted(true);
$this->em->flush();
$conn->commit();
}
}
} catch (\Exception $e) {
$conn->rollBack();
throw $e;
}
}
If I have multiple commands and if one is not committed then all are not committed.
You have set a condition such that commands can only be committed if 'OK' === $result. If this condition is not met, an exception is thrown and the changes are rolled back. To fix this issue, you should move the $conn->commit() call outside of this condition, those changes are always committed unless an exception is thrown. The new code might look like this:
foreach($commandes as $commande)
{
$conn = $this->em->getConnection();
$conn->beginTransaction();
$conn->setAutoCommit(false);
try {
$tmp = $this->checkCommande($commande); // in this function there is multiple relationship created and pushed
if ($tmp) {
$result = $tmp['result'];
if ('OK' === $result) {
$commande->setCompleted(true);
$this->em->flush();
}
}
$conn->commit(); // outside this line
} catch (\Exception $e) {
$conn->rollBack();
throw $e;
}
}
I am trying to use updateOrCreate with multiple conditions and this condition can be applied occasionally if corresponding data are available. I am currently doing as first find the id if exists and if id exists update else create manually but now i want to use laravel given updateOrCreate function.
$existing = Student::where('name_last', $student['name_last'])
->where('name_first', $student['name_first'])
->where(function($query) use ($student) {
if (array_key_exists('phone_preferred', $student) && !empty($student['phone_preferred'])) {
$query->where('phone_preferred', $student['phone_preferred']);
}
if (array_key_exists('email_preferred', $student) && !empty($student['email_preferred'])) {
$query->where('email_preferred', $student['email_preferred']);
}
if (array_key_exists('home_street_address_1', $student) && !empty($student['home_street_address_1'])) {
$query->where('home_street_address_1', $student['home_street_address_1']);
}
if (array_key_exists('mailing_street_address_1', $student) && !empty($student['mailing_street_address_1'])) {
$query->where('mailing_street_address_1', $student['mailing_street_address_1']);
}
if (array_key_exists('mailing_address_city', $student) && !empty($student['mailing_address_city'])) {
$query->where('mailing_address_city', $student['mailing_address_city']);
}
})->first();
if($existing){
try{
Student::where('id', $existing->id)
->update($student);
} catch (\Exception $e) {
$error_encountered = true;
$error_arr[] = $e->getMessage();
$error_row_numbers[] = $row_no;
}
}
I want to implement with something like:
try{
Student::updateOrCreate(
['name_first' => $student['name_first], 'name_last' => $student['name_last]],
$student
); //I could not get how to implement other occasional where condition
} catch (\Exception $e) {
$error_encountered = true;
$error_arr[] = $e->getMessage();
$error_row_numbers[] = $row_no;
}
I want to include those occasional where function query to updateOrCreate method which is currently implementing in manual method
You can just add all the attributes you want to check for in the array that you pass to updateOrCreate
// these are your required conditions
$conditions = [
'name_first' => $student['name_first'],
'name_last' => $student['name_last'],
];
// these are optional
$attributes = [
'phone_preferred',
'email_preferred',
'home_street_address_1',
'mailing_street_address_1',
'mailing_address_city'
];
foreach($attributes as $attr){
// check to see if the attribute exists on the student
// and is not empty
if(isset($student[$attr]) && !empty($student[$attr])){
$conditions[$attr] = $student[$attr];
}
}
Student::updateOrCreate($conditions);
I try this :
public function destroy($id)
{
DB::beginTransaction();
try {
$product = $this->product_repository->find($id);
$result = $product->categories()->detach();
if($result) {
list($status,$instance) = $this->product_repository->delete($id);
}
DB::commit();
return ['status'=>true,'data'=>$status];
} catch (\Exception $e) {
DB::rollback();
return ['status'=>false, 'message'=>$e->getMessage()];
}
}
If the code executed, $this->product_repository->delete($id) not work / not success delete.
But this : $product->categories()->detach();, it works / success deleted.
How to if delete product failed, delete category also failed?
You can't add return statement inside transaction that halts entire process and DB::rollback() is executed.
To switch the return, You can define a boolean variable and make false while you catch exception.
Like this:
public function destroy($id)
{
$success = true;
DB::beginTransaction();
try{
// Your Code
$product = $this->product_repository->find($id);
$result = $product->categories()->detach();
if($result) {
list($status,$instance) = $this->product_repository->delete($id);
}
DB::commit();
}catch(\Exception $e){
DB::rollback();
$success = false;
}
if($success){
// Return data for successful delete
}
else{
// Return data for unsuccessful delete
}
}
Hope you understand.
You can use it like this:
$returnResult = [];
DB::beginTransaction();
try {
...
DB::commit();
$returnResult['status'] = true;
$returnResult['data'] = $status;
} catch (...) {
...
DB::rollback();
$returnResult['status'] = true;
$returnResult['message'] = $e->getMessage();
}
return $returnResult;
I had 2 tables . driver and part_time_available, when I select driver type parttime it'll show part_time_available field. the problem is I can't save.
it throws this error : Integrity constraint violation: 1048 Column 'driver_id' cannot be null
here's my save controller code so far :
public function save(Request $request, $obj = null) {
if (!$obj) {
$obj = new Driver;
}
$obj->active = TRUE;
$obj->counter = 0;
return $this->saveHandler($request, $obj);
}
public function saveHandler(Request $request, $obj)
{
try {
DB::beginTransaction();
$obj->fill($request->all());
if (!$obj->save()) {
throw new ValidationException($obj->errors());
}
foreach($request->parttimeAvailabilities as $pta) {
if (empty($pta['id'])) {
$parttimeAvailability = new ParttimeAvailability();
}
else {
$parttimeAvailability = ParttimeAvailability::find($pta['id']);
}
$parttimeAvailability->Driver()->associate($obj);
$pta['driver_id'] = isset($pta['driver_id']) ? $pta['driver_id'] : null;
$driver = Driver::find($pta['driver_id']);
$parttimeAvailability->driver()->associate($driver);
$parttimeAvailability->day = $pta['day'];
$parttimeAvailability->start_time = $pta['start_time'];
$parttimeAvailability->end_time = $pta['end_time'];
$parttimeAvailability->available = isset($pta['available']);
$parttimeAvailability->save();
};
$obj->save();
if (!$parttimeAvailability->save()) {
throw new ValidationException($parttimeAvailability->errors());
}
DB::commit();
return $this->sendSuccessResponse($request);
} catch (ValidationException $e) {
DB::rollback();
\Log::error($e->errors);
return $this->sendErrorResponse($request, $e->errors);
} catch (Exception $e) {
DB::rollback();
\Log::error($e->getMessage());
return $this->sendErrorResponse($request,'Unable to process. Please contact system Administrator');
}
}
any idea ??
Take a look here:
$pta['driver_id'] = isset($pta['driver_id']) ? $pta['driver_id'] : null;
$driver = Driver::find($pta['driver_id']);
From this code chunk we can see that driver_id can be null. In that case there is no driver to find. You should only search for a driver if you have an id.
I have a collection of items to save to database, but I want the record to be inserted only if not exists.
I think the most effective way would be to filter the collection before saving. Can Doctrine do it automatically?
Or shall I get all id's of all items in the collection, then query the database for items which do not exists on the list of these id's, then in foreach remove all collection items which we do not need, and finally save collection?
Any better approach suggested?
This is the save function from the Doctrine_Collection class
public function save(Doctrine_Connection $conn = null, $processDiff = true)
{
if ($conn == null) {
$conn = $this->_table->getConnection();
}
try {
$conn->beginInternalTransaction();
$conn->transaction->addCollection($this);
if ($processDiff) {
$this->processDiff();
}
foreach ($this->getData() as $key => $record) {
$record->save($conn);
}
$conn->commit();
} catch (Exception $e) {
$conn->rollback();
throw $e;
}
return $this;
}
I'm not sure where you are getting your collection from, or if you are manually building it, but you might want to try extending the Doctrine_Collection class and overloading the save function like this
<?php
class My_Collection extends Doctrine Collection
{
public function save(Doctrine_Connection $conn = null, $processDiff = true, $createOnly = true)
{
if ($conn == null) {
$conn = $this->_table->getConnection();
}
try {
$conn->beginInternalTransaction();
$conn->transaction->addCollection($this);
if ($processDiff) {
$this->processDiff();
}
foreach ($this->getData() as $key => $record) {
if($createOnly)
{
if ($record->exists())
{
$record->save($conn);
}
}else{
$record->save($conn);
}
}
$conn->commit();
} catch (Exception $e) {
$conn->rollback();
throw $e;
}
return $this;
}
}
I don't know anything about Doctrine, but the MySQL REPLACE query does just what you want - updates existing rows & creates new rows if no primary key matches were found.