Reputation: 35
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
Upvotes: 1
Views: 2200
Reputation: 824
Provide the ResolvedHosts entity:
public function AddResolvedHost($fqdn, $ip)
{
$resolvedhost = $this->ResolvedHosts->newEntity();
$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";
}
}
Upvotes: 1