Command not getting paramaters?!

Hi,

I have a jobs website we import jobs to. When doing a Batch Task, we add a Command for “import” which is working absolutely fine.

However, we added a new command for “delete”, but for some reason we keep getting this error, and it makes absolutely no sense as to why… because its built in the same way.

Could not decode message: Cannot create an instance of "Concrete\TLA\JobImport\Logging\Command\JobDeletedCommand" from serialized data because its constructor requires parameter "pageId" to be present.

Below is the line adding it to the Batch - this is working fine, and the page id is there, I can echo it out etc… Also, if I replace this whole line with an ImportCommand, it works fine, so nothing wrong here.

...
$this->batch->add(new JobDeletedCommand($page->getCollectionID()));
...

The simple command. Here you see the constructor has the right paramaters.

<?php
namespace Concrete\TLA\JobImport\Logging\Command;

use Concrete\Core\Foundation\Command\Command;

defined('C5_EXECUTE') or die("Access Denied.");

class JobDeletedCommand extends Command
{
    protected $pageId;

    public function __construct($pageId)
    {
        $this->pageId = $pageId;
    }

    
    public function getPageToDelete()
    {
        return $this->pageId;
    }

}

Next is the Handler, which again, is an empty handler as I commented out logic to check it wasnt causing it.

<?php
namespace Concrete\TLA\JobImport\Logging\Command;

use Concrete\Core\Command\Task\Output\OutputAwareInterface;
use Concrete\Core\Command\Task\Output\OutputAwareTrait;

defined('C5_EXECUTE') or die("Access Denied.");

class JobDeletedCommandHandler implements OutputAwareInterface
{
    use OutputAwareTrait;

    public function __invoke(JobDeletedCommand $command)
    {
       //$pageToDelete = \Page::getByID($command->getPageToDelete());
       //$jobId = $pageToDelete->getAttribute('job_id');
       //$this->output->write(t('Deleting Job ID: %s', $jobId));
       //$pageToDelete->delete();
    }

}

So, why is Concrete telling me I am not passing the constructor to the Command… I CLEARLY am.

As mentioned, my ImportCommand works fine and looks like this:

<?php
namespace Concrete\TLA\JobImport\Logging\Command;

use Concrete\Core\Foundation\Command\Command;

defined('C5_EXECUTE') or die("Access Denied.");

class ImportCommand extends Command
{
    protected $fileID;
    protected $update;
    protected $jobPath;

    public function __construct(array $jobData, bool $update, string $jobPath)
    {
        $this->jobData = $jobData;
        $this->update = $update;
        $this->jobPath = $jobPath;
    }

    public function getJobData(): array
    {
        return $this->jobData;
    }

    public function getJobPath(): string
    {
        return $this->jobPath;
    }

    public function isUpdate(): bool
    {
        return $this->update;
    }

}

Am I missing something obvious? Or is there something weird going on here?

If this is v9, and the constructor is invoked with app->make, you will need to pass arguments as an array of named parameters.

It is v9, the Batch is created as so:

$this->batch = Batch::create();

The ImportCommand I have works like this:

$this->batch->add(new ImportCommand($job,true,$jobPath));

And other Commands work fine that I have done, but for some reason, this JobDeletedCommand just keeps throwing this error, no matter how I rename it. Yet if I swap it with the ImportCommand it works no problem.

I haven’t done much work in this area, so don’t have a definitive answer. Just pointing out a common trap with v8->v9 __construct() when app->create() is involved - the hypothesis is maybe batch::create() covers up a similar trap.

I will give it a go… ImportCommand the first parameter is an array, so maybe thats somehow making it work… but then I’d expect the 2nd and 3rd paramaters to not get seen. Will try passing an array with just 1 value in and see if it somehow then works.

… Turns out no, didnt change anything, it’s still just saying no parameter was passed. I’m wondering if I’ve missed a crucial step somewhere in adding a Command lol… I’ll revisit another time and just leave the deleting in the TaskHandler instead, only moved it to log it really.