Magento 2: How Insert on Duplicate works in this case? - magento

I am using Magento 2.1.7. In my database table, I need to create a row / Update a row based on ItemNo and GroupNo group.
For example, I need to update the Qty based on ItemNo and GroupNo combination.If the combination doesn't exist, then a new row to be created.
How is this possible using Insertonduplicate in SQL query?

After creating your model and resource model, you can insert on duplicate using the load method (Although deprecated).
$itemNo = 1000;
$model = $this->modelFactory->create();
$model->load($itemNo, "ItemNo (Field in DB)");
$model->addData([
"Item No" => $itemNo,
"Group NO" => 20,
"Qty" => 300
]);
$saveData = $model->save();
if($saveData){
echo "data saved sucessfully";
}
This creates new row if itemNo does not exist in DB and update the row if it is there

Related

Yii2 activerecord not returning database trigger value

i just learned about YII2 framework, I'm just create a activerecord model. lets say Item and on table item i've just add a trigger before insert to add value a field. Im trying to test like this:
$item = new Item();
$item->name = 'item 1';
$item->save();
die(var_dump('<pre>', $item->attributes));
And trigger code something like this:
BEGIN
SET new.mycolumn = store_procedured_function();
END
And the dump result is
Array(
['id']=>
int(123)
['name']=>
string(6) "item 1"
['mycolumn']=>
NULL
);
but when i'm check to database mycolumn not null and id return value. why mycolumn not showing the value like id? And how to get mycolumn value after activerecord function save()?
For performance reasons Yii 2 does not automatically fetch attributes from database after save - in most cases it would not change anything, just create additional useless query. If you're relying on some database triggers, you may need to call refresh() after save().
$item->save();
$item->refresh();
var_dump($item->attributes);

Laravel 4.2 - Update a pivot by its own id

I'm having some trouble with my pivot table. I've recognized too late, that it is possible, that some pivot rows doesn't have unique values in my project, means I've to add an auto_increment ID field to my pivot table.
This is my structure:
Order.php
public function items()
{
return $this->belongsToMany('Item', 'orders_items', 'order_id', 'item_id')->withPivot(['single_price', 'hours', 'prov', 'nanny_id', 'vat', 'vat_perc', 'invoice_id','id']);
}
orders_items
id, order_id, item_id, nanny_id, hours
I've conntected Orders and Items through a pivot table ('orders_items)'. It is possible, that one order has 2 or more same items in the pivot table. So I've to add an unique ID to identify and update them.
Now I try to update a pivot row. Problem is, if I have 2 or more items, he updates them all, not only one. This is my update command:
$order = Order::find($orderId);
$items = $order->items()->whereNull('nanny_id');
$free_item = $items->first();
$free_item->pivot->nanny_id = 123;
$free_item->pivot->save();
With this command, he updates all pivot rows from the order. I know the problem: Laravel uses here the wrong identifiers (it uses order_id and item_id as defined in my belongsToMany relationship - and they aren't unique). For example, Laravel tries to execute this code on save():
UPDATE orders_items SET [...] WHERE order_id = 123 AND item_id = 2;
I want, that Laravel changes the query to this one:
UPDATE orders_items SET [...] WHERE order_id = 123 AND item_id = 2 AND id = 45;
// Edit
Okay, this solution works:
$free_item->pivot->where('id',$free_item->pivot->id)->update('nanny_id',123);
But is there an easier way, f.e. adding a custom pivot model that adds the id automatically to save() and update() methods?

Insert and delete from "associations table on EF model"

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);

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?

Adding auto increment column in table in codeigniter

I am new to codeigniter. In my project I am getting some data from database and then showing them using codeigniter table->generate method. But I want to add an autoincrement column (like 1,2,3..) to show the row number in an additional column at the left of the table. Also, beside the number there will be checkbox to mark the row. Can anybody give me idea how can I do it in codeigniter?
You could add a count to your SQL query so that it is returned along with your results:
SET #n=0; SELECT #n := #n+1, * FROM example_table
Alternatively you could add the count to each result in PHP:
$count = 1;
foreach($results as $key => &$result){
array_push($result, $count);
$count++;
}
echo $this->table->generate($results);

Resources