how to check result of doctrine statement - codeigniter

I use this code to remove data from database using doctrine
$user = $em->getReference('model\User', $id);
$em->remove($user);
$em->flush();
how I can check if the $user is deleted or not?
what is the returned data from $em->flush();?

i have used try and catch() blocks to solve this issue
when try excute correctly i have return true and in catch i return False

Related

Laravel transaction not work when try to save 2 new item in Controller

I try to create 2 records in database in 2 tables.The
2nd table has some triggers.
I use transaction to manage 2 query
but when 2nd query is error, 1st query still work and save to database
Please help me, I'm trying to learn Laravel 6.x. I use only one database
public function store(Request $request)
{
DB::beginTransaction();
try{
$product= new Product;
//...//
$product->save(); // This query still work, save to database and doesn't rollback
$ticket = new Ticket;
//...//
$ticket->save(); // This query is error
DB::commit();
}catch (\Illuminate\Database\QueryException $e) {
DB::rollBack();
return back()->with('error',$e->getMessage());
}
}
I'm sure that it catch the exception! The error message is shown but the rollback() not work
In your case you have used DB record to work with the transaction, and Eloquent to work with database actions. As per my understanding you can not mix this. I said from my past experience.
I know Laravel documents are a bit vague on this. But I have got success with below method as it uses the DB actions in the closure. This should work well.
Note : Laravel Version 8.x
DB::transaction(function () {
$product= new Product;
//...//
$product->save(); // This query still work, save to database and doesn't rollback
$ticket = new Ticket;
//...//
$ticket->save(); // This query is error
}

Auth::user() works but Auth::user()->id breaks within a where query in eloquent?

I'm very confused by this!
In a laravel controller:
$user = Auth::user();
return $user;
returns the entire user object
$user = Auth::user()->id;
return $user;
returns the id of $user as expected
however if I put that exact thing into a query such as:
$user = Auth::user();
$query = Model::where('user_id', $user->id)->get();
return $query;
I get an error that I'm trying to get an 'id' property of a non-object!!!
How can this be possible?
I've also checked 100 times that I'm logged in when testing this.
Editing to add that I've also tried:
$query = Model::where('user_id', 1)->get();
and that works fine
edit to show the function:
$user = Auth::id();
$result = Lesson::where('user_id', $user)
->whereNotNull('notes')
->get();
return response()->json($result, 200);
Expected results: A list of objects with a filled in "notes" column.
Actual results: an empty [] and an error saying can't get id of a non-object
Don't use Auth in Models.
You can set a relation between User and your Model and do query like:
Auth::user()->relation()->get();
This error occurs if the Model doesn't contain any raw with that user id. Make sure you have the raw in the Model where you are querying to get data by user id.
You should try this
$user = Auth::user();
$result = Model::where('user_id', $user['id'])->get();
The error occurs because of you passed property of non-object.
Even after error not solve then restart your xampp or wamp.
If possible, upload the screenshot of error.
Your code should work if you are authed, it seems your are not based on the error message. Since you return json, i assume this is an api request. Api-routes uses another auth driver as default in Laravel (token vs. session). Either change the driver/guard or move your route(s) from routes/api.php to routes/web.php.
See docs for auth
You can change the driver in config/auth.php.
try this
$user_id = Auth::user()->id;
$query = Model::where('user_id', $user_id)->get();
return $query;

How to get row in Laravel?

I get one row by $id in Laravel like as:
$user = User::where("id", $id)->get();
After I need to do return $user->first();
Can I use this request more shorty?
You can simply run:
return User::find($id);
or maybe (depending on your needs) even:
return User::findOrFail($id);
you donot have to call ->get() and ->first(), since you are sure that you are going to get only one result by the id column, search like this,
User::find($id);
or
User::where('id',$id)->first()
and you have other alternative methods in the case you want to handle exceptions, ->findOrFail() or ->firstOrFail()
You can use $user = User::findOrFail($id); since the id is unique so you'll either get a User object or Exception which you can catch and respond accordingly.
Since you have only one where clause, the shortest way will be
return User::find($id);

Laravel Eloquent - update() function

Laravel's Eloquent update() function returns a boolean, and not the updated entry. I'm using:
return User::find($id)->update( Input::all() )
And this returns only a boolean. Is there a way I can get the actual row, without running another query?
I think that's the behaviour that you want:
$user = User::find($id)->fill(Input::all());
return ($user->update())?$user:false;
I hope it works fine for you.
Another approach can is to use the laravel tap helper function.
Here is an example to use it for updating and getting the updated model:
$user = User::first();
$updated = tap($user)->update([
"username" => "newUserName123"
]);
tap($user) allows us to chain any method from that model. While at the end, it will return the $user model instead of what update method returned.

Codeigniter: does $this->db->last_query(); execute a query?

Does query execution happen at the get_where() clause of the following codeigniter active record statement?
$this->db->select('*');
$q = $this->db->get_where('Contacts', array('id' => $contact_id));
$sql = $this->db->last_query();
Or does it happens once you call the result_array()?
And is $this->db->last_query(); a reliable way in getting the query string.
The query execution happens on all get methods like
$this->db->get('table_name');
$this->db->get_where('table_name',$array);
While last_query contains the last query which was run
$this->db->last_query();
If you want to get query string without execution you will have to do this.
Go to system/database/DB_active_rec.php Remove public or protected keyword from these functions
public function _compile_select($select_override = FALSE)
public function _reset_select()
Now you can write query and get it in a variable
$this->db->select('trans_id');
$this->db->from('myTable');
$this->db->where('code','B');
$subQuery = $this->db->_compile_select();
Now reset query so if you want to write another query the object will be cleared.
$this->db->_reset_select();
And the thing is done. Cheers!!!
Note : While using this way you must use
$this->db->from('myTable')
instead of
$this->db->get('myTable')
which runs the query.
Take a look at this example
For me save_queries option was turned off so,
$this->db->save_queries = TRUE; //Turn ON save_queries for temporary use.
$str = $this->db->last_query();
echo $str;
Ref: Can't get result from $this->db->last_query(); codeigniter
For Me It Works Perfectly: Source Disclosure : This Source website Belongs to me , i am also sharing solutions on my website ...
public function index()
{
$db = \Config\Database::connect();
$heroesCount = $db->table('products')->countAll();
echo $db->getLastQuery();
exit;
}

Resources