Insert and delete from "associations table on EF model" - linq

I have to tables "ALUMNOS" and "MATERIAS". In SQL exist another table "ALUMNOS BY MATERIAS". I know this is not necessary in EF model, because exists the properties navigation. But how can insert or delete from this " associations table"??
Thanks in advance!!
Guille

The only way would be to remove the link between the Materia and Alumno objects before saving again. This way they are individual objects or only contain links that are required. This will set the mapping table correctly.

Created the tables and got the objects like you have got. To insert a new row, I do the following
StackExchangeExampleEntities exchangeExampleEntities = new StackExchangeExampleEntities();
Alumno alumno = new Alumno() {AlumId = 2, Description = "aldesc"};
Materia materia = new Materia() {materia_Id = 2, description = "mdesc"};
materia.Alumnos.Add(alumno);// attach the relationship
exchangeExampleEntities.Materias.Add(materia);
exchangeExampleEntities.SaveChanges();
To Delete the mapping table, I clear down the collection in one side ( you can use either collection - Alumno or materia)
StackExchangeExampleEntities exchangeExampleEntities = new StackExchangeExampleEntities();
Materia materiaFetched = exchangeExampleEntities.Materias.Single(m => m.materia_Id == 2);
materiaFetched.Alumnos.Clear();
exchangeExampleEntities.SaveChanges();
If you just want the mapping table to be used, then add a new entityframework file and then select only the mapping table( dont select any thing else). This will create entity just for this mapping table in the context.
Now you can use it like this:
StackExchangeExampleEntities1 exchangeExampleEntities1 = new StackExchangeExampleEntities1();
MateriaAlumono s = exchangeExampleEntities1.MateriaAlumonoes.First();
Console.WriteLine(s.Alumuno_id);

Related

Oracle trying to update a table by joining a non indexed table

I tried looking for a similar example to my problem but could not reproduce the solution to my success.
I have 2 tables, Controller and Actions.
The Actions table has the columns Step, Script, Description, Wait_Until and Ref_Code.
The Controller table can only be joined on the Action table by the Ref_Code.
The Action table cannot have a PK because for each Ref_Code there is a Step to be taken.
Im getting an error when trying to update the Controller table using a merge statement:
ORA-30926: unable to get a stable set of rows in the source tables
My merge statement is as follows:
MERGE INTO DSTETL.SHB_FTPS_CONTROLLER ftpsc
USING (SELECT DISTINCT FTPSC.SESSION_ID,
FTPSC.ORDER_DATE,
sa.step,
sa.next_step,
LAST_ACTION_TMSTMP,
SA.ACTION_SCRIPT,
sa.ref_code,
SA.WAIT_UNTIL
FROM DSTETL.SHB_FTPS_CONTROLLER ftpsc, DSTETL.SHB_ACTIONS sa
WHERE SA.REF_CODE = FTPSC.REF_CODE
AND SA.STEP > ftpsc.curr_step
AND sa.step = ftpsc.next_step) v1
ON (v1.REF_CODE = FTPSC.REF_CODE)
WHEN MATCHED
THEN
UPDATE SET FTPSC.LAST_ACTION_TMSTMP = CURRENT_TIMESTAMP,
ftpsc.next_step = v1.next_step,
ftpsc.curr_step = v1.STEP,
ftpsc.action_script = v1.action_script
WHERE CURRENT_TIMESTAMP >= v1.LAST_ACTION_TMSTMP + v1.WAIT_UNTIL;
COMMIT;
I tried doing this using a normal update as well but Im getting ORA-01732: data manipulation operation not legal on this view.
UPDATE (SELECT FTPSC.SESSION_ID,
FTPSC.ORDER_DATE,
FTPSC.CURR_STEP,
FTPSC.NEXT_STEP,
FTPSC.ACTION_SCRIPT,
sa.step, --New Step
sa.next_step AS "NNS", --New Next Step
FTPSC.LAST_ACTION_TMSTMP,
SA.ACTION_SCRIPT AS "NAS", --New action script
sa.ref_code,
SA.WAIT_UNTIL
FROM DSTETL.SHB_FTPS_CONTROLLER ftpsc
LEFT JOIN
DSTETL.SHB_ACTIONS sa
ON SA.REF_CODE = FTPSC.REF_CODE
AND SA.STEP > ftpsc.curr_step
AND sa.step = ftpsc.next_step) t
SET t.curr_step = t.step,
t.LAST_ACTION_TMSTMP = CURRENT_TIMESTAMP,
t.next_step = t."NNS",
t.action_script = t."NAS";
COMMIT;
Any advice would be appreciated, I already understand this is because the Action table has multiple Ref_Codes but REF_CODE||STEP is unique. And the output of:
SELECT DISTINCT FTPSC.SESSION_ID,
FTPSC.ORDER_DATE,
sa.step,
sa.next_step,
LAST_ACTION_TMSTMP,
SA.ACTION_SCRIPT,
sa.ref_code,
SA.WAIT_UNTIL
FROM DSTETL.SHB_FTPS_CONTROLLER ftpsc, DSTETL.SHB_ACTIONS sa
WHERE SA.REF_CODE = FTPSC.REF_CODE
AND SA.STEP > ftpsc.curr_step
AND sa.step = ftpsc.next_step;
Is how I want the Controller table to be updated like.
Thanks in advance.
It sounds like what you want to do is: update each row in the Controller table with the matching "next step" details from the Actions table. But your Merge statement is querying the Controller table twice, which confuses things.
Is this what you're trying to do?
MERGE INTO DSTETL.SHB_FTPS_CONTROLLER ftpsc
USING (SELECT
step,
next_step,
ACTION_SCRIPT,
ref_code,
WAIT_UNTIL
FROM DSTETL.SHB_ACTIONS
) sa
ON (sa.REF_CODE = FTPSC.REF_CODE)
WHEN MATCHED
THEN
UPDATE SET FTPSC.LAST_ACTION_TMSTMP = CURRENT_TIMESTAMP,
ftpsc.next_step = sa.next_step,
ftpsc.curr_step = sa.STEP,
ftpsc.action_script = sa.action_script
WHERE CURRENT_TIMESTAMP >= ftpsc.LAST_ACTION_TMSTMP + sa.WAIT_UNTIL
AND SA.STEP > ftpsc.curr_step
AND sa.step = ftpsc.next_step;
EDIT: updated query
EDIT2: So, in your original query, in the USING section you were selecting the rows in the Controller table that you wanted to update... but you never joined those rows to the Controller table from the MERGE INTO section to match them up. Having the same alias "ftpsc" just made it less clear that they're two separate objects in the query, and which one you wanted to update.
Honestly I don't really understand why Oracle won't let you update columns that appear in the USING..ON clause. It apparently works fine in SQL Server.

