For each offer in the array, if "inDB" is true, I retrieve the the corresponding tag from the DB.
This is where the problem occurs:
$tag = $this->tagRepo->find($tagData['id']);
Although I've made sure the $tagData['id'] value is different on each iteration, this line always returns
me the same record (the first one it retrieved when processing the first iteration in the loop).
But if I change the "find" method in my TagRepository from this:
it works perfectly (even though it might create some effects somewhere ?)
If I directly call the model it works just fine but defeats the purpose of a repository.
$tag = Tag::find($tagData['id']);
Here is the (simplified) code I have in a Job class.
public function __construct($offersToInsert)
{
$this->offersToInsert = $offersToInsert;
}
public function handle(TagRepository $tagRepo)
{
$this->tagRepo = $tagRepo;
$insertedOffers = [];
foreach ($this->offersToInsert as $key => $offer) {
$tagsToBeAttached = [];
foreach ($offer['tags'] as $key => $tagData) {
if($tagData['inDB'] === 0) {
$existingTag = $this->tagRepo
->findWhere([
'title_fr' => $tagData['title'],
'title_en' => $tagData['title']
],
['id', 'counter'],
true
)
->all();
if($existingTag) {
$existingTag[0]->increment('counter');
$tagsToBeAttached[] = $existingTag[0]->id;
}
else {
$newTag = [
'tag_type_id' => 1,
'title_en' => $tagData['title'],
'title_fr' => $tagData['title'],
'counter' => 1,
];
$newTag = $this->tagRepo->create($newTag);
$tagsToBeAttached[] = $newTag->id;
}
}
else {
// Works if I remove the "newQuery()" call inside the "find" method of the repo.
$tag = $this->tagRepo->find($tagData['id']);
// Works just fine.
//$tag = Tag::find($tagData['id']);
}
}
}
// Return whatever...
}
I'm not really familiar with the purpose of the newQuery method, but does someone see something wrong in my code ? Can I safely remove the newQuery() calls everywhere ?
Hi,
I'm facing a weird problem when fetching records from the DB using the find or findWhere methods.
$offersToInsert is an array of objects sent via AJAX.
For each offer in the array, if "inDB" is true, I retrieve the the corresponding tag from the DB. This is where the problem occurs:
Although I've made sure the $tagData['id'] value is different on each iteration, this line always returns me the same record (the first one it retrieved when processing the first iteration in the loop).
But if I change the "find" method in my TagRepository from this:
to this (by removing the newQuery():
it works perfectly (even though it might create some effects somewhere ?)
If I directly call the model it works just fine but defeats the purpose of a repository.
Here is the (simplified) code I have in a Job class.
I'm not really familiar with the purpose of the newQuery method, but does someone see something wrong in my code ? Can I safely remove the newQuery() calls everywhere ?
Thanks a lot for your precious time !