Magento - how are categories joined to stores, and to each other - magento

we are using Magento with multiple stores. Each store has categories starting with a root category. I understand that the store is a core_store entity, and category is a catalog_category_entity record, and that they are joined somehow in the EAV attributes table. But I have a few questions on this:
Category C is a subcategory of B is a subcategory of A, i.e. A > B > C - how are the relationships between A and B, and B and C stored?
How is the store joined in? Is there a join table, or is there a column allowing for the category to be listed in a table multiple times, each with a different value for the store field?

You can easily look at the tables and figure this out yourself.
In catalog_category_entity, the subcategories are linked to parent by the parent_id column. This column is also in the flat tables.
If you look in catalog_category_entity_varchar for the category name attribute for example, there's a store_id column. 0 is the default value, 1 is for store view 1, etc. All this means is that on the front end, if say Store View 1 is showing, then the value for the category name is the row in the table with store_id 1. For example, if your category name is "Hello" in Store View 1 English, then it might be "Hola" in Store View 2 Spanish, and so on.

Related

Return only the categories which have products (Elasticsearch)

I have two indexes Categories and Products. A product has only one category.
I am looking for a way to query the Category index and filter out all categories that have no products.
Product
id
name
category
id
name
price
Category
id
name
Any ideas on how I can achieve that?
You can run a terms aggregate on field category.id in Product index. Notice set size of the aggregate (NOT the global size to specify the return hits) a big number.

EF6 How to query where children contains all values of a list

Say I have a document table, with doc_id (PK) and doc_name fields, a category table with cat_id (PK) and cat_name fields, and a document_categories table with doc_id (PK, FK) and cat_id (PK, FK) fields, so I can attribute one or many categories to each document.
I have generated a model with EF6 in "Database first" mode, which gives me two entities: document and category, each containing a field which is a collection of children.
document contains a categories field which lists the categories of the document, and vice-versa in the category entity.
Now, I want to query all documents that contain category 1 AND category 2.
Let's say the database contains the following documents:
Doc A: Categories 1, 3
Doc B: Categories 1, 2
Doc C: Categories 1
Doc D: Categories 1, 2, 3
My query should return docs B and D.
How can I achieve that with EF6 using Linq?
Searched long on this site and in Google but found nothing for this particular request ... Thanks for your help
Use this:
var ids = new int[]{1,2};
var docs = context.Documents
.Where(d=> ids.All(cid=> d.Categories.Any(dc=>dc.cat_id == cid))).ToList();
Or
var ids = new int[]{1,2};
var result = from d in context.Documents
where ids.All(id => d.Categories.Any(dc=> dc.cat_id == id))
select s;

Set Include in Navigation Menu to Yes in database

I want to set Include in Navigation Menu for all categories to Yes.
Can anybody tell me in which table this value is?
Magento use an eav model to save values in the database.
You have to search an attribute called "include_in_menu" in the "eav_attribute" table.
This attribute have an "attribute_id" which will be retrieve in the other tables.
On my installation, this attribute has attribute_id = 67 which is stored as an INTEGER (int)
On magento all the attributes have a type which you can find on the eav_attribute table.
You want to update your categories, we have to update the table where the integer attribute "include_in_menu" of the categories is saved.
As you can see in your database, you have a lot of tables for the categories :
catalog_category_entity, catalog_category_entity_datetime, catalog_category_entity_decimal, catalog_category_entity_int...
You have to select all your categories from the first table : select entity_id from catalog_category_entity
After you have to select the attribute_id in the table related to the good attribute type. Here is "catalog_category_entity_int" for all the INTEGER attribute...
select value from catalog_category_entity_int where attribute_id = 67 and store_id = ...
Be careful if you have multiple stores...All the stores are on the same table.
And now you just have to update value to "1" on the selected rows.
Sorry for my English, i m french...
Best regards
Cédric
This answer worked on Magento EE 2.2.2.
Based on Cedric's answer I used the following query to remove all of the categories from the menu via Sequel Pro.
You can remove all of your categories from your menu by running:
UPDATE catalog_category_entity_int SET value='0' WHERE attribute_id='67'
You can add all of your categories to your menu by running:
UPDATE catalog_category_entity_int SET value='1' WHERE attribute_id='67'
Then flush your cache (php bin/magento cache:flush) and you should see your changes on the frontend.
Again, be careful of what 'store_id' you're affecting.
You can use following query to update all categories:
update catalog_category_entity_int cci
inner join eav_attribute a on a.attribute_id = cci.attribute_id
set cci.value = 1
where a.attribute_code = 'include_in_menu';

Count the number of rows in many to many relationships in Hibernate

I have three of many tables in Oracle (10g) database as listed below. I'm using Hibernate Tools 3.2.1.GA with Spring version 3.0.2.
Product - parent table
Colour - parent table
ProductColour - join table - references colourId and prodId of Colour and Product tables respectively
Where the ProductColour is a join table between Product and Colour. As the table names imply, there is a many-to-many relationship between Product and ProductColour. I think, the relationship in the database can easily be imagined and is clear with only this much information. Therefore, I'm not going to explore this relationship at length.
One entity (row) in Product is associated with any number entities in Colour and one entity (row) in Colour can also be associated with any number of entities in Product.
Let's say as for an example, I need to count the number of rows available in the Product table (regarding Hibernate), it can be done something like the following.
Object rowCount = session.createCriteria(Product.class)
.setProjection(Projections.rowCount()).uniqueResult();
What if I need to count the number of rows available in the ProductColour table? Since, it is a many-to-many relationship, it is mapped in the Product and the Colour entity classes (POJOs) with there respective java.util.Set and no direct POJO class for the ProductColour table is available. So the preceding row-counting statement doesn't seem to work in this scenario.
Is there a precise way to count the number of rows of such a join entity in Hibernate?
I think you should be able to do a JPQL or HQL along the lines.
SELECT count(p.colors) FROM Product AS p WHERE p.name = :name ... other search criteria etc
or
SELECT count(c.products) FROM Color AS c WHERE c.name = :name .... other search criteria
From Comment below, this should work:
Long colours=(Long) session.createQuery("select count(*) as cnt from Colour colour where colour.colourId in(select colours.colourId from Product product inner join product.colours colours where product.prodId=:prodId)").setParameter("prodId", prodId).uniqueResult();

Save many to many relationship in datamapper ORM - Codeigniter

I am a beginner. Here I would to ask all of you that how can I save many to many relationship in to the three tables(table A , table A-b , table B)? Now I am trying to save a new record with new ID into table A, and I have some IDs of table B that I want to save them to the middle table A-B depend on ID of table A. If anyone have experiences with this, please kindly share.
Example:
$a = new modelA();
$a->name = ‘new name’;
$a->des = ‘something to say’;
$b = new modelB();
$IDs = new array(1,2,3); //IDs of records in table B
$a->save(array($IDs=>$b));
Passing ID's is not supported, Datamapper needs objects to be able to relate.
If you have an array of ID's, you can fetch the objects using an where_in() query, and then save the relation using
$a->save($b->all);
I have some values which get from post form like:
'new name'
'new description'
array('val1','val2','val3')
in my controller I want to save a new record to table A with:
- 'new name'
- 'new description'
and array('val1','val2','val2') I got IDs of them from table B which has many to many with table A(a new record going to save).
So when I save into table A, the IDs of table B also save to middle table A-B with new ID of table A I just save. How to save?

Resources