<?php
namespace Phinx\Seed;
use Phinx\Db\Table;
use Phinx\Db\Adapter\AdapterInterface;
use think\console\Input as InputInterface;
use think\console\Output as OutputInterface;
/**
* Abstract Seed Class.
*
* It is expected that the seeds you write extend from this class.
*
* This abstract class proxies the various database methods to your specified
* adapter.
*
* @author Rob Morgan <robbym@gmail.com>
*/
abstract class AbstractSeed implements SeedInterface
{
protected $adapter;
protected $input;
protected $output;
final public function __construct(InputInterface $input = null, OutputInterface $output = null)
{
if (!is_null($input)){
$this->setInput($input);
}
if (!is_null($output)){
$this->setOutput($output);
}
$this->init();
}
protected function init()
{
}
public function run()
{
}
public function setAdapter(AdapterInterface $adapter)
{
$this->adapter = $adapter;
return $this;
}
public function getAdapter()
{
return $this->adapter;
}
public function setInput(InputInterface $input)
{
$this->input = $input;
return $this;
}
public function getInput()
{
return $this->input;
}
public function setOutput(OutputInterface $output)
{
$this->output = $output;
return $this;
}
public function getOutput()
{
return $this->output;
}
public function getName()
{
return get_class($this);
}
public function execute($sql)
{
return $this->getAdapter()->execute($sql);
}
public function query($sql)
{
return $this->getAdapter()->query($sql);
}
public function fetchRow($sql)
{
return $this->getAdapter()->fetchRow($sql);
}
public function fetchAll($sql)
{
return $this->getAdapter()->fetchAll($sql);
}
public function insert($table, $data)
{
if (is_string($table)) {
$table = new Table($table, array(), $this->getAdapter());
}
return $table->insert($data)->save();
}
public function hasTable($tableName)
{
return $this->getAdapter()->hasTable($tableName);
}
public function table($tableName, $options = array())
{
return new Table($tableName, $options, $this->getAdapter());
}
}