Doctrine 2 join table + extra fields - field

I've got two tables and a join table: 'staff', 'classification' and 'staff_classification'. In the join table I've got an extra boolean field: 'showclassification'. My annotation is as follows:
/**
* #ManyToMany(targetEntity="Staff", inversedBy="classifications")
* #JoinTable(name="staff_classifications",
* joinColumns={#JoinColumn(name="staffid", referencedColumnName="id")},
* inverseJoinColumns={#JoinColumn(name="classificationid", referencedColumnName="id", unique=true)});
*/
How do I add the extra field 'showclassifications' to the join table?
How do I reference the field via DQL? E.g. What query would get all of a staff's classifications that are allowed to be shown?
Do I place the above annotation in one class and a #ManyToMany annotation with no #joinTable in the other? E.g. #ManyToMany (targetEntity="Classification")?

You want an entity that describes the relationship (StaffClassifications), which has OneToMany relationships with both staff and classifications.
ManyToMany doesn't allow you have any "extra" properties, because the join table is not an entity, and thus can't have any properties.

Related

Create relationship on tables for both ID and TIMESTAMP

I have two tables which save data each day.
Table1:
timestamp/id_1/value1/value2
01.01.2020/123/asdf/xyz
02.01.2020/123/asdf/xyz12
03.01.2020/222/asdf/ertw
Table2:
timestamp/id_2/value1/id_1
01.01.2020/345/asdfx/123
02.01.2020/345/asdfy/123
03.01.2020/678/asdfc/222
There is a relationship between both tables:
Table2 * : 1 Table1
The problem when I create the relationship is, that it doesnt recognize the timestamp.
Do you have any idea how to realize setting up the relationship by ID AND by timestamp?
So that:
"02.01.2020/345/asdfy/123" relates to "02.01.2020/123/asdf/xyz12"
and:
"01.01.2020/345/asdfx/123" relates to "01.01.2020/123/asdf/xyz"
Thank you very much!
I am building the data model in PowerBI.
Just concatenate the columns for unique id in power query (or in the source, or sql) and use it for connect

Sequelize : how to join on foreign keys

I'm using Sequelize and Node.js, and I need to join 2 tables on 2 foreign keys,
tableA.hasOne(tableB, { foreignKey: 'fk_tableB' });
tableB.belongsTo(tableA, {foreignKey: 'fk_tableB' });
(by the way, I don't understand the functionality of "targetKey")
but I can only obtain a join on tableA.primaryKey = tableB.fk_tableB.
How can I replace the tableA.primaryKey by tableA.fk_tableA?
I also tried to define twice tableA : in 2 different structures (one with the real primary key and the other with fk_tableA as primary key), but it's also not working (because I need the real tableA mode in another place).
Has someone an idea? Is it a bug from Sequelize?
How can I replace the tableA.primaryKey by tableA.fk_tableA?
There is no tableA.fk_tableA. But if there were, we would expect you to have named it that because column tableA.fk_tableA is a FK to a key column in tableA. Because that's the convention for naming a column fk_tableA. Similarly we would expect a belongTo like yours that adds a column that is a FK to the tableA PK to call it fk_tableA, not fk_tableB. Just like your hasOne gives tableA a column fk_tableB to the tableB PK. (If you want a FK to be to some other column than the PK then you say so via targetKey.)
If you so named FKs after their target table, you seem to want tableA.fk_tableB = tableB.fk_tableA. The way you have named them now, you seem to want tableA.fk_tableB = tableB.fk_tableB.
I need to join 2 tables on 2 foreign keys
It is extremely unlikely that you need the join above. Declaring a column to be a FK says that a value of the source/referencing column is always a value of the target/referenced column. Here targets are PKs. Such a join on a FK to one table and a FK to another table will only return rows that share the same PK value, even though the PKs are from different tables.

How to join this tables properly?

I have 2 tables, one is for the categories and the other is for the particulars of the corresponding categories.
I need all categories to be shown with their corresponding particulars.
I am not clear about your question and i assume you are using sql server
select t1.category,t2.particularname from categories t1 left join particulars t2 on t1.categoryid=t2.categoryid
From what you said, it seems Categories table has one-to-many relation with particulars. That means Primary Key of Categories should be refered in particulars as Foreign Key in Particulars. You can use following Query to join
SELECT * FROM categories C JOIN particulars P ON C.cat_id = P.cat_id;
And you should rename if some other columns in both tables have same name and you don't want them to be a part of JOIN.
For more example on JOIN.

Compare 3 tables in SQLite

Originally, I have 2 tables. I normalized it since the relationship of this tables is many to many. Now I have 3.
Jobs
jID PK
jName
jDesc
jEarnings
jTags
Course
cID PK
cName
cDesc
cSchool
cProgram
JobsCourse
ID PK
jID FK
cID FK
My app displays a tableview of the jobs
When clicked it displays the UIViewcontroller of jobs plus a tableview of the related course
How do I query the jobcourse table so that I can get all the related Courses to a certain job?
You can join the two tables, e.g.:
SELECT course.* FROM course INNER JOIN jobscourse ON jobscourse.cID = course.cID WHERE jobscourse.jID = ?
That gets all entries from course where the jID in jobscourse is equal to some value.

Performing Left Join in Rhomobile

Is it not possible to perform a left join in Rhomobile?
I have models PriceGroups, PriceLookup which have a 1-many relationship (ie. each PriceGroup and have many PriceLookup records).
I need to do a simple SQL Left Join so I have the required information from the PriceGroups Table
SELECT * FROM PriceLookup
LEFT JOIN PriceGroups ON PriceLookup.price_group_code=PriceGroups.code
I have added this to the price_lookup model:
belongs_to :price_group_code, 'PriceGroups'
The following is what I have tried in Rhomobile
PriceLookup.find_by_sql("SELECT *
FROM PriceLookup
LEFT JOIN PriceGroups on PriceLookup.price_group_code=PriceGroups.code")
But I get error:
Error: could not prepare statement: 1; Message: no such table: PriceGroups
I know I can do two selects and join them myself but this is a very crap way of doing it
You need to create the RhoMobile model as FixedSchema, not using the default PropertyBags.
Otherwise you don't have a real table in SQLite but you're using the special objectValues table that is implementing a Key-Value store:
http://docs.rhomobile.com/rhodes/rhom#fixed-schema
Example:
dbPT = ::Rho::RHO.get_src_db('PriceLookup')
sql = "SELECT * FROM PriceLookup LEFT JOIN PriceGroups ON PriceLookup.price_group_code=PriceGroups.code"
lines = dbPT.execute_sql(sql)
This could happen if the table PriceGroups hasn't been initialized (created) yet. Tables are created when the model class is loaded, if they don't exist.
For your case, simply call the model class, this will load it and create the table if necessary.
PriceGroups; #Only for create the table
PriceLookup.find_by_sql("SELECT * FROM PriceLookup LEFT JOIN PriceGroups on PriceLookup.price_group_code=PriceGroups.code")

Resources