Case with doctrine2, symfony2 and postgresql entities

為{幸葍}努か 提交于 2019-12-19 21:52:05

问题


I have a problem with doctrine2 in symfony2 app with postgres database.

I get error:

SQLSTATE[3F000]: Invalid schema name: 7 ERROR: schema "main" does not exist 

Problem is that my schema is Main not main. When I rename it, similar thing happends for table relation:

SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "main.brand_brandid_seq" does not exist 

Problem is case sensitivity and I guess maybe it have something to do with quoting or some doctrine configuration.

Entity:

namespace MyB\Entity;

/**
 * MyB\Entity\Brand
 *
 * @orm:Table(name="Main.Brand")
 * @orm:Entity
 */
class Brand
{
    /**
     * @var integer $brandid
     *
     * @orm:Column(name="BrandId", type="integer", nullable=false)
     * @orm:Id
     * @orm:GeneratedValue(strategy="SEQUENCE")
     * @orm:SequenceGenerator(sequenceName="Main.Brand_BrandId_seq", allocationSize="1", initialValue="1")
     */
    private $brandid;

    /**
     * @var string $brandname
     *
     * @orm:Column(name="BrandName", type="string", length=32, nullable=false)
     */
    private $brandname;

    /**
     * Set name.
     *
     * @param string $name
     */
    public function setName($name) {
        $this->brandname = $name;
    }
}

Schema:

SET search_path = "Main", pg_catalog;

CREATE SEQUENCE "Brand_BrandId_seq"
    START WITH 2
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


SET default_tablespace = '';

SET default_with_oids = false;


CREATE TABLE "Brand" (
    "BrandId" integer DEFAULT nextval('"Brand_BrandId_seq"'::regclass) NOT NULL,
    "BrandName" character varying(32) NOT NULL
);

Controller:

        $reseller = new \MyB\Entity\Brand();
        $reseller->setName('Sasa');

        $em = $this->get('doctrine.orm.entity_manager');
        $em->persist($reseller);
        $em->flush();

Any idea?


回答1:


Try this

namespace MyB\Entity;

/**
 * MyB\Entity\Brand
 *
 * @orm:Table(name="""Main"".""Brand""")
 * @orm:Entity
 */
class Brand
{
    /**
     * @var integer $brandid
     *
     * @orm:Column(name="""BrandId""", type="integer", nullable=false)
     * @orm:Id
     * @orm:GeneratedValue(strategy="SEQUENCE")
     * @orm:SequenceGenerator(sequenceName="""Main"".""Brand_BrandId_seq""", allocationSize="1", initialValue="1")
     */
    private $brandid;

    /**
     * @var string $brandname
     *
     * @orm:Column(name="""BrandName""", type="string", length=32, nullable=false)
     */
    private $brandname;

    /**
     * Set name.
     *
     * @param string $name
     */
    public function setName($name) {
        $this->brandname = $name;
    }
}

In postgres every word case sensitive must be escape.




回答2:


When using escaped table names take care about this "bug" : https://github.com/doctrine/doctrine2/pull/615 . Doctrine takes the first character of the table name as aliasprefix and thus a quote is used, crushing all your SQLs




回答3:


I write PpSqlBundle but it isn't finished yet, but you could try, I think it should work. It can generate form db same as in symfony. in config.yml you should have:

doctrine:
dbal:
    default_connection: default
    connections:
        default:
            driver:   
            dbname:   
            host:     
            user:     
            password: 
            driverClass: PgSqlBundle\Doctrine\DBAL\Driver\PDOPgSql\Driver # it's important
            logging:  

and you should use command

app/console doctrine:mapping:import Yourbundlename annotation

https://github.com/mstrzele/PgSqlBundle




回答4:


If you are using migration files on Laravel. Change Schema::table to Schema::create. This might help someone.



来源:https://stackoverflow.com/questions/5573865/case-with-doctrine2-symfony2-and-postgresql-entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!