Magento addFieldToFilter: how to getCollection() from table using forigen key

I am new to magento, tried to solved it, but not succeeded.
My Problem is that,I have to apply "addFieldToFilter" filter on base if "store_id" , I have to drop all orders for a specific store/stores from collection on table
sales_flat_order
but store_id is in table
sales_flat_order_address
"sales_flat_order_address" table's primary key "entity_id" is used as foreign key in "sales_flat_order_address" as "parent_id", I have written a query some thing like this,
$ordercollection = Mage::getModel('sales/order')->getCollection();
$ordercollection->getSelect()->joinLeft(array('sfoa' => 'sales_flat_order_address'),'main_table.entity_id = sfoa.store_id',array('store_id'=>'sfoa.store_id'));
$ordercollection->addFieldToFilter('store_id',array('neq'=>'555'));
I don't understand it, And it's not working
You can use this to filter your orders by store id
$ordercollection = Mage::getModel('sales/order')->getCollection();
$ordercollection->addFieldToFilter('store_id',array('neq'=>'4'));
$ordercollection->getSelect()->joinLeft('sales_flat_order_address', "main_table.entity_id = sales_flat_order_address.parent_id",array('country_id'));
try this extension for deleting orders..
http://www.magentocommerce.com/magento-connect/delete-orders-6.html

SQL Statement : Update issue

