In CakePHP, we can provide recursive option while retrieving data from database, which automatically fetches the data from all the dependent tables.
We can also provide option for recursive e.g recursive=2, which fetches dependent tables of the dependent tables.
How can we achieve the same functionality in Zend framework?
If you have defined Table Relationship on your model before, you can use findDependentRowset() to retrieve records from multiple related table. For more details, see this.
But i am not sure of limiting the recursion.
Related
Hi I'm using laravel in my current project. I thought its recommended to create a model for each database table to get the best out of eloquent's Relationships
somewhere in my Database I have this mapping table to map records from table A to table B (many to many)
should I create a model for my mapping table ? what is the best practice in such cases?
This question is best answered from an architetural point of view:
Models are made to represent entities. Mapping Tables do not store entities but information about relationships between entitites (many-to-many). Therefore, creating models for mapping tables does not make much sense.
Laravel offers a concepts called pivot-table for this kind of usecase, which is very well documented in the docs.
I'm working on a green-field application that has a corporate mandate that Stored Procedures are used for all database interaction.
I'd like to use Entity Framework and leverage Stored Procedure Mapping to gain the benefits of the ORM.
Since we will be developing the database and .NET application in parallel, I'm looking for information to help the database developer/administrator. Does anyone know of a consolidated guide on how to design tables and stored procedures so they can be best integrated with the Entity Framework?
A couple tips I've collected are:
Update Stored Procedures require exactly 1 parameter per table column
There must be an insert, update, and delete Stored Procedure for every table
I want to know as much about how the database should be designed for easy use with Entity Framework because the database is very difficult to change later in our environment.
I wrote a blog post describing the limitations of using mapping in this way after working on this for several months:
The Pitfalls of Mapping the Entity Framework to Stored Procedures
If you want to use Stored Procedures are used for all database interaction, I just don't see the need to use Entity Framework. One good reason of EF is to save time to write T-SQL, and if you don't take advantage of this, why even use EF?
We currently have a system with quite large database and stored procedures are used both for CUD and querying. DataSets are used to retrieve the results from the SP querys.
Now we are looking into developing another project against the same database using Entity Framework. When querying the database, the stored procedures often perform a lot of joins to gather some fields not in the target table, but data from the joined tables that is needed by the client in some way. When using DataSets, all the fields returned by the SP was included in the DataTable. So the DataTable doesn't actually match the target database table.
What is the correct way of handling this scenario in EF? When creating my model, the entities are mapped to each table which as mentioned above only sometimes matches the result of the SP. Can I add the "additional" fields of the SP query result to the entity class as properties and have them filled by the query but with these properties being excluded when it comes to CUD on the specific entity type? Seems like the EF-way, if queried through LINQ to Entities and not SPs, would be to have entity instances with relational properties to the joined entities so that using those "additional" properties would be done by navigating the relational properties?
You can define an arbitrary complex type (just a class that's generated for you, to match the columns and datatypes returned by the stored procedure) as the return type for your stored procedure in Entity Framework (as of version 4 and newer) - no problem here.
See Stored Procedures in the Entity Framework for a great explanation of all things related to using stored procedures in Entity Framework.
I'm trying to display the results of a sproc in my MVC 3 web app.
However, the sproc calls into 4 tables on one database and joins them with 5 views (single table views only, thank goodness) on another database. Each (SQL Server) db is on a separate server but that shouldn't matter.
I've read this: http://blogs.msdn.com/b/swiss_dpe_team/archive/2008/02/04/linq-to-sql-returning-multiple-result-sets.aspx
and this:
http://www.codeproject.com/KB/dotnet/linqToSql5.aspx
and still cannot determine whether I should use the dataContext classes or just embed the straight SQL.
Perhaps there is a better way to return my results than LINQ to SQL (15 columns, 3 different data types)? I need to update the tables as well. The user will have the ability to update each value if they choose. Is this a task best suited for the entity framework classes?
I plan on using the repository pattern so I can change data access technology if I must but would rather make the correct decision the 1st go 'round.
I was hoping for a resource that was more up-to-date than say, NerdDinner and more robust than the movie apps for MVC3 that abound, particularly implementing the sproc results inside a view. Any suggestions would surely be appreciated. Thanks.
Once you plan to "update" data then you are going to handle it all through stored procedures. Both Linq-to-sql or Entity framework will not help you with this because they are not able to persist changes to something created from arbitrary query. You should very carefully check if you are even able to track the data back to the correct record in the correct table. Generally result of a stored procedure is mostly for viewing the data but once you want to modify the data you must work with each table directly or again use some stored procedure which will do the task. Working with tables from multiple databases can be pretty complex in entity framework (EF doesn't support objects from multiple databases in one entity model).
Also what you mean by 15 columns, 3 different data types? Stored procedures support in both Linq-to-sql and Entity framework will return enumeration of one flattened data type containing 15 properties.
I'm not aware of anything that linq-to-sql can do that Entity Framework can't really, so EF seems to be a better solution in this case. You can add a stored procedure to your Entity Framework model as well, so you can just have it call the procedure and deal with whatever comes back.
Since the end goal will involve accessing the same Databases with either technology and they will be using sql to retrive the data either way its really a subjective anwser.
I would use whatever technology you are most comfortable and focus more on the implementation. Both data access platforms are based off of ado.net technologies and are for the most part equally powerful.
Regardless of the technology I would evaluate how the data is accessed and make implementation decisions based on that.
We have an application that creates new tables at runtime, but always with the same table schema. The only thing that varies from one of these tables to the next is the table name. Is it possible to access these tables using Entity Framework, specifying which table to access by name?
Entity Framework is not designed for DDL, it's an ORM tool for data access. You would want to use a simple ADO.NET query to create/drop the table.
Creating and dropping tables for every user session will make your log file grow very big very fast. I would consider carefully the reasons you think this is necessary. If the data is temporary, why not save the Session ID in each row and truncate the table on a daily basis?
UPDATE:
No, not really. The Entity Data Model is not dynamic, it's a static XML document that describes the structure of the database. If you want to interact with a table with a dynamic name, you're going to have to stick to "classic" ADO.NET.
With Linq to SQL I guess it would be possible with a stored procedure taking the table Name as a parameter.
A nice post about SP in L2SQL: http://weblogs.asp.net/scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx
I don't know if that feature exists in EF.