How to Set Up Entity (Doctrine) for Database View in Symfony 2

How to set up entity (doctrine) for database view in Symfony 2

There is nothing special in querying a view — it's just a virtual table. Set the table of your entity this way and enjoy:

/**
* @ORM\Entity
* @ORM\Table(name="your_view_table")
*/
class YourEntity {
// ...
}

Using Views in Doctrine 2 with Symfony 2

You have Doctrine Entity and EntityRepository.

Queries should go to Repository Classes as a method. A mysql-view is just a query. A Repository Class returns one or more Entity classes.. I.E Row in Database Table.

Please provide some code and schema, to get better answers.

This could go to Order Entity:

public function getOrderTotal() {
$sum = 0.0;
foreach ($this->getOrderItems() as $item) {
//Process
}
return $sum;
}

Native MySQL Views handling and generating is not supported by Doctrine2.

Create Single Entity on Doctrine 2 + Symfony 2

php app/console doctrine:mapping:import --force AcmeBlogBundle:YOURENTITYNAME yml

How to structure models and use Doctrine in Symfony2

Pretty much all of my MYSQL database queries are stored in the models

That's bad, but I'll mention about it later.

Now after looking at Symfony2, my interpretation is that "model" files in Symfony are called Servies. Is this correct?

Queries should be done in either repositories or in some cases in services.
If your query returns entities, then it should be repository for sure.

My question here is do I place my custom Doctrine queries inside the Service files or do I create them inside the Entity files?

Entities should be plain PHP objects. They shouldn't depend on anything than other entities. Entities actually doesn't even know anything about database. It's pure object oriented business logic.

Again, all DB queries should be in repositories or services.

I would suggest to go through Symfony Book in first place, to get idea of how "the Symfony way" works.

Symfony2, Doctrine2, MySql, view tables

if you want to define views in mappers I think you can't do this
If you want simply use views to read data from them I think you can do it. Simply define views fields in mapper as for general tables

Symfony-Doctrine : join a table with a view after persisting entity

I've just checked, it works fine with Bidirectional One-To-One relation

In my case tables are defined like:

create table T (`id` int(11) NOT NULL AUTO_INCREMENT, name varchar(100), primary key (id));
create view V as select id as entity, name, '123' as number from T;

Annotations in T:

/**
* @ORM\Table(name="T")
* @ORM\Entity()
*/
class T
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255, nullable=true)
*/
private $name;

/**
* @var V
*
* @ORM\OneToOne(targetEntity="V", mappedBy="entity")
*/
private $view;

Annotations in V:

/**
* @ORM\Table(name="V")
* @ORM\Entity(readOnly=true)
*/
class V
{
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255, nullable=true)
*/
private $name;

/**
* @var string
*
* @ORM\Column(name="number", type="string", length=255, nullable=true)
*/
private $number;

/**
* @var T
*
* @ORM\Id
* @ORM\OneToOne(targetEntity="T", inversedBy="view")
* @ORM\JoinColumn(name="entity", referencedColumnName="id")
*/
private $entity;

And a test snippet to prove that it saves, updates and reads fine:

public function testCRUD()
{
/** @var EntityManager $manager */
$manager = $this->client->getContainer()->get('doctrine.orm.default_entity_manager');

$t = new T();
$t->setName('Me');

$manager->persist($t);
$manager->flush();

$t->setName('He');
$manager->flush();

$manager->clear();
/** @var T $t */
$t = $manager->find(T::class, $t->getId());
$this->assertEquals('He', $t->getView()->getName());
}


Related Topics



Leave a reply



Submit