where are category NAMES stored in magento? - magento

Seems like this should be a findable question but I could not locate it.
Where is the category NAME stored in the magento database? I can see catalog_category_entity has the key ID, and then there are the other EAV tables. But I cannot find the actual name of the category stored in any table prefixed by catalog_category.
At least with products the catalog_products_entity table has the SKU (some human-readable value) in it.

The categories names are in the table catalog_category_entity_varchar
The request you are looking for :
select cat.*, cv.value as `category_name` from `catalog_category_entity` as cat
join `catalog_category_entity_varchar` as cv on cat.entity_id = cv.`entity_id`
join `eav_attribute` as att on att.`attribute_id` = cv.`attribute_id`
join `eav_entity_type` as aty on att.`entity_type_id` = aty.`entity_type_id`
where aty.`entity_model` = 'catalog/category' and att.`attribute_code` = 'name' and cv.`store_id` = 0
Please note that this is the name of your categories for your default store view.
If you do have multiple store or store view in your magento you just have to adapt the condition cv.`store_id` = 0.
The list of your stores can be found in the table core_store

'name' is an EAV attribute of type VARCHAR so you can find the values for that attribute on the catalog_category_entity_varchar table, but it contains all values for attributes that are type VARCHAR, so to query for only the values for the name attribute you have to find what the attribute id is, which you can find in that eav_attributes table using something like:
SELECT * FROM `eav_attribute` WHERE `entity_type_id` = ##CATEGORY ENTITY TYPE ID## AND `attribute_code` = 'name'

