Doctrine 2 - Generate entities with views from dat

2019-02-13 12:56发布

Is it possible to generate a view from database with Doctrine 2?

I explain:

My db contains some views I want use, but I don't know how generate these views.

In my case, I have two tables and one view, the view selects a few columns in each table and I just want THIS view in the folder "Entity" of the project.

2条回答
手持菜刀,她持情操
2楼-- · 2019-02-13 13:33

Database views are not currently supported by Doctrine 2, but it can potentially perform very badly. Try it yourself mapping the view as an entity and mark it as a @readOnly entity.

查看更多
Lonely孤独者°
3楼-- · 2019-02-13 13:36

For people who are interested by the answer :

I based my answer on this : How to set up entity (doctrine) for database view in Symfony 2

For example , if you have a view "A" with column "Id" and "Name" :

At src/Name/YourBundle/Resources/config/doctrine, create "A.orm.yml" with attributs like :

Name\YourBundle\Entity\A:
type: entity
table: A
fields:
    id:
        id: true
        type: integer
        unsigned: false
        nullable: false
        column: id
        generator:
            strategy: IDENTITY
    name:
        type: string
        length: 35
        fixed: false
        nullable: true
        column: name

After that, create your A.php in Name/YourBundle/Entity/A :

namespace Name\YourBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="A")
 */
class A {

    /**
    * @Id @Column(type="integer")
    */
    private $id;
    private $name;

    public function getId() {
        return $this->id;
    }

    public function getName() {
        return $this->name;
    }

}

And ... you can call your view with your controller.

查看更多
登录 后发表回答