CakePHP: newly saved record invisible to find() - php

My controller needs to create/save one or more records by looping over the request data it receives and creating corresponding records. You may be wondering: why not just use saveAll() and save them all at once. The short answer is that certain records need to reference the ID of other records created in the same loop (and those IDs don't exist yet).
My loop creates the first record successfully, subsequent iterations of the loop are unable to "see" that newly created record when I use find(). If I echo the returned array, results are there, but the newly created one is missing. Why? Is CakePHP's magic making the new record unavailable due to some sort of caching?
Here is my code that doesn't include the newest record:
$newest_parent_question = $this->Question->find('first', array(
'conditions'=>array('Question.perm_id'=>$parent_question['Question']['perm_id']),
'order' => array('Question.created DESC')
)
);
However, the new record IS returned with this:
$newest_parent_question = $this->Question->find('all');

There's something wrong with your query.
Likely the $parent_question['Question']['perm_id'] is wrong. There's nothing CakePHP will do that would make your record un-find-able.
Just debug your variables to make sure you're building the correct query with the correct id, and you'll be good to go.
(without seeing the actual way you're building the id, it's impossible to help beyond that)

Related

How to get latest record in Laravel 9 [duplicate]

I would like to retrieve the last file inserted into my table. I know that the method first() exists and provides you with the first file in the table but I don't know how to get the last insert.
You'll need to order by the same field you're ordering by now, but descending.
As an example, if you have a time stamp when the upload was done called upload_time, you'd do something like this;
For Pre-Laravel 4
return DB::table('files')->order_by('upload_time', 'desc')->first();
For Laravel 4 and onwards
return DB::table('files')->orderBy('upload_time', 'desc')->first();
For Laravel 5.7 and onwards
return DB::table('files')->latest('upload_time')->first();
This will order the rows in the files table by upload time, descending order, and take the first one. This will be the latest uploaded file.
Use the latest scope provided by Laravel out of the box.
Model::latest()->first();
That way you're not retrieving all the records. A nicer shortcut to orderBy.
You never mentioned whether you are using Eloquent, Laravel's default ORM or not. In case you are, let's say you want to get the latest entry of a User table, by created_at, you probably could do as follow:
User::orderBy('created_at', 'desc')->first();
First it orders users by created_at field, descendingly, and then it takes the first record of the result.
That will return you an instance of the User object, not a collection. Of course, to make use of this alternative, you got to have a User model, extending Eloquent class. This may sound a bit confusing, but it's really easy to get started and ORM can be really helpful.
For more information, check out the official documentation which is pretty rich and well detailed.
To get last record details
Model::all()->last(); or
Model::orderBy('id', 'desc')->first();
To get last record id
Model::all()->last()->id; or
Model::orderBy('id', 'desc')->first()->id;
Many answers and some where I don't quite agree. So I will summarise again with my comments.
In case you have just created a new object.
By default, when you create a new object, Laravel returns the new object.
$lastCreatedModel = $model->create($dataArray);
dd($lastCreatedModel); // will output the new output
echo $lastCreatedModel->key; // will output the value from the last created Object
Then there is the approach to combine the methods all() with (last()and first()) without a condition.
Very bad! Don't do that!
Model::get()->last();` // the most recent entry
Model::all()->last();` // the most recent entry
Model::get()->first();` // the oldest entry
Model::all()->first();` // the oldest entry
Which is basically the wrong approach! You get() all() the records, and in some cases that can be 200,000 or more, and then pick out just one row. Not good! Imagine your site is getting traffic from Facebook and then a query like that. In one month that would probably mean the CO² emissions of a city like Paris in a year. Because the servers have to work unnecessarily hard. So forget this approach and if you find it in your code, replace it/rewrite it. Maybe you don't notice it with 100 data sets but with 1000 and more it can be noticeable.
Very good would be:
Model::orderBy('id', 'desc')->last(); // the most recent record
Model::latest('id')->first(); // the most recent record
Model::latest('id')->limit(1)->get(); // the most recent record
Model::orderBy('id', 'desc')->limit(1)->get(); // the most recent entry
Model::orderBy('id', 'desc')->first(); // the most recent entry
Model::orderBy('id', 'asc')->first(); // the oldest entry
Model::orderBy('id', 'asc')->limit(1)->get(); // the oldest entry
Model::orderBy('id', 'asc')->first(); // the oldest entry
If orderBy is used in this context, the primarykey should always be used as a basis and not create_at.
Laravel collections has method last
Model::all() -> last(); // last element
Model::all() -> last() -> pluck('name'); // extract value from name field.
This is the best way to do it.
You can use the latest scope provided by Laravel with the field you would like to filter, let's say it'll be ordered by ID, then:
Model::latest('id')->first();
So in this way, you can avoid ordering by created_at field by default at Laravel.
Try this :
Model::latest()->get();
Don't use Model::latest()->first(); because if your collection has multiple rows created at the same timestamp (this will happen when you use database transaction DB::beginTransaction(); and DB::commit()) then the first row of the collection will be returned and obviously this will not be the last row.
Suppose row with id 11, 12, 13 are created using transaction then all of them will have the same timestamp so what you will get by Model::latest()->first(); is the row with id: 11.
To get the last record details, use the code below:
Model::where('field', 'value')->get()->last()
Another fancy way to do it in Laravel 6.x (Unsure but must work for 5.x aswell) :
DB::table('your_table')->get()->last();
You can access fields too :
DB::table('your_table')->get()->last()->id;
Honestly this was SO frustrating I almost had to go through the entire collection of answers here to find out that most of them weren't doing what I wanted. In fact I only wanted to display to the browser the following:
The last row ever created on my table
Just 1 resource
I wasn't looking to ordering a set of resources and order that list through in a descending fashion, the below line of code was what worked for me on a Laravel 8 project.
Model::latest()->limit(1)->get();
Use Model::where('user_id', $user_id)->latest()->get()->first();
it will return only one record, if not find, it will return null.
Hope this will help.
Model($where)->get()->last()->id
For laravel 8:
Model::orderBy('id', 'desc')->withTrashed()->take(1)->first()->id
The resulting sql query:
Model::orderBy('id', 'desc')->withTrashed()->take(1)->toSql()
select * from "timetables" order by "id" desc limit 1
If you are looking for the actual row that you just inserted with Laravel 3 and 4 when you perform a save or create action on a new model like:
$user->save();
-or-
$user = User::create(array('email' => 'example#gmail.com'));
then the inserted model instance will be returned and can be used for further action such as redirecting to the profile page of the user just created.
Looking for the last inserted record works on low volume system will work almost all of the time but if you ever have to inserts go in at the same time you can end up querying to find the wrong record. This can really become a problem in a transactional system where multiple tables need updated.
Somehow all the above doesn't seem to work for me in laravel 5.3,
so i solved my own problem using:
Model::where('user_id', '=', $user_id)->orderBy('created_at', 'desc')->get();
hope am able to bail someone out.
be aware that last(), latest() are not deterministic if you are looking for a sequential or event/ordered record. The last/recent records can have the exact same created_at timestamp, and which you get back is not deterministic. So do orderBy(id|foo)->first(). Other ideas/suggestions on how to be deterministic are welcome.
You just need to retrive data and reverse them you will get your desire record let i explain code for laravel 9
return DB::table('files')->orderBy('upload_time', 'desc')->first();
and if you want no. of x last result
return DB::table('files')->orderBy('upload_time', 'desc')->limit(x)->get();
If the table has date field, this(User::orderBy('created_at', 'desc')->first();) is the best solution, I think.
But there is no date field, Model ::orderBy('id', 'desc')->first()->id; is the best solution, I am sure.
you can use this functions using eloquent :
Model::latest()->take(1)->get();
With pdo we can get the last inserted id in the docs
PDO lastInserted
Process
use Illuminate\Support\Facades\DB;
// ...
$pdo = DB::getPdo();
$id = $pdo->lastInsertId();
echo $id;

UpdateExistingPivot for multiple ids in loop = n+1 problem. Is there another way?

There's another very similar issue with an answer that was very helpful (and I'm currently using) but causes the n+1 query problem.
I'll outline my use case. Polymorphic many to many relationship
I have:
Location model
Vendor model
User Model
Contactable (custom pivot model - still fleshing this out)
Users can be marked as contacts (aka contactables) for both Locations and Vendors.
I need to not detach contactables when dissociating them (I need a record of the fact that a User was once a contact for a location or vendor) so I don't want to detach them, I need to mark them inactive.
I'll limit the scope of this scenario to the following fields in the contactables table:
active
user_ID
contactable_type
contactable_ID
So I'm executing:
$collectionOfLocationIds = $contactDetails->locations()->getRelatedIds(); //changed to 'allRelatedIds()' in 5.4+
foreach ($collectionOfLocationIds as $locationID)
{
$contactDetails->locations()->updateExistingPivot($locationID, ['active' => 0]);
}
This runs great for most of my vendors, but some have 5k+ locations, so then I'm executing 5k+ update operations for what should really be one query. DB lives on a different server, so a few extra milliseconds add up pretty quickly...
I tried passing an array of ids to the updateExistingPivot function (it says it will take a mixed type for the id parameter) it doesn't produce an error, but it only seems to update the first id in the array. I'm not sure if this is a new bug, #Wallace Maxters mentioned that he could pass an array in 4.2, and I am still working in 5.3, but I'm wondering if anyone else has had this problem.
(Updated for clarity)
Use raw query instead of relationship.
I don't exactly understand which rows you want to inactive.
So lets think you want to inactive contactable for a particualar user.
If its not, change it to whatever where().
DB::table('contactable')->where('user_id', $user_id)
->update(['active' => 0]);
this will only execute one query.

Optimal way to detect fields to delete in database comparing to an array of IDs

I am trying to do the following.
I am consulting an external database using a web service. What the web service does is bring me all the products from an ERP system my client uses. As the server and the connection are not really fast, what I decided to do is basically synchronize the database on my web server and handle most operations there, so that the website can run smoothly.
Everything works fine I just need one last step to guarantee that the inventory on the website matches the one available on the ERP. The only issue comes when they (the client) deletes something on the ERP system.
At the moment I am thinking what would be the ideal strategy (least resource and time consuming) to remove products from my Products table if I don't receive them in the web service result.
So I basically have the following process:
I query the web service for all the products, give them a little format and store them in an array. The final size is about 600 indexes.
Then what I do is I do a foreach cycle and have the following subprocess.
I query my database to check if product_id is present.
If the product is present, I just update it with the latest info, stock data.
If the product is not present, I just insert it.
So, I was thinking of doing the following, but I do not think it's the ideal way:
Do a SELECT * FROM Products and generate an array that has all the products.
Do a foreach cycle in the resulting array and in each cycle scan the ERP array to check if the specific product exists. If not I delete it, if yes, I continue with the next product.
Now considering that after all the previous steps this would involve a couple of nested foreach I am a little worried that it might consume too much memory and also take longer to process.
I was thinking that maybe something like array_diff or array map could solve the issue, but I am not really experienced with these functions, and the structure of the two arrays differs a lot, so I am not sure if it would work that easily.
What would you guys recommend?
It's actually quite simple:
SELECT id FROM Products
Then you have an array of your product Ids, for example:
[123,5679,345]
Then as you go and do your updates or inserts, remove the id from the array.
[for updates]I query my database to check if product_id is present.
This is redundant now.
There are a few ways to remove the value from the array (when you do an update), this is the way I would probably do it.
if(false !== ($index = array_search($data['product_id'],$myids))){
//note the !== type comparison because array_search can return 0 for the first index, we must check for boolean false.
//find the index of the product id in our list of id's from local DB
unset($myids[$index]);
//If our incoming product_id is in the local list we Do Update
}else{
//Otherwise we Do Insert
}
As I mentioned above when doing your updates/inserts, You no longer have to check if the ID exists, because you already know this by having an array of IDs from the database. This alone saves you (n) queries (apx 600).
Then its very simple if you have ids left over.
//I wouldn't normally concatenate variables into SQL, in this case it's a list of int IDs from the database.
//you can of course come up with a loop to make it a prepared statement if you wish, but for the sake of simplistically, I'll leave that as an exercise for another day..
'DELETE FROM Products WHERE id IN('.implode(',', $myids).')'
And because you unset these when Updating, then the only thing left is Products that no longer exist.
Conclusion:
You have no choice (other then doing on duplicate key query, or ignoring exceptions) then to pull out the product Ids. You're already doing this on a row by row basis. So we can effectively kill 2 birds with one stone.
If you need more data then just the ID, for example you check that the product was changed before doing an update. Then pull that data out, but I would recommend using PDO and the FETCH_GROUP option. I wont go into the specifics of that but to say it lets you easily build your array this way:
[{product_id} => [ {product_name}, {product_price} etc..]];
Basically the product_id, is the key with a nested array of the row data, this will make lookup easier.
This way you can look it up like this.
//then instead of array_search
//if(false !== ($index = array_search($data['product_id'],$myids))){
if(isset($myids[$data['product_id']])){
unset($myids[$data['product_id']]);
//do your checks, then your update
}else{
//do inserts
}
References:
http://php.net/manual/en/function.array-search.php
array_search — Searches the array for a given value and returns the first corresponding key if successful
WARNING This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
UPDATE
There is one other really good way to do this, and that is to add a field called sync_date, now when you do your insert or update then set the sync_date to the current data.
This way when you are done, those products with an older sync date then today can be deleted. In this case it's best to cache the time when doing it so you know the exact time.
$time = data('Y-m-d H:i:s'); //or time() if you prefer timestamp
//use this same variable for the whole coarse of the script.
Then you can do
'DELETE from products WHERE sync_time != $time'
This may actually be a bit better because it has more utility. When was the last time it was ran, Now you know.

