saving data from Shell with cakephp 3

What I missing to be able to save data in DB from CakePHP Shell ? Is CakePHP 3 Shell able to save data in DB ? I don’t see any documentation about this.

<?php
Namespace App\Shell;

use Cake\Console\Shell;
use Cake\Core\Configure;

// DON'T IF I NEED IT
use Cake\ORM\TableRegistry;
use Cake\ORM\Entity;
use Cake\Datasource\EntityInterface;

class CacheDnsResolverShell extends Shell
{
    public function initialize()
    {
        parent::initialize();
        $this->loadModel('ResolvedHosts');
    }

    public function AddResolvedHost($fqdn, $ip)
    {
        $data = array(
            'fqdn' => $fqdn,
            'ip' => $ip,
            'timestamp' => time()
            );

        // I TRIED THIS, IT DOES NOT WORK
        //$this->ResolvedHosts->save($data);

        // I TRIED ALSO THIS, IT DOES NOT WORK TOO
        $resolvedhost = $this->ResolvedHosts->patchEntity('resolvedhost', $data);

        if ($this->ResolvedHosts->save($resolvedhost)) {
                echo  "$fqdn ($ip) has been added successfully";
        }
    }
}

When running :

./bin/cake cache_dns_resolver AddResolvedHost toto 1.1.1.1

I get for the first try :

Warning Error: Argument 1 passed to Cake\ORM\Table::save() must be an instance of Cake\Datasource\EntityInterface, array given, called in

and the second try

Warning Error: Argument 1 passed to Cake\ORM\Table::patchEntity() must be an instance of Cake\Datasource\EntityInterface,
Warning Error: Argument 1 passed to Cake\ORM\Marshaller::merge() must be an instance of Cake\Datasource\EntityInterface

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s