Phalcon is not able to connect to postgrsql. Here are my settings in config.php
return new \Phalcon\Config(array(
'database' => array(
'adapter' => 'Postgresql',
'host' => 'localhost',
'username' => 'postgres',
'password' => 'root',
'dbname' => 'mydb',
'charset' => 'utf8',
),
'application' => array(
'controllersDir' => __DIR__ . '/../../app/controllers/',
'modelsDir' => __DIR__ . '/../../app/models/',
'viewsDir' => __DIR__ . '/../../app/views/',
'pluginsDir' => __DIR__ . '/../../app/plugins/',
'libraryDir' => __DIR__ . '/../../app/library/',
'cacheDir' => __DIR__ . '/../../app/cache/',
'baseUri' => '/test/',
)
));
Page is blank showing no errors.
DI service implementation
use Phalcon\Db\Adapter\Pdo\Postgresql as DbAdapter;
$di->set('db', function () use ($config) {
return new DbAdapter(array(
'host' => $config->database->host,
'username' => $config->database->username,
'password' => $config->database->password,
'dbname' => $config->database->dbname,
"charset" => $config->database->charset
));
});
There is no port in db connection array you used. Default postgresql port is 5432. so use this in your db connection and try to pass array directly to postgresql adapter constructor.
or
install postgres pdo /php pdo again.
I think that code is fine.
I was working a project where i had to use MySQL and PostGres.I connected my Postgresdb by following-
Below default 'db' connection string i wrote the following code inside services.php-
$di->setShared('pgphalcon', function () {
$config = $this->getConfig();
$adaptar = "PostgreSQL";
$class = 'Phalcon\Db\Adapter\Pdo\\' . $adaptar;
$params = [
'host' => "localhost",
'port' => "5432", // for localhost no need to put this line unless u change the port
'username' => "postgres",
'password' => "12345",
'dbname' => "phalcon",
];
$connection = new $class($params);
return $connection;
});
Then your controller function
do the following-
public function yourcontrollernameAction()
{
$con = $this->di->get('pghalcon');
$post = $con->fetchOne("SELECT * FROM blog ORDER BY id DESC LIMIT 1", Phalcon\Db::FETCH_ASSOC);
$this->view->data= $post;
}
For better Understanding I'd like to suggest visit this url
One thing to clarify- I was failed to do db operation by Model for PostGreSQL. That's why I used this way.
Related
I am trying to connect mysql to zend framework 3 application but it gives such result as below:
Class 'Zend\Db\Adapter\Adapter\Driver\Mysqli' not found
I am using below code:
$config = [
'driver' => 'Mysqli',
'database' => 'abc',
'host' => 'localhost',
// 'port' => 27017,
'username' => 'root',
'password' => '',
];
$adapter = new Mysqli($config);
$result = $adapter->query("select * from
table1",Adapter::QUERY_MODE_EXECUTE);
foreach($result as $row){
echo $row->sentence;"<br>";
}
inserted this as well. use Zend\Db\Adapter\Adapter\Driver\Mysqli;
I am trying to query an additional external database connection in one of my repositories. In LocalConfiguration.php I've defined two connections (Default, External).
[...]
'DB' => [
'Connections' => [
// Local MySQL database
'Default' => [
// ...
],
// External MSSQL database
'External' => [
'charset' => 'utf-8',
'dbname' => 'DBNAME',
'driver' => 'sqlsrv',
'host' => 'someExternalIP',
'password' => 'somePassword',
'port' => 1433,
'user' => 'someUser',
],
],
],
[...]
In my repository I want to query the external database (via Doctrine).
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('dbo.SomeTable');
$queryBuilder->getRestrictions()->removeAll();
$queryBuilder
->select('*')
->from('dbo.SomeTable');
Do I have to explicitly tell the QueryBuilder to use that particular connection? Right now I am getting an Doctrine\DBAL\Exception\ConnectionException error, as the system tries to connect via the Default-Connection.
An exception occurred while executing 'SELECT * FROM `dbo`.`SomeTable`':
SELECT command denied to user 'myLocalUser'#'localhost' for table 'SomeTable'
Check out $GLOBALS['TYPO3_CONF_VARS']['DB']['TableMapping'] where you can explicitly define what tables are located in which database. See also this for some more details https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Database/Configuration/Index.html
The other option is actually to use ask the Connection by name, and create a query builder out of that.
GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionByName('External')->createQueryBuilder(...)
I personally would go with the latter, as it is more explicit within the actual callers code what is used.
To work with external DB, you have to :
configure the mapping with external database and table mapping in LocalConfiguration.php
define the TCA for external tables in myExt/Configuration/TCA/MyExternalTableName.php
configure the external tables/columns mapping in ext_typoscript_setup.txt
and then, the queries in repositories will work.
Sample LocalConfiguration.php :
'DB' => [
'Connections' => [
'Default' => [
'charset' => 'utf8',
'dbname' => 'LOCAL-DB',
'driver' => 'mysqli',
'host' => '127.0.0.1',
'password' => 'PWD',
'port' => 3306,
'user' => 'USER',
],
'externalDb' => [
'charset' => 'utf8',
'dbname' => 'EXTERNAL-DB',
'driver' => 'mysqli',
'host' => 'localhost',
'password' => 'PWD',
'port' => 3306,
'user' => 'USER',
],
],
'TableMapping' => [
'MyexternalTable1' => 'externalDb',
'MyexternalTable2' => 'externalDb',
...
]
]
Sample columns mapping in myExt/ext_typoscript_setup.txt :
plugin.tx_myext {
persistence {
classes {
Vendor\MyExt\Domain\Model\LocalModel {
mapping {
tableName = ExternalTableName
recordType = \Vendor\MyExt\Domain\Model\LocalModel
columns {
col1.mapOnProperty = uid
col2.mapOnProperty = name
...
}
}
}
}
}
}
I can see how to have multiple connections using a configuration file in app/config/database.php and this is well documented.
Is this the only way or can you also define multiple connections using the .env file?
First, you'll need to configure your connections. You need to create a config directory in your project and add the file config/database.php. Like this:
<?php
return [
'default' => 'mysql',
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'homestead',
'username' => 'root',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'mysql2' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'homestead2',
'username' => 'root',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
],
Once you've added your connection configurations, you can access them by getting the database manager object out of the container and calling ->connection('connection_name'). See below for a full example.
<?php
namespace App\Http\Controllers;
use Illuminate\Database\DatabaseManager;
class StatsController extends Controller
{
/**
* #return array
*/
public function getLatest()
{
// Resolve dependencies out of container
/** #var DatabaseManager $db */
$db = app('db');
$database1 = $db->connection('mysql');
$database2 = $db->connection('mysql2');
// Look up 3 newest users and 3 newest blog posts
$threeNewestUsers = $database1->select("SELECT * FROM users ORDER BY created_at DESC LIMIT 3");
$threeLatestPosts = $database2->select("SELECT * FROM blog_posts ORDER BY created_at DESC LIMIT 3");
return [
"new_users" => $threeNewestUsers,
"new_posts" => $threeLatestPosts,
];
}
}
http://andyfleming.com/configuring-multiple-database-connections-in-lumen-without-facades/
So I am debugging some code that someone else wrote and it utilises the cakephp cake email thing. I have never used it before and have never written an email function before either.
When the function executes it outputs cakes standard: "Error: An Internal Error Has Occurred"
as well as this line:
SMTP Error: 535 5.7.8 http://support.google.com/mail/bin/answer.py?answer=14257 h66sm5396348yhb.7 - gsmtp
The code is here:
public function newAppEmail($email_addr, $password) {
$Email = new CakeEmail();
$Email->config('default');
$Email->sender(array('polarontest#gmail.com' => 'Polaron'));
$Email->from(array('polarontest#gmail.com' => 'Polaron'));
$Email->to($email_addr);
$Email->subject('Eligibility Check');
$Email->template('newapp');
$Email->emailFormat('text');
$Email->viewVars(array('name' => $this->request->data['Applicant']['first_name'], 'email' => $this->request->data['Applicant']['email'], 'password' => $password));
$Email->attachments(array(
'Polaron - PL Passport - Info Pack - 2013.pdf' => array(
'file' => APP . 'documents/Email_attachments/Polaron - PL Passport - Info Pack - 2013.pdf',
'mimetype' => 'pdf'),
));
$Email->send();
}
and this is the config file:
<?php
class EmailConfig {
public $default = array(
'transport' => 'Smtp',
'from' => array('email#email.com' => 'company name'),
'sender' => array('email#email.com' => 'company name'),
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'email#email.com',
'password' => 'password');
public $fast = array(
'transport' => 'Smtp',
'from' => array('email#email.com' => 'Test Mail name sender'),
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'email#email.com',
'password' => 'password');
}
Can anyone shed some light on what might be wrong and where I should look to fix it?
Well, SMTP Error 535 means that authentication fails, which is easy to find out.
The exception is thrown because of that. So get the right credentials and try again, this is not an issue of the php code but your credentials.
If your login / password is correct, test the configuration:
public $smtp = array(
'transport' => 'Smtp',
'from' => array('email#gmail.com' => 'Name'),
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'timeout' => 30,
'username' => 'email#gmail.com',
'password' => '**********',
'client' => null,
'log' => false,
'charset' => 'utf-8',
'headerCharset' => 'utf-8',
);
zend framework 1.x
how to receive zend db_adapter and connect to mysql having dsn like
mysql://john:pass#localhost:3306/my_db
previously was always using this way:
$connectParams = array('dbname' => MY_DB,
'password' => MY_PASS,
'username' => MY_USER,
'host' => MY_HOST,
'slave' => array(),
'maxQueryAllowedTime' => 500,
'logQueries' => false);
return new \Db_Adapter_Pdo_Mysqlreplicator($connectParams);
sure i can parse dsn and use usual way, just curious if i can use DSN directly, coz PDO can use it but i cant find how to use it through Zend Db_Adapter
If you are using ZendFramework 1.x ->Try this:-
$config = array('dbname' => 'yourDbName',
'password' => 'yourpassword',
'username' => 'root',
'host' => 'localhost',
'slave' => array(),
'maxQueryAllowedTime' => 500,
'logQueries' => false);
$adapter = new Zend_Db_Adapter_Mysqli($config);
if ($config) {
echo ' connected';
}