Yii2: duplicate row in database with ActiveRecord - php

i develop the product module that add product info into product table, i add the button at the top of my page that copy the current product , i want to duplicate the row of that product ( known by id ) in product table , i user activerecord to duplicate data bud the id always set null and my script cannot duplicate data , my script is :
$product_original = Product::findOne($id);
$product_copy = new Product();
$product_copy->attributes = $product_original->attributes
$product_copy->isNewRecord = true;
$product_copy->id = null;
$product_copy->save();
after save product $product->save(); always return false, please help me to solve my problem , thank alot :)

You just need to copy attributes and that's it why are you assigning the $product_copy->isNewrecord and $product_copy->id=NULL;.
The only reason the record is not saved is due to the validation error before saving the record you can check for errors if not saved
Just use
$product_original = Product::findOne($id);
$product_copy = new Product();
$product_copy->attributes = $product_original->attributes;
if(!$product_copy->save()){
//either print errors or redirect
print_r($product_copy->getErrors());
}

Related

Laravel edit records

my problem is when I try to edit one of the records in my KeyController it not only edit record that I want but all the records connected by local_id. Variable $local is only for redirecting to previous page to show all the keys.
I want to change type, quantity and intercom password. id_klucza after request is correct.
public function potwierdzenie_edycji_kluczy(Request $request,$id_klucza)
{
$klucze = klucze::where('id_klucza','=', $id_klucza)->first();
$klucze->typ_klucza = $request->typ_klucza;
$lokal=$klucze->id_lokalu;
$klucze->ilosc_kluczy = $request->ilosc_kluczy;
$klucze->kod_domofon = $request->kod_domofon;
$klucze->save();
return redirect('lista_kluczy/'.$lokal);
}
Can you tell me what I'm doing wrong and what I can do to edit only one record I want no everything connected to it?

Laravel Crud Editing makes a new record

I have a problem with my Laravel crud application for Registrations.
There are these tables: Registration, ExtraRegistration (with a registration_id, extra_id and extraoptions_id),
Extra and ExtraOptions (with the Extra_id).
In the RegistrationController when i add a Registration it makes a new record in the ExtraRegistration with the extraoptions_id and the extra_id. the extra_id is the name of the option and the extraoptions_id is the id of the option you selected.
But now, when you click on edit a record, it shows all the information. the problem is that when you change the extraoption, it makes another record, and not change the select.
And when you have edited something and you look at it again, it still shows the option before you edited it.
RegistrationController
$options = Extra::where("exa_form_id", $distance->asd_form_id)->get();
foreach($options as $option){
$input_name = "option_" . $option->exa_id;
$input_option = $request->$input_name;
if(!is_null($input_option)){
$input_name_extra = "extraoptions_" . $option->exa_id;
$input_option_extra = $request->$input_name_extra;
$registrationextra = new ExtraRegistration();
$registrationextra->iea_registration_id = $registration->isg_id;
$registrationextra->iea_extra_id = $input_option;
$registrationextra->iea_extraoption_id = $input_option_extra;
$registrationextra->iea_price = $option->exa_price;
$registrationextra->save();
}
}
$registration->isg_options = $input_option;
$registration->isg_option_extra_id = $input_option_extra;
I want a check before it makes a new ExtraRegistration. that it only makes a new registration if the registration_id with that extra_id doesn't already exists. (Not 100% sure though).
Thanks in advance!
you make a new object of ExtraRegistration so its always make a new entry for update first get object of those id after that update
check the below link
https://laravel.com/docs/5.8/eloquent#updates
This happens because you're creating a new ExtraRegistration record:
$registrationextra = new ExtraRegistration();
If you want to update it, you need to find the related $registrationextra for your $options, and then update them (assuming you have relations set up):
$registrationextra = ExtraRegistration::where('options_id', $option->id);
$registrationextra->update([
'your_fields' => value
// etc...
]);
If you want to check if ExtraRegistration exists, and depending on that, create or update it, you can do something like this:
$registrationextra = App\ExtraRegistration::updateOrCreate(
['your_fields' => 'value'],
);
You can read more on official documentation.

How to update custom user fields on Drupal 8

I try to update/save a custom Field of User Profile on Drupal 8.
I am lucky to get Values but not to save them back.
Here is my Code, any1 knows why this won’t work?
I've tried different variations of these already.
$user = \Drupal\user\Entity\User::load(1); // Load USER of ID=1
$user->set(‚field_user_curpage‘,38); //set my custom field = 38 !!!
$user->save(); // save …
This is how I Load these fields:
$user = \Drupal\user\Entity\User::load(1); // Load user with id = 1
$curpage_load = $user->get(‚field_user_curpage‘); // Load custom field from User Profile
$curpage = preg_replace(‚/[^0-9]/‚, ‚‘, $curpage_load->value); // filter for No. only
return $curpage; // return output this
The Entity Load function returns a static object.
Try loading the user with this instead.
\Drupal::entityTypeManager()->getStorage('user')->load($id);

