Magento Category Flat Tables - php

I have added to the category entity a lot of custom attributes, now when I try to reindex I get the error
SQLSTATE[42000]: Syntax error or access violation: 1118 Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. You have to change some columns to TEXT or BLOBs
I Know that means that the flat tables are too large and indeed there are a lot of columns there. I don't need my custom attributes in that table so I can remove them, but how can i do that? I found that if I set the filterable and comparable to false they shouldn't be in the flat table. Any help will be appreciated
public function getDefaultEntities()
{
return array(
'catalog_category' => array(
'entity_model' => 'catalog/category',
'attribute_model' => 'catalog/resource_eav_attribute',
'additional_attribute_table' => 'catalog/eav_attribute',
'entity_attribute_collection' => 'catalog/category_attribute_collection',
'table' => 'catalog/category',
'attributes' => array(
'cat_type' => array(
'group' => 'General',
'label' => 'Category Type',
'type' => 'int',
'input' => 'select',
'default' => '0',
'class' => '',
'backend' => '',
'frontend' => '',
'source' => 'eav/entity_attribute_source_cattype',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => true,
'required' => false,
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'visible_in_advanced_search' => false,
'unique' => false
),
Edit:
I have deleted the attributes and it works then I ran the install script again, all of them have the filterable and comparable set to false as the example I posted below, It shows me the error again, What I'm missing?
Edit:
I checked the flat category table and I see that all the attributes are added them.
So the filterable and comparable just works for product attributes?
I thought that they will be the same values for category products.
Is there anyway to exclude my attributes from that table?
I can exclude them modifying the file that create the table, obviously in my local folder but I want to know which is the clean way to do this?

Check eav_attribute table and column named used_in_product_listing set it to 0 or false and it shouldn't be added to category product flat table.

Related

Magento 2 Attribute Not Show In Navigation

I have a problem when applying magento 2.1.9 to my project.
My attribute is ab_size
I have created that attribute with code blow.
$categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
$entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
foreach ($singleAttributeCodes as $key => $label) {
$categorySetup->removeAttribute($entityTypeId, $key);
$categorySetup->addAttribute(
$entityTypeId,
$key,
[
'type' => 'varchar',
'label' => $label,
'input' => 'select',
'required' => false,
'sort_order' => $sortOrder,
'visible' => true,
'user_defined' => true,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'filterable' => 0,
'visible_on_front' => true,
'used_in_product_listing' => true,
'group' => $group,
'apply_to' => 'simple,configurable,bundle,downloadable,grouped',
]
);
}
With config in the backend.
Enable filter (with result) done
Category Is Anchor done
Reindex done
Enable Category flat done
Enable Product flat done
Reindex all data done
Clear cache done
Use magento clean doesn't have any extension done
Check with attribute color (done it show in navigation)
Product Price Show
Category Show
about my system information
CentOS 7.0
Litespeed
Php7.0
Magento CE 2.1.9
I have debugged that the product collection buckets return empty
I think that problem with creating attribute code, has anyone got the same problem.
Thank anyone have tips.
With select attribute we must use type of int..
I think you have messed your attributes with this code.
Looking at it, it removes all the attributes and adds them but as type varchar, which may not be applicable to all attributes.
I would suggest resetting your Magento database, and then add the attribute using code like the following.
As I do not know what you are planning to use this attribute, some of the settings bellow may not apply to your use case.
$categorySetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'ab_size',
[
'type' => 'varchar',
'backend' => '',
'frontend' => '',
'label' => 'AB Size',
'input' => 'select',
'class' => '',
'source' => '',
'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => 0,
'searchable' => false,
'filterable' => true,
'comparable' => false,
'visible_on_front' => true,
'used_in_product_listing' => true,
'unique' => false,
'apply_to' => ''
]
);

If I were adding multiple attributes to different entities in Magento, would I use a different setup model for each?

I've written a module that adds an attribute to the customer model, and it's worked fine.
I wanted to extend the sales/order_shipment model and add another attribute, so I just used the same reference that I had used for the installer for adding the new customer attribute.
I relied on Magento finding and installing the module a file with the following path and name app/code/local/[namespace]/[module_name]/sql/install-01.php
I've set the installer to the value of $this during the setup of the module initially:
$installer = $this;
$installer->startSetup();
I kept the same reference for the installer and just added a new field:
$installer->addAttribute('customer','sap_bp_id',
array(
'type' => 'varchar',
'label' => 'SAP Business One Business Partner ID',
'input' => 'text',
'required' => false,
'visible' => true,
'visible_on_front' => false,
'global' => 1,
'position' => 62,
)
);
$installer->addAttribute('shipment','sap_doc_num',
array(
'type' => 'varchar',
'label' => 'SAP Business One Document Number',
'input' => 'text',
'required' => false,
'visible' => true,
'visible_on_front' => false,
'global' => 1,
'position' => 62,
)
);
It's worked just fine, but I want to know this:
Is this the correct method for adding a new attribute for different models in the same installation file? Is there a better method? Have I missed anything?

Add source model to attribute in magento

I created an install script to add two fields to customer using the script below.
But I get this error.
Source model "" not found for attribute "dob_month"
Am I not defining the model in the first line? What does that actually do? What is the best way to fix this?
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttribute('customer', 'dob_month', array(
'label' => 'Month',
'type' => 'varchar',
'input' => 'dropdown',
'visible' => true,
'required' => true,
'position' => 1,
'option' => array (
'value' => array (
'optionone' => array('January'),
'optiontwo' => array('February'),
'optionthree' => array('March'),
'optionfour' => array('April'),
'optionfive' => array('May'),
'optionsix' => array('June'),
'optionseven' => array('July'),
'optioneight' => array('August'),
'optionnine' => array('September'),
'optionten' => array('October'),
'optioneleven' => array('November'),
'optiontwelve' => array('December')
)
)
));
$setup->addAttribute('customer', 'dob_year', array (
'label' => 'Year',
'type' => 'varchar',
'input' => 'text',
'visible' => true,
'required' => true,
'position' => 1
));
If you've already added the attribute, you'll want to use updateAttribute to set the source model value in the eav_attribute table.
<?php
$installer = Mage::getResourceModel('customer/setup','default_setup');
/***
* When working with EAV entities it's important to use their module's setup class.
* See Mage_Customer_Model_Resource_Setup::_prepareValues() to understand why.
*/
$installer->startSetup();
$installer->updateAttribute(
'customer',
'dob_month',
'source_model', //a.o.t. 'source'
'whatever/source_model',
)
$installer->endSetup();
If not, then you can use addAttribute(), which - due to the Mage_Eav setup class' _prepareValues() method - requires an alias for the source_model column as indicated in Alexei's answer ('source' as opposed to 'source_model').
Source model is used when Magento needs to know possible values of your attribute. For example, when rendering dropdown for your attribute. So no, you're not defining it. If my memory doesn't fail me, you can do that by adding 'source' to attribute definition array. Something like:
...
'source' => 'eav/entity_attribute_source_table'
...
That will mean that all your possible options are stored in tables eav_attribute_option and eav_attribute_option. So if your options from install script are successfully added to these tables, that should work. Or you can write your own source model, which I prefer more.

Defining multiple streams and adding fields to each of them in PyroCMS

I am new to PyroCMS and am willing to build a Job Site wherein there'll be 2 main users namely, Employers and Job Seekers. In order to allow them to register on the site, I'm using the Streams API from PyroCMS to build the forms. These users will be part of 2 different modules namely the Employer module and the Job Seeker module.
In the details.php file, under the install() function, I want to create multiple streams(database tables). The following code helps us to add a stream:
$this->streams->streams->add_stream();
The following code then helps us to define the fields to be added to the stream:
$this->streams->fields->add_fields($fields);
My concern is how do I add multiple streams like the above ones and add fields to each of them? In other words, how would the syntax
$this->streams->fields->add_fields($fields);
know which stream to add the fields to?
Have a look at the Fields Driver documentation for the Streams API. Fields and streams are separate entities, with no required association between the two. When adding a field you can assign it to a stream like this:
$field = array(
'name' => 'Question',
'slug' => 'question',
'namespace' => 'streams_sample',
'type' => 'text',
'extra' => array('max_length' => 200),
'assign' => 'STREAM_SLUG_GOES_HERE',
'title_column' => true,
'required' => true,
'unique' => true
);
$this->streams->fields->add_field($field);
Or you can create the streams and fields separately, and then assign each field to a stream like this:
$this->streams->fields->assign_field('streams_sample', 'STREAM_SLUG_GOES_HERE', 'question', array('required' => true));
All this talk of fields and streams makes me want to go outside...
You can add multiple streams like this example.
// Add banners streams
if ( ! $this->streams->streams->add_stream(lang('banner:banners'), 'banners', 'banner', 'banner_', null)) return false;
// Add groups streams
if ( ! $this->streams->streams->add_stream(lang('banner:groups'), 'groups', 'banner', 'banner_', null)) return false;
// Add some fields
$fields = array(
// BANNERS
array(
'name' => 'Banner Title',
'slug' => 'banner_title',
'namespace' => 'banner',
'assign' => 'banners',
'type' => 'text',
'extra' => array('max_length' => 200),
'title_column' => true,
'required' => true,
'unique' => true
),
// GROUPS
array(
'name' => 'Group Title',
'slug' => 'group_title',
'namespace' => 'banner',
'assign' => 'groups',
'type' => 'text',
'extra' => array('max_length' => 200),
'title_column' => true,
'required' => true,
'unique' => true
)
);
$this->streams->fields->add_fields($fields);

Magento adding attribute with option filterable (no results) through extension installation

I've searched around quite a bit for this answer and haven't been able to trace down the exact setting I need. I'm reaching out to see if anyone can help.
I'm writing a Magento extension to add some attributes to my installation. Everything is fine with the exception of one intricacy. I cannot set the attribute's "Use In Layered Navigation" property to "Filterable (no results)".
I can use the values in the attribute array in my installer file (below) to set that property to either "No" (0 value) or "Filterable (with results)" (1 value) but not without results.
Anyone have a suggestion of a property I may be missing or set incorrectly in my array?
Very much appreciated!
<?php
...
// Add the mm_framestyle attr. (filterable, non-super attr.)
$setup->addAttribute('catalog_product', 'mm_framestyle', array(
'backend' => 'eav/entity_attribute_backend_array',
'visible' => true,
'required' => false,
'user_defined' => true,
'searchable' => true,
'filterable' => true,
'comparable' => true,
'label' => 'Frame Types',
'group' => 'MyMaui Attributes',
'type' => 'varchar',
'input' => 'select',
'global' => false,
'option' => array (
'value' => array('maui_flex' => array('MAUI FLEX'),
'full_frame_metal' => array('FULL FRAME'),
'rimless_metal' => array('RIMLESS'),
'shields' => array('SHIELDS'),
)
),
'visible_on_front' => true,
'unique' => false
));
...
?>
To set the is_filterable property to "Filterable (no results)", your configuration array should have filterable set to 2.
If you wish to use an update script to change the previously-established setting, the syntax would be as follows:
$setup->updateAttribute('catalog_product', 'mm_framestyle', 'is_filterable', 2);

Categories