Doctrine 1.2: events with out-of-date invokers cause problem with versionable behaviour

I have a feeling there might be something I am missing here, but here I go anyway.
Consider this: I have a 'Booking' class that has a user_id field and the Versionable behaviour and I run the following code:
$booking = new Booking();
$booking->user_id = 1;
$booking->isValid();
$booking->user_id = 2;
$booking->save();
This results in the correct record being inserted into the 'booking' table. BUT the record that is inserted into 'booking_version' table is out of date! The user_id is set to 1 because the data is pulled off the event invoker that was created during the first isValid() call. Furthermore, the id field is set to 0 for the same reason (which means the version record cannot be linked back to the booking)
I can get around this problem by calling $booking->clearInvokedSaveHooks() before the save(), but I dont really want to do this because I dont want to run all my save hooks again on saving.
Is there a better way to get around this?
There is nothing to workaround here. Versionable behaviour is designed to store previous values of the record.
If you want to store every single temporary state of the object - you'll have to save it each time.

Get the last insert id

Hello I am using cakePHP 1.3 and I am unable to retreive the last inserted row's id. I actually am using $this->Model->id to retreive the last inserted id but I am unable to get the id. When tried to check what is return type, it says as bool(false), which means nothing is returned.
Here I am loading a different model in a different controller, so would that be the issue?? But even though I am loading, I get back nothing!!
$this->loadModel('Contact');
$this->Contact->query("insert into contacts(tblContact_firstName,tblContact_lastName,tblContact_company,tblContact_department,tblContact_address,tblContact_country,tblContact_city,tblContact_state,tblContact_zipcode,tblContact_phone1,tblContact_email1) values('$sanitizedFormData[fname]','$sanitizedFormData[lname]','','$sanitizedFormData[company]','$sanitizedFormData[address]','$sanitizedFormData[country]','$sanitizedFormData[city]','$sanitizedFormData[state]','$sanitizedFormData[zip]','$sanitizedFormData[phone]','$sanitizedFormData[email]');");
$this->loadModel('Contact');
$contactId = $this->Contact->id;
And when I printed the $this->Contact array recursively, I found the value of "id" key empty. So that explains why I was receiving an empty value.
Now given my situation, how would I get the last inserted id, specific to the controller Contact?
I think you just want to do:
$this->getLastInsertID();
http://book.cakephp.org/2.0/en/models/additional-methods-and-properties.html#model-getlastinsertid
When you use query() you loose a lot of automagic cakephp provides. Use save() instead.
In fact, you even do not need to load Contact in this case. You can execute any query from the current controller with query() even saving to any other table.
You can also avoid using loadModel() if your current model is somehow associated with Contact ($this->CurrentModel->AnotherOne->Contact->save(...)).
If this is MySQl you could use "SELECT from contacts LAST_INSERT_ID()" query to get last ID.
or just "SELECT LAST_INSERT_ID()"
For MSSQL it is "SELECT ##IDENTITY".
This bypasses any solution in cakePHP though, so there might be a better solution.
You can get last inserted record id by
echo $this->ModelName->getLastInsertID();
Alternately, you can use:
echo $this->ModelName->getInsertID();
This methods can be found in cake/libs/model/model.php on line 2775
Note: This function doesn't work if you run the insert query manually

Categories