how to add category by UpgradeData in magento2 - magento2

if I want add a new Category in magento2 by UpgradeData.php,how could i do.only need to add a new record in table catalog_category_entity?but I didn't found the Category name in this table.

Inject Magento\Catalog\Model\CategoryFactory and Magento\Catalog\Model\CategoryRepository into your UpgradeData class:
public function __construct(
\Magento\Catalog\Model\CategoryFactory $categoryFactory,
\Magento\Catalog\Model\CategoryRepository $categoryRepository
) {
$this->categoryFactory = $categoryFactory;
$this->categoryRepository = $categoryRepository;
}
Create a new category instance and set the required data:
$category = $this->categoryFactory->create();
$category->setData('name', 'CATEGORY_NAME');
[...] // set other category data here
Save the new category:
$this->categoryRepository->save($category)

Related

I have made a category and want to make sub category, how can I add dynamically?

I made a form in which I have titlte, image, pdf, content and category and all are working fine, but I want to add submenu in my form and also want to make this dynamic so can show all sub-menu on my website.
You must have to intruduce a key parent_id with categories table.
And in category model do create the relation like this.
class Category extends Model
{
public $fillable = ['title','parent_id'];
/**
* Get the index name for the model.
*
* #return string
*/
public function childs() {
return $this->hasMany('App\Category','parent_id','id') ;
}
}
Here you can find the complete example..https://itsolutionstuff.com/post/laravel-5-category-treeview-hierarchical-structure-example-with-demoexample.html
Hopefully this could help you.

In Magento 2, How can i get current product id via custom module at admin panel

In Magento 2.2.5, how can i get current product id at admin panel ?
Actually i have created a module at product edit page admin panel, There i wants to get current product id like frontend we use $this->_registry->registry('current_product');
Please suggest me correct script.
You can basically do it the same way as you already do on frontend. In your Block Class, include registry and then use it in your method, like this:
namespace Vendor\Module\Block\Adminhtml\...;
class Dummy
{
protected $_coreRegistry = null;
public function __construct(
...
\Magento\Framework\Registry $registry,
...
)
{
...
$this->_registry = $registry;
...
}
public function dummyfunction()
{
$_product_id = $this->_registry->registry('product'))->getId();
}
}
Alternatively use object manager, like this:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');
$_product_id = $product->getId();

Is there a way to create objects/ table entries during the extension installation in TYPO3?

I want to create objects during the installation of an extension. For example I have the following two simple domain models:
class Product extends AbstractEntity
{
protected $name = '';
protected $sku = '';
...
}
class Location extends AbstractEntity
{
protected $name = '';
protected $city = '';
...
}
and a third domain model like:
class Mapper extends AbstractEntity
{
protected $domainModelName = '';
protected $domainModelProperty = '';
}
Now I want too add entries like this:
domain_model_name | domain_model_property
Product | name
Product | sku
Location | city
....
during the extension installation or directly after the installation, so that the tx_foxexample_domain_model_mapper table will be filled automatically, is this possible?
I know that I can use a initializeAction, but then the entries will only be generated if I add a plugin and visit the page etc., but I want that the entries/ objects already exists before I use a plugin or add some objects.
You can store your static data in the file ext_tables_static+adt.sql which must be located in the root folder of your extension.
According to the TYPO3 API, you must should use the following command to export your static data
mysqldump --password=[password] [database name] [tablename] --add-drop-table > ./ext_tables_static.sql
Also make sure, that the table structure of static tables is present in the ext_tables.sql file.
The extension static_info_tables makes use of this technique. You can have a look at the extension here for more details.

Silverstripe DataObject - drag and drop ordering

