I am new to the Zend Framework and this is my first project, I have a list of clubs that when clicked it takes you to a description page of the club you specified. I then want a comments box for this club on the description page. I have a comments table, user table and clubs table in my database, all these tables will have to be linked by a foreign key so that I can see what club the comment is for and which user posted the comment. The problem I am having is understanding which keys will be foreign keys and how to do this in phpmyadmin. Once this is set up I am then wondering how to implement this in the ZendFramework does any one know of any useful materials online that I could read relating to this sort of issue?
Thanks
Rik
Once you set your database use Zend_Db_Adapter to connect to your DB and define classes for each of your tables, extending the abstract class Zend_Db_Table_Abstract as explained here .
I am then wondering how to implement this in the ZendFramework does
any one know of any useful materials online that I could read relating
to this sort of issue
Read this tutorial to get you started with ZF (it has an example similar to what your are trying to do) .
Take a look also at Zend_Db_Select which will help you retrieve data from your database .
and Zend_Db_Table Relationships is also useful.
In a table a foreign key is an "id" that is not the primary key of the table , so if you have 2 tables , clubs and comments , then
in comments table , the foreign key would be the club_id column so you can refer to the club primary key.
You should take the time to learn how a Zend Application is structured , check the documentation on ZendFramework website.
Related
I'm using .Net 4.5, entity framework 5, database first. I have a junction (many-to-many) table in my database. For this example lets say the table is "StudentsCourses":
Students
-PkStudentId
-Name
Courses
-PkCourseId
-CourseNumber
StudentsCourses
-FkStudentId
-FkCourseId
This works just fine right now. The 'generate model from database' creates a Student entity with a navigation property to the Course entity. But here is where the trouble is:
I need to add another column to the StudentsCourses table. Lets just call this column "CourseYear". So our junction table would now look like this:
StudentsCourses
-FkStudentId
-FkCourseId
-CourseYear
So, I've added this column to the database and ran "Update Model from Database" on the edmx. I would expect to see an entity created for StudentCourses, with a navigation property to both Students and Courses. But no such entity is created. I still see the same two tables (Students & Courses) with the same navigation property as before.
I've done a lot of reading and researching, but haven't really come across a clear-cut answer. There is a wealth of information on code-first which I can't apply to my scenario. Is there a way to get what I'm after? Is it as simple as adding a PkId to the StudentCourses table? SQL Replication is preventing me from doing this. I would think the composite should suffice, but maybe EF needs a PK to do it's magic? I read a little bit about manually setting relationships, but could not find anything speaking to my particular situation. It could be that I am just missing a simple step in the process of updating the edmx from database. I've done this plenty of times when: adding new tables, adding columns, deleting columns, etc. I'm following the same steps as I always do, but maybe I need to do something different in this case?
Thanks ahead of time for any help. It is greatly appreciated. Please let me know if any more information would help.
From what I've gathered it appears as though EF will not generate a model for a table that doesn't have a Primary Key.
I'm a bit late for this, but you have the answer in this thread Updating Entity Framework Model after adding a field to a previous look up only table
As they say here, you have to delete the relationship between Students and Courses in the designer. Then update your model from the database, and make sure StudentsCourses table is checked in the Tables branch of the Add tab.
I'm really new to Entity Framework (currently using EF5) and vs2012 and am having difficulty trying to figure something out.
I have an .edmx that was generated from my database. It has two tables in it: Item and 3rdPartyItem. In short, the Item table is the main table for all the company items while the 3rdPartyItem table is a table that is used to hold additional attributes for items. This 3rdPartyItem table was created by an outside company and is used with their software for the company, so I can't mess with either table. What I'm trying to do is show a list of Items in a grid but I need to show a combination of all the fields for both tables. In vs2012, if I create a relationship and make it 'zero-to-one' (because for each record in the Item table, there doesn't necessarily have to be one in the 3rdPartyItem table), vs complains about not being mapped correctly. When I set the mapping, it then complains that there's multiple relationships. I did some research and found that you can't create a relationship with 2 primary keys, so I was thinking that was the problem. But how can I change the .edmx so that in code, I can access Items and 3rdPartyItem like so:
var items = dbContext.Items;
items.3rdPartyItem.SomeField <--field from 3rdPartyItem table.
not too sure if it's even possible, but it would be very, very helpful if so. Any ideas?
What you're looking for is table-per-type (TPT) mapping inheritance. You can view an MSDN walkthrough here (although you'd want your base type to be instantiable):
http://msdn.microsoft.com/en-us/data/jj618293.aspx
I'm encountering some problems importing my DB on Entity Framework. Some relationship between tables returns me some other entities without any primary key.. For my application I need to have a PK for every table.
Is there any way to say something like:
MyEntityCollection.SetKey("ColumnName")
Or something like that?
Thank you very much!
Take a look here, i discuss this in this answer,
EF4 Unknown Column In Field List
http://xhalent.wordpress.com/2011/01/21/configuring-entity-framework-4-codefirst/ has some detail around the two ways in EF4 of specifying a FK
While creating a schema from a database many-to-many relationships between tables are not created.
Is this a principal problem?
Is it possible to detect from the table structure that many-to-many relationships exist and create the respective code in schema classes automagically?
It is indeed a somewhat fundamental problem -- many_to_many is a "relationship bridge" and not a "relation." The documentation explains that "the difference between a bridge and a relationship is, that the bridge cannot be used to join tables in a search, instead its component relationships must be used."
On the other hand, this means that if the real relationships are correctly discovered it should be straightforward to add the many-to-many relationships automatically: First, search for tables that have two or more has_many relationships. Then, for each pair of such relationships, create a many-to-many relationship bridge. (Of course, one might hope that DBIx::Class would do this itself.)
The problem with developing this kind of code is that many tables that contain multiple references are not many-to-many tables, and have multiple references for other reasons. For instance, I'll make up a schema for some fictional app where something could be regarded as a many-to-many table, when it is not.
create table category (
id primary key,
...
);
create table sub_category (
id primary key,
category references category(id),
...
);
/* EDIT:
This is the table that could be regarded as many_to_many
by an automated system */
create table product (
id primary key,
category references category(id),
sub_category references sub_category(id),
...
);
Something could be built this way for ease of use, without having to do multiple table joins in the database on a website, especially when considering speed. It would be difficult for a piece of code to say definitively 'this is not a many_to_many' situation, while the developer should be able to easily figure it out, and add in the many_to_many line below the checksum.
I consider DBIX::Class schema outputs a good starting point, and little more, especially when working with auto numbering in non-MySQL databases, among other things. I often need to modify above the "Don't modify above this line" stuff (although many_to_many can obviously go below that checksum, of course.
Please can you help me to enable the deleting of books from my database?
I am using EF 4 and have a many-to-many relationship between books and authors. When I try to delete a book, I get the following:
he DELETE statement conflicted with the REFERENCE constraint "FK_BookAuthor_Book". The conflict occurred in database "C:\PROGRAM FILES\MICROSOFT SQL SERVER\MSSQL10.SQLEXPRESS\MSSQL\DATA\NTCODING.MDF", table "dbo.BookAuthor", column 'BookAuthor_Author_Id'.
The statement has been terminated.
Being a SQL guru is not what I do, but I think it is telling me that there is a record in the join table that also needs to be deleted. I'm not sure which property I need to set or what bit of coding I need to add.
If you can help me with this I would be really appreciative.
Thanks in advance
Nick
When you create the foreign key relation between the table book and the table lying between book and author (the one that breaks the many-to-many relationship in two one-to-many relationship), try to specify the "cascade" action at "insert and update specification". The same for the link between authors and the middle table. Thus, when you try to delete an author (or a book), all the dependent records from the middle table will be deleted.
Looks like my sql knowledge wasn't too bad. I hoped that by removing the authors from the book's collection of authors the join table would remove the records....a la fantastico!
I had to do this in the controller action of my web application because I use a generic repository and do not cast it to a book in there. So if you do know a way I could enforce this rule in my repository instead, that would be useful still.
Thanks