Doctrine 2 - Creating Objects with Views from a Database

Can I create a view from a database using Doctrine 2?

I explain:

My db contains some views that I want to use, but I do not know how to generate these views.

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

+6
source share
2 answers

Not database views are currently supported by Doctrine 2, but can potentially work very poorly. Try matching the view as an entity yourself and marking it as an @readOnly object.

+5
source

For those interested in the answer:

I based my answer on this: How to set up an object (doctrine) to represent a database in Symfony 2

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

In src / Name / YourBundle / Resources / config / doctrine, create "A.orm.yml" with attributes 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 file in the Name / YourBundle / Entity / A field:

 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 opinion using your controller.

+2
source

Source: https://habr.com/ru/post/956424/


All Articles