I am a newbie for magento 2 and try to save data into custom table when user submit a product data in backend.
I tried to use di.xml
and events.xml
, but I still don't know when and where do this action insert the data into table.
Because there is basic on the exist page that is product submit page, not a new page to do it.
So, I am confused the data flow.
Updated:
I used the events.xml to do it. but not working for me. there is the file code:
Demo\User\etc\adminhtml\events.xml:
<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_catalog_product_save_entity_after'">
<observer name="demo_user_product" instance="Demo\User\Model\Product" method="testEvent" />
</event>
</config>
Demo\User\Model\Product:
<?php
namespace Demo\User\Model;
use Magento\Framework\Model\AbstractModel as AbstractModel;
class Product extends AbstractModel
{
public function testEvent()
{
$myfile = fopen("/file_path/Product.txt", "w");
fwrite($myfile, 1);
fclose($myfile);
}
}
when I click the save button of product in backend, there is no data is written into "Product.txt" file. this file's permission is 777. so, anybody know what's wrong in my code?
method
attribute is not valid (check magento/framework/Event/etc/events.xsd:48) An observer must implements\Magento\Framework\Event\ObserverInterface
, so you need to defineexecute
method – bchatard Mar 10 '16 at 8:06<event name="controller_action_catalog_product_save_entity_after">
(a quote) – bchatard Mar 10 '16 at 8:36