PHP code example of symbiotic / database

1. Go to this page and download the library: Download symbiotic/database library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

symbiotic / database example snippets


    $config = [
       'default' => 'my_connect_name',
        // Namespace connections
        'namespaces' => [
           '\\Modules\\Articles' => 'mysql_dev',
        ]
       'connections' => [
            'my_connect_name' => [
                'driver' => 'mysql',
                'database' => 'database',
                'username' => 'root',
                'password' => 'toor',
                'charset' => 'utf8mb4',
                'collation' => 'utf8mb4_unicode_ci',
                'prefix' => '',
            ],
            'mysql_dev' => [
             // ....
            ],
        ]
    ];
    
  // Building from an array
  $manager = \Symbiotic\Database\DatabaseManager::fromArray($config);
  
  // Building via constructor
  $manager = new \Symbiotic\Database\DatabaseManager(
            new \Symbiotic\Database\ConnectionsConfig($config['connections'], $config['default']),
            new \Symbiotic\Database\NamespaceConnectionsConfig($config['namespaces']) // необязательно
        );

/**
 * @var \Symbiotic\Database\DatabaseManager $manager 
 */
// Getting all connections
$connections = $manager->getConnections();

// Default Connection
$defaultConnection = $manager->getDefaultConnectionName();

// Checking if a connection config exists
$bool = $manager->hasConnection('my_connect_name');
$bool = isset($manager['my_connect_name']);
 
// Getting connection data
$connectionData = $manager->getConnection('my_connect_name');
$connectionData = $manager['my_connect_name'];

// Retrieving connection data by namespace, if search engine by namespaces is enabled (description below)
$connectionData = $manager->getConnection(\Modules\PagesApplication\Models\Event::class);
 
// Adding a connection
$manager->addConnection(
         [
            'driver' => 'mysql',
            'database' => 'test_db',
            'username' => 'root',
            'password' => 'toor',
            //....
        ],
        'test_connection'
);
$manager['my_connect_name'] = [
//....
];

// Deleting a connection by name
$manager->removeConnection('test_connection');
unset($manager['test_connection']);



/**
 * @var \Symbiotic\Database\DatabaseManager $manager 
 */

// Is the connection search by namespace active?
$bool = $manager->isActiveNamespaceFinder();

// Enable/disable search
$manager->activateNamespaceFinder(false);

// Adding a connection for a module
$manager->addNamespaceConnection('\\Modules\\PagesApplication', 'test_connection');

// Getting the name of the connection by class, if disabled, it will return null
$pagesConnectionName = $manager->getNamespaceConnection(\Modules\PagesApplication\Models\Event::class); // return `test_connection`

// Automatic connection search in the call stack, if disabled, returns null
$connectionData = $manager->findNamespaceConnectionName();



// Configuration part
'default' => 'my_connect_name',
// Packet Connections
'namespaces' => [
   '\\Modules\\Articles' => 'mysql_dev',
]
/**
 * @var \Symbiotic\Database\DatabaseManager $manager 
 */
 // Installed Namespace from config 
namespace  Modules\Articles\Models {

$objectConnectionName = (string)$manager; //  mysql_dev (namespace connection)
$objectConnectionData = $manager->getConnection(__NAMESPACE__); //  mysql_dev config  (namespace connection)
$objectConnectionName = $manager->getNamespaceConnection(__NAMESPACE__); //  mysql_dev  (namespace connection)
$connectionData = $manager->findNamespaceConnectionName(); //  mysql_dev  (namespace connection)

// turn off detection by namespaces
$manager->activateNamespaceFinder(false);

$objectConnectionName = (string)$manager; //  my_connect_name (default)
$objectConnectionData = $manager->getConnection(__NAMESPACE__); //  NULL
$objectConnectionName = $manager->findNamespaceConnectionName();//  NULL
$objectConnectionName = $manager->getNamespaceConnection(__NAMESPACE__); // NULL
// Namespace connection can be requested directly from the config
$objectConnectionName = $manager->getNamespacesConfig()->getNamespaceConnection(__NAMESPACE__); // mysql_dev (namespace connection)

}
// Любой другой неймспейс
namespace  Modules\NewSpace\Models {

$objectConnectionName = (string)$manager; //  my_connect_name  (default)
$objectConnectionData = $manager->getConnection(__NAMESPACE__); //  NULL
$objectConnectionName = $manager->findNamespaceConnectionName();//  NULL
$objectConnectionName = $manager->getNamespaceConnection(__NAMESPACE__); // NULL
}