what i need
i need to connect oracle database with symfony2.
i have checked by php -m
oci8
pdo_odbc
odbc
here is the link i follow https://gist.github.com/johnkary/6481664
a.)config.yml
Doctrine Configuration
doctrine:
dbal:
default_connection: default
connections:
default:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: "%database_charset%"
b.) parameter.yml
# If connecting via SID
parameters:
database_driver: oci8
database_host: abc
database_port: '1521'
database_name: test
database_user: aa
database_password: aa
database_charset: AL32UTF8
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
locale: en
secret: zzzz
c.)services.orcale.yml
services:
acme.doctrine.dbal.events.oracle_session_init.listener:
class: %doctrine.dbal.events.oracle_session_init.class%
tags:
- { name: doctrine.event_listener, event: postConnect }
acme.doctrine.dbal.oracle_platform.type_mapping.listener:
class: Acme\MisBundle\Listener\OracleDoctrineTypeMappingListener
tags:
- { name: doctrine.event_listener, event: postConnect }
then run symfony database cmd
php bin/console doctrine:database:create
error:
cannot create database test for connection named default.
notice: undefined index dbname
i have googled a day but i think there are few developer who works with symfony2 and oracle.
i had made simple php script that works to test connections
$userName = ""; $password = ""; $dtabasePort = "1521"; $serverName = "";
$databaseName = "testingdb";
$c = oci_connect($userName, $password, '(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = host)(PORT = 1521)) (CONNECT_DATA = (SERVICE_NAME = ) (SID =)))');
print_r($c);
output
Resource id #3
tsnames.ora
test=
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = sss)(PORT = 1521))
(CONNECT_DATA = (SID = test))
)
i have worked sf3 and mysql but im new to oracle thats why i don"t knew how to connect oracle db with symfony.
please give some solution where i have done wrong.
Can anyone suggest in steps how to connect oracle db with sf2/3 is most appreciated.
link of github relies same issue that im facing https://github.com/doctrine/dbal/issues/1564
i have also changed the parameter.yml file
parameters:
database_driver: oci8
database_user: <user>
database_password: <password>
database_charset: WE8MSWIN1252
database_name: (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=<host>)(PORT=<port>))(CONNECT_DATA=(SERVICE_NAME=<service_name>)))
then error you have requested non-existence parameter "database_host"
last & final solution i tried but didn"t worked i think there bug in sf2 for oracle
parameters:
database_driver: oci8
database_host:
database_port: 1521
database_name:
database_user:
database_password:
domain_name:
doctrine:
dbal:
default_connection: default
connections:
default:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
persistent: true
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
services:
pdo:
class: PDO
arguments:
- "oci8:Server=%database_host%;Database=%database_name%"
- "%database_user%"
- "%database_password%"
calls:
- [setAttribute, [3, 2]] # \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION
session.handler.pdo:
class: ESERV\MAIN\FrameworkChanges\MtlPdoSessionHandler
arguments: ["#pdo", "%pdo.db_options%"]
here is what I use to connect MSSQL : https://github.com/realestateconz/MssqlBundle
config.yml :
doctrine:
dbal:
types:
string: ***\BacsManagementBundle\Type\StringType
default_connection: default
connections:
default:
host: "%database_host%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
driver_class: Realestate\MssqlBundle\Driver\PDODblib\Driver
mapping_types:
enum: string
string: string
hope it helps.
This might be a little old but I think it is still useful. I am working on a Symfony 5 project connected to a legacy Oracle 11G database. For the most part Doctrine works fine. In some cases you need to perform a raw query.
Your env file:
DATABASE_URL2="oci8://USERNAME:PASSWORD#HOSTNAME:1521/DATABASENAME"
Note: if you are using Oracle 11G Express Edition your database name most likely will be XE
Next update your doctrine.yaml file. In my case I have two databases. The default one is MySQL and the 2nd database is Oracle 11G. It would also be a good idea to install the DoctrineExtensions package that gives a little more support to Oracle and MySQL in Doctrine.
doctrine:
dbal:
default_connection: default
connections:
default:
# configure these for your database server
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
mapping_types:
enum: string
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
oracle:
# configure these for your database server
url: '%env(resolve:DATABASE_URL2)%'
driver: 'oci8'
server_version: '11'
charset: AL32UTF8
orm:
auto_generate_proxy_classes: '%kernel.debug%'
default_entity_manager: default
entity_managers:
default:
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
connection: default
auto_mapping: true
dql:
numeric_functions:
rand: DoctrineExtensions\Query\Mysql\Rand
datetime_functions:
DATE_FORMAT: DoctrineExtensions\Query\Mysql\DateFormat
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
oracle:
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
connection: oracle
dql:
datetime_functions:
TO_CHAR: DoctrineExtensions\Query\Oracle\ToChar
TO_DATE: DoctrineExtensions\Query\Oracle\ToDate
mappings:
Oracle:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Oracle'
prefix: 'App\Entity\Oracle'
alias: Oracle
Now this is where it gets tricky. My database does not have any primary keys so I had to add them manually to each table in order to import the entities. Once I imported the entities I then removed the primary keys and updated the entities manually.
php bin/console doctrine:mapping:import "App\Entity\Oracle" annotation --path=src/Entity/Oracle --em=oracle
You should now be able to use Doctrine to perform a query. I like using repositories so I do not use Query Builder.
Example:
public function getEmployeeInfo($clientID)
{
$sql =
"
SELECT
p.employeeId,
p.clientId,
p.firstname,
p.lastname
FROM
Oracle:Phoneext p
WHERE
p.clientId = :clientID
ORDER BY p.lastname ASC
"
;
$query = $this->getEntityManager()->createQuery($sql);
$query->setParameter('clientID', $clientID);
$results = $query->getResult();
return ($results);
}
In a case you need to perform a RAW query:
public function getMultiStatusRowId($employeeID, $status, $date, $time, $em)
{
$conn = $em->getConnection();
$sql =
"
SELECT
ROWIDTOCHAR(ROWID) as row_id
FROM
MULTI_STATUS
WHERE
EMPLOYEE_ID = ?
AND STATUS = ?
AND IN_DATE = ?
AND IN_TIME = ?
ORDER BY ORDER_NUM ASC
"
;
$result = $conn->prepare($sql);
$result->bindValue(1, $employeeID);
$result->bindValue(2, $status);
$result->bindValue(3, $date);
$result->bindValue(4, $time);
$result->execute();
$data = array();
$i = "0";
while ($row = $result->fetch()) {
$data[$i]['ROW_ID'] = $row['ROW_ID'];
$i++;
}
return $data;
}
One final item if you need help setting up a local system on a Mac you can use this to help setup Lando. Note I had to setup Oracle 11G on AWS EC2 then was able to use the IMP tool to import my dump files.
If you setup RDS Amazon will default to Oracle 20 or some newer version. AWS RDS does not support the older backups from IMP. RDS will only support data pump using S3.
https://github.com/rsaylor73/lando-apache-php-mysql-oci8
Related
I'm trying to connect to my aws rds database with symfony. But I constantly get an error: An exception occurred in driver: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.". My security groups are open to all ports and here are my settings.
//services.yaml
parameters:
database_driver: pdo_mysql
database_host: '%env(RDS_HOSTNAME)%'
database_port: '%env(RDS_PORT)%'
database_name: '%env(RDS_DB_NAME)%'
database_user: '%env(RDS_USERNAME)%'
database_password: '%env(RDS_PASSWORD)%'
//doctrine.yaml
doctrine:
dbal:
# if the url option is specified, it will override the above config
default_connection: default
connections:
default:
dbname: '%env(RDS_DB_NAME)%'
user: '%env(RDS_USERNAME)%'
password: '%env(RDS_PASSWORD)%'
host: '%env(RDS_HOSTNAME)%'
driver: 'pdo_mysql'
port: 3306
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
In .env you are the correct variables.
If someone has a fix, please provide it to me :)
I found the problem.
Currently, I'm a student and living in a student dorm. My dorm network is blocking the connection with an outside MySql server so that's why I repeatedly get this error.
The solution would be to use a non-public source of the internet or try to share internet from some other device. You could also use a VPN (make sure to test first because with some of them the same error ocurrs).
A symfony noob here. Been trying since morning to map entity/s of the database using Doctrine via console throws up no primary key error. The mysql server is a remote one which I unfortunately only have read access and I am not able to create a primary key. I did see a few questions on SO with the exact same issue but they were unanswered and old.
I tried https://medium.com/#joaoneto/solves-doctrine-orm-error-with-tables-without-primary-key-on-mysql-when-mapping-the-database-1ce740610b51
but again it throws up error regarding empty columns.
Call to a member function getColumns() on null
My doctrine.yaml. Obviously I altered the connection details.
doctrine:
dbal:
default_connection: default
connections:
# configure these for your database server
default:
driver: 'pdo_mysql'
host: 'localhost'
port: '3306'
dbname: 'symfony_test_db'
user: 'root'
password: ''
charset: utf8mb4
customer:
driver: 'pdo_mysql'
host: 'xxx.xxx.xx.xxx'
port: '3306'
dbname: 'sg3_symfony_db'
user: 'sguser'
password: 'password'
charset: UTF8
backoffice:
driver: 'pdo_mysql'
host: 'localhost'
port: '3306'
dbname: 'back_office'
user: 'backoffice_user'
password: 'password'
charset: UTF8
one:
driver: 'pdo_mysql'
host: 'xxx.xxx.xx.xxx'
port: '3306'
dbname: 'one_db'
user: 'one_user'
password: 'password'
charset: UTF8
staging:
driver: 'pdo_mysql'
host: 'xxx.xxx.xx.xxx'
port: '3306'
dbname: 'staging'
user: 'staginguser'
password: 'password'
charset: UTF8
# With Symfony 3.3, remove the `resolve:` prefix
#url: '%env(resolve:DATABASE_URL)%'
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
#auto_mapping: true
mappings:
Main:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Main'
prefix: 'App\Entity\Main'
alias: Main
customer:
connection: customer
mappings:
Customer:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Customer'
prefix: 'App\Entity\Customer'
alias: Customer
backoffice:
connection: backoffice
mappings:
Backoffice:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Backoffice'
prefix: 'App\Entity\Backoffice'
alias: Backoffice
one:
connection: mt4
mappings:
One:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/One'
prefix: 'App\Entity\One'
alias: One
staging:
connection: staging
mappings:
staging:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Staging'
prefix: 'App\Entity\Staging'
alias: Staging
CLI command I use to map but fails.
php bin/console doctrine:mapping:convert --from-database annotation --force --em=one ./src/Entity/One/ --verbose
In this case, sometimes you need to walk around.
This error:
Table ____ has no primary key. Doctrine does not support reverse
engineering from tables that don’t have a primary key.
So.
Doctrine can not work on tables that have no primary key.
In MySQL, create tables without PK will always be a bad idea, but, in some cases (or legacy systems) that not have PKs on some tables, you still can use Doctrine as ORM.
However, by default (and I believe this will not change) if your database has tables without primary key Mapping simply will not work.
The more fast way to solve this is override the vendor class DatabaseDriver, in the namespace:
namespace Doctrine\ORM\Mapping\Driver;
On line 289, change:
if ( ! $table->hasPrimaryKey()) {
continue;
// throw new MappingException(
// "Table " . $table->getName() . " has no primary key. Doctrine does not ".
// "support reverse engineering from tables that don't have a primary key."
// );
}
To avoid any future problems, after mapping your database, return the settings to avoid any problems later.
Good luck!
Reference: Solves Doctrine ORM Error with tables without Primary Key on MySQL when mapping the database.
Not a very clean solution but for the moment I managed to do it by, exporting the schema of tables I need and recreating them on my local server. Forcing Pk's on the tables that did not have PK defined. This created entity class files instantly and worked like a charm.
In my Symfony2 project, I need to do some work in database with a Command.
When I launch it, I have this error :
[Exception]
There is no default connection
I don't understand why ?
My code :
1. $this->output = $output;
2. $this->writeln( 'ImportBase' );
3. $importBase = Model\ImportBase::builder()
->whereEgal('termine', 0)
->limit(1)
->getObject();
For information, there is just one connection in my project.
Also, I do not use Doctrine but ORM (I guess ? This is not my project)
Do you have any idea ? I can't find anything...
EDIT :
config.yml :
doctrine:
dbal:
default_connection: default
connections:
default:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
Parameters.yml
parameters:
database_driver: pdo_mysql
database_host: 127.0.0.1
database_port: null
database_name: [...]
database_user: [...]
database_password: [...]
Model :
<?php
namespace xxx\xxxhBundle\Model;
use xxx\ORM\Model;
class ImportBase extends Model
{
protected static $table_name = 'import_base';
protected static $primary_key = 'import_base_id';
}
Builder in Model file
/**
* Get command builder for the class
*
* #return DbCommandBuilder builder
*/
public static function builder() {
if( static::$table_name == null )
throw new \Exception('Canot get builder, table_name is not set');
$builder = static::dbConnection()->builder( static::tableName(), static::$builder_class );
$builder->setFetchClassName( get_called_class() );
return $builder;
}
That seems a little strange... I don't notice anything wrong based on the configuration you've supplied.
However, since you appear to only have one dbal connection, you could simply use the "terser" configuration:
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%
charset: UTF8
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
Hope this helps :)
I have to work with a real legacy mssql database and I'm trying to use the Doctrine DBAL Query Builder.
FYI I'm on a linux machine with working freetds connection
Until now I tried the https://github.com/realestateconz/MssqlBundle
and configured the database connection like this
doctrine:
dbal:
default_connection: default
connections:
default:
driver_class: Realestate\MssqlBundle\Driver\PDODblib\Driver
host: %database_host%
dbname: %database_name%
user: %database_user%
password: %database_password%
Now I used in the controller
$conn = $this->get('database_connection');
But now when I write $qb = $conn->createQueryBuilder();
it isn't aware of the createQueryBuilder...
Anyone knows a solution?
I am about to start a project in doctrine symfony, but I have to make connection with multiple databases. One of them is an existing database (SQL SERVER) that cannot be mapped with ORM. Is there any possibility to connect with this db with another db that is NOT mapped in doctrine and work with controllers normally?
I develop a multi-database sf2 app with doctrine2 orm mapping.
We use intellectsoft-uk/MssqlBundle
Our configuration is:
config.yml
# Doctrine Configuration
doctrine:
dbal:
default_connection: acme_mysql
connections:
acme_mysql:
host: %acme_mysql_database_host%
port: %acme_mysql_database_port%
dbname: %acme_mysql_database_name%
user: %acme_mysql_database_user%
password: %acme_mysql_database_password%
charset: UTF8
acme_slqsrv:
driver: sqlsrv
driver_class: \Realestate\MssqlBundle\Driver\PDODblib\Driver
host: %acme_slqsrv%
port: %acme_slqsrv%
dbname: %acme_slqsrv%
user: %acme_slqsrv%
password: %acme_slqsrv%
charset: UTF8
orm: #optional if you want to map some entity in doctrine2
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: acme_mysql
entity_managers:
em_mysql:
connection: acme_mysql
mappings:
AcmeMysqlBundle: ~
em_sqlsrv:
connection: acme_sqlsrv
mappings:
AcmeSqlSrvBundle: ~
This configuration permit you to take a connection instance in a controller/service and use it for access database and execute stored procedures and so on...
Hope this help