Please help me solve the problem. Task: to organize the unloading of reviews from a third-party service that gives reviews in the form of xml.
I solved this problem through the module. I did all the necessary actions to process the received data in the controller. Here is the controller code:
<?php
namespace modules\reviews\controllers;
use Craft;
use craft\web\Controller;
use craft\elements\Entry;
class ReviewsController extends Controller
{
protected array|bool|int $allowAnonymous = true;
public function actionStart() {
$xml="XMLURL";
$xml= simplexml_load_file($xml);
foreach($xml->reviews->review as $review){
$id = $review->attributes()->id;
$text= $review->text;
$entries = Entry::find()->section('reviews')->search('reviewId::'.$id)->all();
if(count($entries) == 0){
$entry = new Entry();
$entry->sectionId = 10;
$entry->enabled = true;
$entry->title = "Review ID".$id;
$entry->setFieldValues([
'reviewId' => $id
'reviewText' => $text,
]);
$success = Craft::$app->elements->saveElement($entry);
if (!$success) {
Craft::error('Couldn’t save the review ID"'.$id.'"', __METHOD__);
}
}
}
return "Ready";
}
}
That's what my code does - it receives a list of reviews from a third-party service, runs through the reviews that are in my database. If the code finds some new review, it adds it to the database.
Checking for a new review will be carried out by the created simple field "reviewId". At least that's how it should work. And it works, but not when it needs to.
In the file routes.php I made CRAFT systems so that my module worked out by the URL of my site: site.com/reviews/update . At the hoster in CRON, I set a timer for processing an HTTP request (1 time per hour should be executed).
And here something strange happens.
If I personally open my URL in the browser, then everything works out as intended! But for some reason CRON reacts strangely to if(count($entries) == 0). A new review is added to the database, but when the CRON task is run again for the condition if(count($entries) == 0) for the same review returns TRUE. As if this new comment was not added to the database in the previous task. But this is not the case, it is added to the database normally. In this case, my database is filled with a bunch of identical comments in the morning. What could be the problem? Help plz