I have table (let's name it "First") with columns id, userID, moonID, typeID. And another table (let's name it "Second") where is also column "typeID" and TypeName (simple example, basicly this table is a huge storage of data).
I need create simple relations with this two (entities) tables such way that i can simple create new entries in "First" table and remove\edit them. But i don't want to del\add\edit entries from "Second" table. So "Second" is untouchable at all, we just select data from "Second" by typeID of "First". How can i do this?
I want to see two entities and controller. Please help me with it.
Its very easy have a look at here
http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html
You will know what to do. they have examples over there.
By default Doctrine wont delete the linked side unless you do onCascade="Delete"
You can use OnDelete= Null if you want to make the linked is to null in case of deletetion of owning side
Related
I've known how to map a model to a table in SQL in MVC using this:
[Table("table_name")]
I found this answer Take data from different tables and display it in View Index in mvc4
. The answer was to create a new model on joining tables in SQL then bind columns to model's fields.
However, can I make it easier by mapping my models to SQL views? Since views are considered to be (virtual) tables and they already contain what I need.
Yes you can map an entity to a view. EF doesn't know or care whether the object is a table, view, synonym or external table. You just need to map the entity to the view name, and declare the key columns. Views don't really have key columns, so just use whatever combination of columns uniquely identifies a row in the view results.
I have entities "Article" and "Product". Now I want to add comments to these 2 entities. Should I create 2 different entities "ArticleComment" and "ProductComment" with the same properties, and build a ManyToOne relation to their respective entity, or create a single "Comment" entity and find a way to build a relation to both "Article" and "Product" entities. Considering solution #2, how could I do that ?
Considering solution #2, how could I do that ?
One way would be to use Single Table Inheritance
Single Table Inheritance is an inheritance mapping strategy where all classes of a hierarchy are mapped to a single database table. In order to distinguish which row represents which type in the hierarchy a so-called discriminator column is used.
This means that you can easily create two separate entities ArticleComment and ProductComment both extending Comment. Then you use the advantages DiscriminatorMap column provides.
Your Comment entity could hold a relation called parent for instance that would refer to either your Article or Product entities. By creating new instance of ArticleComment or ProductComment your discriminator map field would be automatically populated depending on which type you're using.
This would also give you advantages with using DQL to query related comments by their type. Example from documentation:
$query = $em->createQuery('SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u INSTANCE OF Doctrine\Tests\Models\Company\CompanyEmployee');
And more. You can read that chapter here. Of course this is just a sample and you can use completely different approach.
In AX2009, how is it possible to find the table that is related to an InventTransType in the code?
For instance, InvnetTransType::Sales is related to the SalesTable, but how is it possible to get the table id or table name from SalesTable in the code?
I do not think this is possible, because there is no 1:1 relationship between the elements of the InventTransType enum and tables. At best there is a 1:n relationship (e.g. you could argue that InventTransType::Sales is related to table SalesLine just as well or even better as table SalesTable). Note also that the documentation on the enum says that it specifies "the module that generated the transaction".
It really depends on the question you ask and the data you want to retrieve. That said, here are some points you could research, maybe one of them fits your question/requirements. If all else fails, you can always write your one mapping method that takes an enum element and gives you the id of the table you think is appropriate for this element(standard AX does this in several cases to map other entities to the enum elements, see for example table InventDimSetup, method transType2FieldId).
cross references: check the cross references of the enum or of an enum element, that should give you an idea which table(s) are associated with which element
relations of table InventTrans: for some of the enum elements, you can find table relations in table InventTrans (but unfortunately not for all of them); ponus point is that by using reflection, you can analyze the relations and get the referenced table (which is probably as close to your requirement as it can get in standard AX)
I have a problem more concerning database relations than Doctrine itself.
I have a table "project" and a table "project_data". My table "project_data" is ALWAYS linked to a project entry.
However, in my table "project", I can have two references to a project_data entry : project_data_id, and project_data_waiting_id. However, these references can be null and have no relation with the "project_id" that is set in project_data table.
Question :
How to define all these relations? I want to be able to have project entries without any project_data references.
How to handle it with Doctrine? I'm kind of new with Doctrine and with database design, and I am a bit lost between all the joins that I have to do between my tables.
I join you a diagram to have a better idea of what I want to do.
Thank you.
In this case I will give you two options (assuming project_data always has only one project):
First:
project
- id
- project_data_id
- project_data_waiting_id
project_data
- id
- name
In that case you can define two one-to-one relationsships on your project class. Look at http://docs.doctrine-project.org/en/latest/reference/association-mapping.html#one-to-one-bidirectional for more information how to handle this.
Second: option:
It's also possible to create a many-to-many relationship and give your project_data a status. It would look like this:
project
- id
project_data
- id
- name
- project_id
- status_id
project_data_status
- id
- name
In this case project will have a many-to-one relation with project_data and project_data will have a one-to-many relation with project_data_status. This solution gives you more flexibility. You can add as many project_data objects to project as you like.
How to define relations in doctrine2 can be found on the same page I already provided in this post.
Hopefully this will point you in the right direction.
I am using EF 4.1 RC and CodeFirst/POCO to build my database by code.
Imagine having a many-to-many relationship like Teachers-Students (one teacher can have many students, and one student may have many teachers). Accordingly I have two POCOs: (1) Teacher and (2) Student.
When EF creates the corresponding tables you will end up with three tables: (1)Teachers, (2) Students and (3) an extra join table. The join table contains exactly two fields: a Teacher_ID and a Student_ID.
I was wondering if I had any chance to add an extra field to the join table, e.g. "Grade" (the grade a certain teacher gives a certain teacher)?
Currently I have no idea how to achieve this with only two POCOs.
So I guess all I can do is create a third POCO (for the join table) manually, am I right? That will certainly work, but then I am losing nice navigation properties like oneTeacher.Students.First(), etc. That is the main reason why I am still looking for another way.
That's correct, and does not only apply to Code-first. If you have extra fields in your joining table, you will have it mapped as an entity. And vice-versa, if you want an extra field in your joining table, you need to create a new entity and have zero-or-one-to many or one-to-many navigation properties to the Teacher and Student entities. In any case, you lose the comfort of accessing Teacher.Students and Student.Teachers and have to go via the intermediate entity.
Alternatively, you could think about modeling the DB structure differently and extracting the extra info into the Teacher or Student or a fourth entity. But that depends entirely on your scenario.
Yes, the join table cannot have a payload or you need to break it down to 2 one to many association which will result in creating a third entity to hold the PKs as well as the additional properties.
This is an idea I still haven't found time to try it out. Maybe you can keep your Student and Teacher classes as they are, and add a third POCO StudentGrade with properties Student, Teacher and Grade. Then you'll have to use the fluent API to make sure that both the many to many relation between Student and Teacher and the StudentGrade class map to the same table.
Hope that helps