If you have ID's with you then this works:
SELECT DISTINCT value, entity_id FROM catalog_category_entity_varchar WHERE entity_id IN (ID's of the category) AND attribute_id = 111 AND STORE = 0
Make sure to replace "ID's of the category" with ID's seperated by commmas and Store ID too. ) is default Store ID.

Related

How to get specific columns from relation table in laravel?

I am trying to fetch soecific columns data from relational table but it is giving me null
$allConsignments = Consignment::query();
$allConsignments->select(['id','customer_reference'])->with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get();
When I don't use select() then it gives correct data .
like this
$allConsignments = Consignment::query();
$allConsignments->with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get()
it is working but I also need specific columns from Consignment Table. what could be the reason?
You can also do like this.
$allConsignments = Consignment::query();
$allConsignments::with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get(['id','customer_reference']);
Actually, I also need to select the foreign key column from the table on which relationship is based. for example in my case I have customer_id in consignment table so it should be like that
$allConsignments = Consignment::query();
$allConsignments->select('id','customer_reference','customer_id')->with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get();
I need to select customer_id as well

Get list of joined rows with DAX

I have Supplier dimension table has 1:n relationship with InvoiceDetail fact table. I would like to get the list of active suppliers like below SQL, but in DAX language:
SELECT [Id]
,[Name]
,[Code]
,[CountryIso]
FROM [Supplier] s
WHERE EXISTS (SELECT 1 FROM [InvoiceDetail] id WHERE s.id = id.SupplierId)
I am not sure how I can do on Measure with DAX
Assuming that an active supplier means that the supplier has an invoice against them and that your data looks something like this..
Invoice Table
Supplier Table
Creating a relationship between the two tables will in effect, 'join' the two tables.
You can then use invoice number field from the invoice table and the name/code/countryiso from the supplier table.
Example being:
The value are only being drawn from the invoice table, so you'll only see active Invoices.
If being an active supplier means having a true bool value, join the tables and add a report/page wide filter on that bool value.

Updating a Magento product attribute's database field type after creation

The scenario: You've created a product attribute programmatically using a database migration. Several months later you'd like to change that attribute from a VARCHAR to a TEXT field type.
How do you change the field type of an EAV attribute after creation while preserving the data?
My gut feeling is that this isn't supported directly through Magento's setup classes, due to the myriad of tables that would need to be touched, records that would need to be updated and content that would need to be copied from table to table.
I'm not going to pretend this is the prettiest solution around, and I'm doubtful it's database agnostic, but here's my solution:
<?php
/** #var $this Mage_Eav_Model_Entity_Setup */
$this->startSetup();
$attribute_id = $this->getAttribute(
Mage_Catalog_Model_Product::ENTITY,
'your_attribute_code',
'attribute_id'
);;
if (!is_numeric($attribute_id)) {
Mage::throwException("Couldn't run migration: Unable to find attribute id");
}
/** #var Varien_Db_Adapter_Pdo_Mysql $connection */
$connection = $this->getConnection();
$connection->beginTransaction();
/**
* Copy the data from the VARCHAR table to the TEXT table.
*/
$connection->query("
INSERT INTO catalog_product_entity_text
(entity_type_id, attribute_id, store_id, entity_id, value)
SELECT
entity_type_id, attribute_id, store_id, entity_id, value
FROM catalog_product_entity_varchar
WHERE attribute_id = ?
",
array($attribute_id)
);
/**
* Update eav_attribute to use the text table instead of the varchar.
*/
$connection->query("UPDATE eav_attribute SET backend_type = 'text' WHERE attribute_id = ?", array($attribute_id));
/**
* Delete the attribute values from the VARCHAR table.
*/
$connection->query("DELETE FROM catalog_product_entity_varchar WHERE attribute_id = ?", array($attribute_id));
$connection->commit();
$this->endSetup();
Yeah I confirm it's not supported.
You could try to update tables with SQL but it will be a pain ...
I would export all your products, apply the upgrade script that modify your attribute backend table and re-import all your products.
That way magento will fill automatically the new table (catalog_product_entity_text) used by your attribute.
After that, you should clean your varchar table to delete unused values linked to your products (values that will be never deleted nor updated as your attribute's product is now TEXT)

Get primary key column of views

Is there way to retrieve view list along with primary key column name if that view is created with primary key column of dependent table?
E.g.:
Employee(ID PRIMARY KEY, FIRST NAME, LAST NAME, SALARY, DEPARTMENT)
The view derived from Employee table:
EMPLOYEEVIEW(ID, FIRST NAME, LAST NAME)
EMPLOYEEVIEW satisfies my constraint. I need to get these kind of views.
The desired result is something like EMPLOYEEVIEW ID.
To fetch the primary key constraints of the tables in the current schema, you can use this query:
select *
from user_constraints
where constraint_type = 'P'
so to search your view for primary keys I'd use a query like this
select *
from user_views v
join user_constraints c on upper(v.text) like '%'||c.table_name||'%'
where c.constraint_type = 'P'
and v.view_name = 'YOUR_VIEW_NAME'
Unfortunately the text field in the user_views view is of the horrible datatype LONG, so you will need to create your own function (or google one) to convert the LONG to VARCHAR, so you can use upper() and like on it.

How to populate column data in one table based on an id match from another table (using the data from the matched table)

In Oracle I have two tables. Both are populated with data and have a timestamp column.
One table has that column filled with data, the other table doesn't. But I need to get the data from the table that does into the column of the other table based on a match of another column.
Each has 'code' so the timestamp from the one table should only be put in the timestamp of the other table where the codes match.
I've tried cursors etc, but it seems like I'm missing something.
Any suggestions?
It sounds like you want a correlated update. This will update every row of destinationTable with the timestamp_col from sourceTable where there is a match on the code column.
UPDATE destinationTable d
SET timestamp_col = (SELECT s.timestamp_col
FROM sourceTable s
WHERE s.code = d.code )
WHERE EXISTS( SELECT 1
FROM sourceTable s
WHERE s.code = d.code )
Just for completeness, I think I would have done something like this (untested), if the destination has a primary key or unique column:
UPDATE (
SELECT d.primary_key AS pk, d.timestamp_col AS dest_col, MAX(s.timestamp_col) AS src_col
FROM dest d
JOIN src s ON d.code = s.code
GROUP BY pk, dest_col
) j
SET j.dest_col = j.src_col

Resources