Doctrine 2 - Generate entities with views from database

15,499

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.

Share:
15,499
user2839159
Author by

user2839159

Updated on June 23, 2022

Comments

  • user2839159
    user2839159 over 1 year

    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.

  • user2839159
    user2839159 about 10 years
    Where did you see that ?
  • manix
    manix about 10 years
    Try it yourself. Doctrine2 schema tool will not recognize the view as table, because it is a state.
  • user2839159
    user2839159 about 10 years
    I will try tomorrow at work. Look the last point, 26.2.1, it's hope maybe ... docs.doctrine-project.org/en/2.0.x/reference/…
  • user2839159
    user2839159 about 10 years
    Please,you have examples ?
  • ReynierPM
    ReynierPM almost 9 years
    And this works for you? I'm having issues trying to get this working see this post stackoverflow.com/questions/28141515/…
  • Taylan
    Taylan over 7 years
    @manix Why did you change your answer about support from not to is? See stof's comment
  • manix
    manix over 7 years
    @Taylan, just a note: readonly property has no relation with views, because the views naturally is just for reading purposes. And yes, I do; I changed from "not" to "is" because is completely valid working with views in Doctrine.
  • Taylan
    Taylan over 7 years
    @manix, what I mean is you can use views with doctrine but it is not officially supported. Also as far as i can see for readOnly entities Doctrine ORM still allows to insert/delete, but not updating them. So what would happen if someone were to try to insert/delete rows on an entity mapped to a view? I haven't tried but I'm assuming it will give an error. Maybe you can add a warning to your answer about this.
  • manix
    manix over 7 years
    @Taylan, I get your point, but lets put the things in practices.. You can not insert records into a view using doctrine neither directly from database, that is not possible, a view internally is just a SELECT statement. Indeed, if you plan insert records using a view you are not understanding the concept of view. In the other hand, lets have a "employees" table, and in your application want to display a list of birthdays, so create a view that calculates the next 20 birthdays called "birthdates". From doctrine you can use that view without problems. That is the main purpose of mapping a view.
  • Vincent Pazeller
    Vincent Pazeller about 6 years
    Actually, views in different rdms MAY be updatable or insertable (i.e. they are not always read only). For mysql, for instance: dev.mysql.com/doc/refman/5.7/en/view-updatability.html . Since the criteria of different rdms to make a view updatable or not are highly technical (and dependent), I think Doctrine may have chosen to send queries anyway and let the database reject the queries...
  • manix
    manix about 6 years
    @VincentPazeller, I think Doctrine may have chosen to send queries anyway and let the database reject the queries.... Totally agreed
  • ROLO
    ROLO over 4 years
    This answer is completely wrong, the OP is asking something different.