ForkCMS\Bundle\InstallerBundle\Form\Type\DatabaseType::checkDatabaseConnection PHP Метод

checkDatabaseConnection() публичный Метод

Validate if a database connection can be made
public checkDatabaseConnection ( InstallationData $data, Symfony\Component\Validator\ExecutionContextInterface $context )
$data ForkCMS\Bundle\InstallerBundle\Entity\InstallationData The form data
$context Symfony\Component\Validator\ExecutionContextInterface The forms validation context
    public function checkDatabaseConnection(InstallationData $data, ExecutionContextInterface $context)
    {
        try {
            // create instance
            $db = new \SpoonDatabase('mysql', $data->getDbHostname(), $data->getDbUsername(), $data->getDbPassword(), $data->getDbDatabase(), $data->getDbPort());
            // test table
            $table = 'test' . time();
            // attempt to create table
            $db->execute('DROP TABLE IF EXISTS ' . $table);
            $db->execute('CREATE TABLE ' . $table . ' (id int(11) NOT NULL) ENGINE=MyISAM');
            // drop table
            $db->drop($table);
        } catch (\Exception $e) {
            $context->addViolation('Problem with database credentials');
        }
    }