简体   繁体   中英

Using Views in Doctrine 2 with Symfony 2

I have a database for orders (simplified): order: {id, shipment, discount, date} order_item: {id, order_id, name, amount, price}

If I want to get the full price (SUM(item prices)+shipment-discount) I could of course ad a method to my Order class that does the query. On the other hand, it would be handy to have a view on order that includes the full price.

Is it possible to integrate that into a Doctrine2 entity object? Is it even possible to generate such a view via annotations in the class, as I am maintaining my database layout with Symfony/Doctrine?

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.. IE 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.

mmm ¿Posible? I don't know. I think doing a custom repository for these entity would be the best. Entities with queries in its methods are not clean. Here there is an howto example working with entities and custom queries http://www.zalas.eu/doctrine2-and-symfony2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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