Silverstripe DataObject - I want to add drag and drop ordering to a current Class that extends dataobject like what exists for pages. So when dropped it updates an OrderBy field for all the dataobjects in that view. I created the class and can freely edit one item at a time, but a simple drag and drop ordering would make it so much easier but I can not see any such extensions currently on Dataobjects only on Pages.
In SilverStripe 3.1 there are a few excellent modules that give you this sort of functionality. Two of these modules are SortableGridField and GridFieldExtensions.
To get this working you need to install one of these modules, add a sort field to your custom DataObject class and add the module sort object component to your GridFieldConfig.
SortableGridField
The SortableGridField module is specifically to allow sorting functionality for objects on a GridField.
To get this working you need to add a sort field to your custom DataObject class and add GridFieldSortableRows() as a component to your GridField.
For the following examples I will use HomePage as the page with a has_many relationship to a Slide DataObject.
Slide
class Slide extends DataObject
{
private static $db = array (
'Title' => 'HTMLText',
'SortOrder' => 'Int'
);
private static $has_one = array (
'HomePage' => 'HomePage'
);
private static $summary_fields = array(
'Title' => 'Title'
);
private static $default_sort = 'SortOrder ASC';
private static $singular_name = 'Slide';
private static $plural_name = 'Slides';
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->removeByName('SortOrder');
return $fields;
}
}
HomePage
class HomePage extends Page {
private static $has_many = array (
'Slides' => 'Slide'
);
public function getCMSFields()
{
$fields = parent::getCMSFields();
$slidesFieldConfig = GridFieldConfig_RecordEditor::create();
$slidesFieldConfig->addComponent(new GridFieldSortableRows('SortOrder'));
$slidesField = GridField::create(
'Slides',
'Slide',
$this->Slides(),
$slidesFieldConfig
);
$fields->addFieldToTab('Root.Slides', $slidesField);
return $fields;
}
}
GridFieldExtensions
The GridFieldExtensions module contains GridFieldOrderableRows to control the sort order on a GridField, just like the SortableGridField module. It also has other useful GridField tools.
To get this working you need to add a sort field to your custom DataObject class and add GridFieldOrderableRows() as a component to your GridField.
Your code would be just like the above example except the component you add to your GridFieldConfig is GridFieldOrderableRows:
public function getCMSFields()
{
$fields = parent::getCMSFields();
$slidesFieldConfig = GridFieldConfig_RecordEditor::create();
$slidesFieldConfig->addComponent(new GridFieldOrderableRows('SortOrder'));
...
}

Using ListBoxFor in ASP.NET MVC 2

I am trying to update the Roles a specific group has in my application. The Group model I use in my view has an additional AllRoles IEnumerable attached to it, so that in my view I can do something like this:
<%: Html.ListBoxFor( model => model.aspnet_Roles, new MultiSelectList( Model.AllRoles, "RoleId", "RoleName" ), new { #class = "multiselect" } )%>
This generates a multiple select drop down as expected. However, coming form PHP, I noticed that the name of the select was without square brackets, maybe that is OK in ASP.NET but in PHP it is wrong.
Now, how do I go about updating the group after submiting the form, more precisely, how can I read the multiselct selected values. What I need is that based on the RoleIds that I receive to Add respective aspnet_Roles to my Group model.
Trying to read the received values using HttpContext.Request.Form["aspnet_Roles"] failed and is also ugly. Can I somehow use the model to fetch the needed data? Controller function:
[AcceptVerbs( HttpVerbs.Post )]
public ActionResult Edit( SYSGroups updatedGroup ) {}
Thanks
The selected ids will be sent as a collection:
[HttpPost]
public ActionResult Edit(string[] aspnet_Roles)
{
// the aspnet_Roles array will contain the ids of the selected elements
return View();
}
If the form contains other elements that need to be posted you could update your model:
public class SYSGroups
{
public string[] Aspnet_Roles { get; set; }
... some other properties
}
and have your action method look like this:
[HttpPost]
public ActionResult Edit(SYSGroups updatedGroup)
{
// updatedGroup.Aspnet_Roles will contain an array of all the RoleIds
// selected in the multiselect list.
return View();
}