# Execute cron that creates a new record using Entity

**URL:** <https://discourse.cakephp.org/t/execute-cron-that-creates-a-new-record-using-entity/10827>\
**Category:** Need Help\
**Created:** [December 15, 2022, 5:47pm UTC](https://discourse.cakephp.org/t/execute-cron-that-creates-a-new-record-using-entity/10827 "2022-12-15T17:47:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjvg](https://avatars.discourse-cdn.com/v4/letter/j/7bcc69/32.png) [@jjvg](https://discourse.cakephp.org/u/jjvg)\
**Post date:** [December 15, 2022, 5:47pm UTC](https://discourse.cakephp.org/t/execute-cron-that-creates-a-new-record-using-entity/10827/1 "2022-12-15T17:47:46Z")

</div>

Hi!

The application needs to query an external api every 60 seconds to check if it has new orders. If have new orders, it must create a new record in the orders database.

For this I must create a routine to run using cron from linux.

Where should I put the php file of this routine and how do I access the database of this routine via Entity of cakephp? Is there a sample?

Thanks in advance,  
jjvg

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [December 15, 2022, 6:02pm UTC](https://discourse.cakephp.org/t/execute-cron-that-creates-a-new-record-using-entity/10827/2 "2022-12-15T18:02:42Z")

</div>

Basic CLI tools can be implemented in [Commands](https://book.cakephp.org/4/en/console-commands/commands.html).

But in your case I would personally rather go with a queueing system like [GitHub - dereuromark/cakephp-queue: Queue plugin for CakePHP - simple, pure PHP and without dependencies.](https://github.com/dereuromark/cakephp-queue) or [GitHub - dereuromark/cakephp-queue: Queue plugin for CakePHP - simple, pure PHP and without dependencies.](https://github.com/dereuromark/cakephp-queue)

Because what would you do if

- your API doesn’t respond after 60 seconds/at all?
- responds with such a huge data-set that you can’t process it in 60 seconds.

You don’t want to re-fetch data from the API if you haven’t processed the “old” request.

Regarding “accessing the database”: In CakePHP you should never directly access the database. Otherwise why would you use a MVC framework with an ORM?

What you need is the `LocatorAwareTrait` explained in [Query Builder - 4.x](https://book.cakephp.org/4/en/orm/query-builder.html#the-query-object)  
With that you can get a table instance which behaves the same like you have in your generated controllers from `bin/cake bake`.

So like

```auto
public function execute(Arguments $args, ConsoleIo $io)
{
    $tableInstance = $this->getTableLocator()->get('Categories');
    $query = $tableInstance->find();
}

```

is the same as in your controller doing

```auto
public funtion index()
{
    $query = $this->Categories->find();
}

```

With that table instance you can basically do all CRUD operations which are already present in your controller.

---

<div class="post-metadata">

**Author:** ![jjvg](https://avatars.discourse-cdn.com/v4/letter/j/7bcc69/32.png) [@jjvg](https://discourse.cakephp.org/u/jjvg)\
**Post date:** [December 15, 2022, 8:21pm UTC](https://discourse.cakephp.org/t/execute-cron-that-creates-a-new-record-using-entity/10827/3 "2022-12-15T20:21:07Z")

</div>

Kevin, thanks for your fast answer.

The two queueing system looks pointing to the same link 🙂

Thank you!

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [December 15, 2022, 8:39pm UTC](https://discourse.cakephp.org/t/execute-cron-that-creates-a-new-record-using-entity/10827/4 "2022-12-15T20:39:16Z")

</div>

Right sorry, the second one should have been [GitHub - cakephp/queue: A queue-interop compatible Queueing library](https://github.com/cakephp/queue)

---

<div class="post-metadata">

**Author:** ![mvojwe](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@mvojwe](https://discourse.cakephp.org/u/mvojwe)\
**Post date:** [December 16, 2022, 2:35pm UTC](https://discourse.cakephp.org/t/execute-cron-that-creates-a-new-record-using-entity/10827/5 "2022-12-16T14:35:11Z")

</div>

Which one would you recommend? or does one plugin has some advantages over the other? I’ve read both documentations but since I’ve never implemented something like this, I don’t know which one should I use.

My goal is to read mysql data from several tables, apply some formulas and create records in another table.

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [December 16, 2022, 4:10pm UTC](https://discourse.cakephp.org/t/execute-cron-that-creates-a-new-record-using-entity/10827/6 "2022-12-16T16:10:01Z")

</div>

I personally prefer dereuromark/cakephp-queue but its just because I am used to it.

But before that you should get used to Cakephp and how its ORM works so you know how to “create records in another table”

---

<div class="post-metadata">

**Author:** ![jjvg](https://avatars.discourse-cdn.com/v4/letter/j/7bcc69/32.png) [@jjvg](https://discourse.cakephp.org/u/jjvg)\
**Post date:** [December 17, 2022, 1:43pm UTC](https://discourse.cakephp.org/t/execute-cron-that-creates-a-new-record-using-entity/10827/7 "2022-12-17T13:43:08Z")

</div>

Thank you Kevin!

I’m trying to use dereuromark/cakephp-queue.

- Add cakephp-queue to the project  
`composer require dereuromark/cakephp-queue`

- Load plugin src/Application.php’s bootstrap() using:  
`$this->addPlugin('Queue');`

- Create the tables using the Migrations plugin:  
`bin/cake migrations migrate -p Queue`

- Configuration: create a file called `app_queue.php` inside your `config` folder

```php
return [
    'Queue' => [
        'workermaxruntime' => 60,
        'sleeptime' => 15,
    ],
];

```

- I created a task `pollingOrdersTask.php` that extends Queue\Queue\Task and implements the `run` method, then I placed it in `src/Queue/Task/` folder.

- `bin/cake queue add pollingOrdersTask`  
returns that it can not add pollingOrdersTask task

I’m a bit lost here. 🙂

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [December 17, 2022, 3:05pm UTC](https://discourse.cakephp.org/t/execute-cron-that-creates-a-new-record-using-entity/10827/8 "2022-12-17T15:05:06Z")

</div>

you need to start the queue worker via

```auto
bin/cake queue run

```

this will then process all queued jobs which have not yet been processed.

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [December 17, 2022, 3:29pm UTC](https://discourse.cakephp.org/t/execute-cron-that-creates-a-new-record-using-entity/10827/9 "2022-12-17T15:29:03Z")

</div>

If you want to add tasks via CLI you need to implement the AddInterface which is described here.

> **[cakephp-queue/docs at master · dereuromark/cakephp-queue - Adding jobs from CLI or as click](https://github.com/dereuromark/cakephp-queue/tree/master/docs#adding-jobs-from-cli-or-as-click)**
>
> master/docs

But I usually just call the `$this->QueuedJobs->createJob()` function to add tasks to the queue in controller actions.
