0

I've recently started learning about creating EAV models. It's been quite the ride so far. Although I feel I'm very close right now.

Currently I have created a "Vendor" EAV Model, but when I try to save the model it throws an SQL 1452 Error:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`magento`.`dh_onsite_vendor_entity`, CONSTRAINT `FK_DH_ONSITE_VENDOR_ENTT_ATTR_SET_ID_EAV_ATTR_SET_ATTR_SET_ID` FOREIGN KEY (`attribute_set_id`) REFERENCES `eav_attribute_set` (`attribute_set_), query was: INSERT INTO `dh_onsite_vendor_entity` (`entity_type_id`, `created_at`, `updated_at`) VALUES (?, '2017-01-17 21:40:04', '2017-01-17 21:40:04')

What I've found to be the problem is that Magento isn't automatically adding the correct values into entity_type_id and attribute_set_id.

Here is code that throws the error:

$test = Mage::getModel('onsite/vendor');
$test->setLabel('Hello World');

$test->save();

Here is code that doesn't throw the error:

$test = Mage::getModel('onsite/vendor');
$test->setLabel('Hello World');
$test->setAttributeSetId(9);
$test->setEntityId(10);

$test->save();

I realized this because I was able to create a record by setting this myself.

Please let me know if you need anymore information from me.

0

your main entity table contains a FK to the attribute_set table.
And you cannot create a record in your table without a valid attribute_set_id.

You should be able to add this using the code below before saving a new entity instance.

$test->setAttributeSetId($test->getDefaultAttributeSetId());

if you implemented your eav entity similar to the core ones.

  • Thanks! Is there not a way to do this automatically? With the tutorial I was following they weren't required to set the AttributeSetId or EntityTypeId Or would this go into _beforeSave()? How can I set the default_attribute_set_id? – Ryan Cady Jan 17 '17 at 22:03
  • Take a look at this example: inchoo.net/magento/creating-an-eav-based-models-in-magento At the end they aren't required to manually set the attribute set id – Ryan Cady Jan 17 '17 at 22:16
  • I tried setting the default attribute set id, but that didn't work. Still receiving the same answer – Ryan Cady Jan 17 '17 at 22:36
0

After more digging in Magento's Core I found that some models were not setting attribute_set_id as a foreign key.

Now, this doesn't necessarily solve my problem. A solution would be to set the attribute_set_id to the default whenever saving, but I wasn't sure how to accomplish that and I've realized that it's not important for what I need.

So, this solution is working fine for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.