Magento duplicate attriubte set new attributes not showing in front end

Duplicated attriubte set for new product line.
Attributes set show in backend and front end.
Created new attribute and added to attribute set.
New attribute show's in back end but not front end.
If I add the new attribute to the origonal set that was duplicated the new attribute will now show on the front end for the product assoiated with the duplicated attribute group.
So basicly A new attribute added to a duplicate set won't show on the front end until its added to the attribute set that was duplicated.
I've checked to make sure the attribute is visable on front end etc and tried it several times checking the settings.
The goal is to be able to duplicate a attribute set and add new attributes for different product types. Then call the folder by id and display the assoiated attributes.
I've called the attribute group by ID (spec's). This code is working.
<?php
require_once 'app/Mage.php';
Mage::app();
$attributeGroupCollection = Mage::getResourceModel('eav/entity_attribute_group_collection');
$product = $this->getProduct();
foreach ($attributeGroupCollection as $attributeGroup) {
$attributeGroup->getAttributeGroupId();
$attributeSpecs = Mage::getResourceModel('eav/entity_attribute_collection')
->setAttributeGroupFilter(41);
}
?>
Help is appreciated, thanks
Duplicating an attribute set is duplicating at a time (the moment when you click the button). If you want to add an attribute in both attribute sets, you need to create it in both, for that, you just need to create your attribute and drop it in the section you want to have it linked to (what you call "folder"). If you want to do it programmatically. You need to create an observer that will be fired after the attribute set is saved and that will add the attribute to the duplicated attribute set. I would not advise to do so because it means that you need to enter the ID of the attribute set duplicated and it can be tricky thing when it comes to pushing the development to the production. I am sure there is a workaround for what you want to achieve.
The problem was is magento creates a new id for each new group folder in the new duplicated attribute set (makes sense). I was calling by ID for the group and as a result will only show attributes in the origional folder (weird) even if the product was assoicated with the new attribute set. What I did was get the current attribute set id and then sort out the attribute groups by name so even if the attribute sets are coppied as long as the folde has the name it will display in the custom loop displaying the attributes. Here is my working code:
$attributeSetModel = Mage::getModel("eav/entity_attribute_set");
$attributeSetModel->load($product->getAttributeSetId());
$attributeSetName = $attributeSetModel->getAttributeSetName();
$attributeSetID = $attributeSetModel->getAttributeSetID();
$groups = Mage::getModel('eav/entity_attribute_group')
->getResourceCollection()
->setAttributeSetFilter($attributeSetID)
->setSortOrder()
->load();
$attributeCodes = array();
foreach ($groups as $group) {
echo $groupName = $group->getAttributeGroupName();
$groupId = $group->getAttributeGroupId();
if (strpos($groupName , 'Specifications') !== false) {
echo 'true';
$specificationsNum = $groupId;
};
if (strpos($groupName , 'eatures') !== false) {
echo 'true';
$prodfeaturesNum = $groupId;
};
}
//echo $specifications;
$specifications = Mage::getResourceModel('eav/entity_attribute_collection')
->setAttributeGroupFilter($specificationsNum);
$prodfeatures = Mage::getResourceModel('eav/entity_attribute_collection')
->setAttributeGroupFilter($prodfeatures);

Magento set the new attribute value

I have created a new IMEI attribute of type textarea for all products, see from the image. Can anyone point out a function to update the its value. I have the code like the following.
$this belogns to Mage_Sales_Model_Order.
foreach ($this->getAllItems() as $item) {
$item->setImei('123');
$item->save();
echo $item->getImei();
}
I am getting 123 from the last statement but when I am viewing from admin. Its not changing there. Also in which table the attribute and value will be stored, So I can debug from there.
What class is $this->getAllItems() is it Mage_Catalog_Model_Product?
If it not Mage_Catalog_Model_Product then load the product by id and save the product
foreach ($this->getAllItems() as $item) {
$product = Mage::getModel('catalog/product')->load($item->getId() or $item->getProductId())
$product->setImei($product->getImei() . '123');
$product->save();
}
The values of catalog product attributes of type text are stored in the table catalog_product_entity_text. An SQL would be
select * from catalog_product_entity_text where attribute_id = {insert your attribute id} and entity_id = {insert your product id}
The query will return results for every store view in the system.
The reason why you do not see a change to the attribute in the backend is probably because the new value is set for a different website/store than the one loaded in the backend.
You are already using a correct way to set the attribute value assuming $item is of type Mage_Catalog_Model_Product:
$item->setImei('123');
$item->save();
heres how you do it differently: (color = attribute name, red = attribute value id)
starts assuming you already have $product available.
$attr = $product->getResource()->getAttribute('color');
if ($attr->usesSource()) {
$avid = $attr->getSource()->getOptionId('red');
$product->setData('color', $avid);
$product->save();
}

Categories