I have these three tables :
RESEARCHER(Re_Id, Re_Name, Re_Address, Re_Phone, Re_HomePhoneNumber,
Re_OfficeNumber, Re_FirstScore, Re_Second_Score)
PUBLICATION(Pub_ID, Pub_Title, Pub_Type, Pub_Publisher, Pub_Year,Pub_Country, Pub_StartingPage, Pub_Number_of_Page, Score1, Score2)
WRITTEN_BY(Re_Id, Pub_ID)
I want to change the authors of the publication “Introduction to Database System” to “Henry Gordon” and “Sarah Parker”.
The problem is in WRITTEN_BY table,I just store the researcher's ID and publication's ID.
My idea is to change the Re_Id in WRITTEN_BY by those names are "Henry Gorgon" , "Sarah Parker" , which are already existed in RESEARCHER table
UPDATE WRITTEN_BY
SET Re_Id = ....( SELECT Re_Id
FROM RESEACHER
WHERE Re_Name = ‘Henry Gordon’ OR Re_Name = ‘Sarah Paker’ )
WHERE Pub_ID IN ( SELECT Pub_ID
FROM PUBLICATION
WHERE Pub_Name = ‘Introduciton to Database system’ );
I have problem in the SET part,so how to write the SQL statement for that requirement?
Here is the sqlfiddle link for my schema : http://sqlfiddle.com/#!9/b9118/1
I'd use something like below query:
DELETE FROM WRITTEN_BY WHERE Pub_ID IN (
SELECT Pub_ID FROM PUBLICATION
WHERE Pub_Title = 'Introduciton to Database system' )
INSERT INTO WRITTEN_BY
SELECT Re_Id,Pub_Id
FROM RESEARCHER CROSS JOIN PUBLICATION
WHERE Re_Name = 'Henry Gordon' OR Re_Name = 'Sarah Paker'
AND Pub_Title like 'Introduciton to Database system'
SELECT * FROM WRITTEN_BY
The idea is to first drop the existing mapping- you should not update it- and the insert a new one.
The reason for delete/insert approach vs update in case of mapping table is justified in favor of delete/insert as most mapping tables contain many-many mapping and usually one-to-many mappings.
Initially we may have a book mapped to say n number of authors where n <>1 then we either add extra rows, or are left with extraneous rows.
See sample fiddle: http://sqlfiddle.com/#!6/a0e72/13
The real deal however is CROSS JOIN. This does not take any ON like other JOINs and is used to produce cartesian product type map.
We are restricting it to get only limited number of rows as per our need by adding suitable WHERE clauses

ORA-017779 : cannot modify a column which maps to non key-preserved table

How can I solve this error:
ORA-017779 : cannot modify a column which maps to non key-preserved table.
My Code:
UPDATE (SELECT SOBS034.T1 as OLD, SOBS063.T1 as NEW
FROM SOBS034
INNER JOIN SOBS063 ON SOBS034.ID = SOBS063.ID
where SOBS034.ID='111000' AND SOBS063.T2='' ) t
SET t.OLD =t.NEW
To update a JOIN, Oracle needs to be absolutely sure that for each row of the table you are trying to update, there will be at most one row of the joined table.
Here you'll be able to update the join if and only if SOBS063.ID is unique (explicitely declared by a unique constraint/pk).
If somehow SOBS063.ID is unique for this record with your join condition but is not declared as such, you won't be able to use this method. You could however transform this DML into an equivalent MERGE, something like this:
MERGE INTO SOBS034 a
USING SOBS063 b
ON (a.id = b.id AND a.ID='111000' AND b.T2 IS NULL)
WHEN MATCHED THEN UPDATE SET a.t1 = b.t1;
By the way SOBS063.T2='' is never true in Oracle right now.
Try to create a unique index on SOBS034 (ID) and SOBS063 (ID).

Save many to many relationship in datamapper ORM - Codeigniter

I am a beginner. Here I would to ask all of you that how can I save many to many relationship in to the three tables(table A , table A-b , table B)? Now I am trying to save a new record with new ID into table A, and I have some IDs of table B that I want to save them to the middle table A-B depend on ID of table A. If anyone have experiences with this, please kindly share.
Example:
$a = new modelA();
$a->name = ‘new name’;
$a->des = ‘something to say’;
$b = new modelB();
$IDs = new array(1,2,3); //IDs of records in table B
$a->save(array($IDs=>$b));
Passing ID's is not supported, Datamapper needs objects to be able to relate.
If you have an array of ID's, you can fetch the objects using an where_in() query, and then save the relation using
$a->save($b->all);
I have some values which get from post form like:
'new name'
'new description'
array('val1','val2','val3')
in my controller I want to save a new record to table A with:
- 'new name'
- 'new description'
and array('val1','val2','val2') I got IDs of them from table B which has many to many with table A(a new record going to save).
So when I save into table A, the IDs of table B also save to middle table A-B with new ID of table A I just save. How to save?

Resources