简体   繁体   English

无法使用 symfony2 连接数据库 oracle

[英]Cannot connect database oracle with symfony2

what i need我需要什么

  • i need to connect oracle database with symfony2.我需要用 symfony2 连接 oracle 数据库。

  • i have checked by php -m我已经检查过 php -m

    • oci8 oci8
    • pdo_odbc pdo_odbc

    • odbc数据库

here is the link i follow https://gist.github.com/johnkary/6481664这是我关注的链接https://gist.github.com/johnkary/6481664

a.)config.yml 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 b.) 参数.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 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然后运行symfony数据库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.我已经用谷歌搜索了一天,但我认为很少有开发人员使用 symfony2 和 oracle。

  • i had made simple php script that works to test connections我制作了简单的 php 脚本来测试连接

    $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 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.我已经使用过 sf3 和 mysql,但我是 oracle 的新手,这就是为什么我不知道如何将 oracle db 与 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.任何人都可以分步建议如何将 oracle db 与 sf2/3 连接,这是最值得赞赏的。

  • link of github relies same issue that im facing https://github.com/doctrine/dbal/issues/1564 github 的链接依赖于我面临的相同问题https://github.com/doctrine/dbal/issues/1564

    • i have also changed the parameter.yml file我也改变了 parameter.yml 文件

       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"然后错误您请求了不存在的参数“database_host”

last & final solution i tried but didn"t worked i think there bug in sf2 for oracle我尝试过的最后一个和最终解决方案但没有奏效我认为 oracle 的 sf2 中存在错误

    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这是我用来连接 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.我正在处理连接到旧版 Oracle 11G 数据库的 Symfony 5 项目。 For the most part Doctrine works fine.在大多数情况下,Doctrine 工作正常。 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注意:如果您使用的是 Oracle 11G Express Edition,您的数据库名称很可能是 XE

Next update your doctrine.yaml file.接下来更新您的学说.yaml 文件。 In my case I have two databases.就我而言,我有两个数据库。 The default one is MySQL and the 2nd database is Oracle 11G.默认一个是 MySQL,第二个数据库是 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.安装 DoctrineExtensions 包也是一个好主意,它为 Doctrine 中的 Oracle 和 MySQL 提供更多支持。

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.您现在应该能够使用 Doctrine 来执行查询。 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:在需要执行 RAW 查询的情况下:

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.如果您需要帮助在 Mac 上设置本地系统的最后一项,您可以使用它来帮助设置 Lando。 Note I had to setup Oracle 11G on AWS EC2 then was able to use the IMP tool to import my dump files.注意我必须在 AWS EC2 上设置 Oracle 11G,然后才能使用 IMP 工具导入我的转储文件。

If you setup RDS Amazon will default to Oracle 20 or some newer version.如果您设置 RDS,Amazon 将默认使用 Oracle 20 或某些更新版本。 AWS RDS does not support the older backups from IMP. AWS RDS 不支持来自 IMP 的旧备份。 RDS will only support data pump using S3. RDS 将仅支持使用 S3 的数据泵。

https://github.com/rsaylor73/lando-apache-php-mysql-oci8 https://github.com/rsaylor73/lando-apache-php-mysql-oci8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM