プライマリナビゲーションメニューにすべてのカテゴリとそのサブカテゴリを表示したいとします。 私はcateoryにカーソルを合わせると、そのサブカテゴリが表示されます。 foreachループでMage :: getModel( 'catalog/category')をロードせずにこの機能を実装したいと思います。あなたは、カテゴリのツリー構造を作成するコードの下PHTMLファイルの使用中のコードを書きたいとforeachループでモデルをロードせずにすべてのカテゴリとサブキャリブレーションを取得
答えて
は
<?php
require_once("app/Mage.php");
Mage::app();
function getChildCategories($category, $First = false) {
$sub = $First ? $category : $category->getChildren();
foreach ($sub as $child) {
$_categories[] = [ "name" => $child->getName(), "id" => $child->getId(), "children" => getChildCategories($child) ];
}
return $_categories;
};
function CategoriesTree($category, $First = false) {
$sub = $First ? $category : $category->getChildren();
echo "<pre>";
foreach ($sub as $child) {
echo $child->getName(); ;
CategoriesTree($child);
}
}
$_categories = Mage::helper('catalog/category')->getStoreCategories();
$categories = getChildCategories($_categories, true);
CategoriesTree($_categories, true);
?>
このコードを試してみてください。
とcssとHTMLを使用すると、メインメニューのホバー上にサブメニューを表示するという目標を達成できます。
他のヘルプが必要な場合は、まだ私に教えてください。私はこれをしなかったし、私は誰もがこれを達成するためにどのように思っていたかどうかを確認するために、検索したい考え出して
おかげ
OK。これにトリックは、このカテゴリのモデルは、それが実際に何をしているのかを見てみましょうロードしない
Mage::getResourceSingleton('catalog/category')->getRawAttributeValue($categoryEntityId,array('name','level','url_key','path','is_active'),Mage::app()->getStore());
です。アプリ/コード/コア/メイジ/カタログ/モデル/リソース/抽象
へゴー
public function getAttributeRawValue($entityId, $attribute, $store)
{
if (!$entityId || empty($attribute)) {
return false;
}
if (!is_array($attribute)) {
$attribute = array($attribute);
}
$attributesData = array();
$staticAttributes = array();
$typedAttributes = array();
$staticTable = null;
$adapter = $this->_getReadAdapter();
foreach ($attribute as $_attribute) {
/* @var $attribute Mage_Catalog_Model_Entity_Attribute */
$_attribute = $this->getAttribute($_attribute);
if (!$_attribute) {
continue;
}
$attributeCode = $_attribute->getAttributeCode();
$attrTable = $_attribute->getBackend()->getTable();
$isStatic = $_attribute->getBackend()->isStatic();
if ($isStatic) {
$staticAttributes[] = $attributeCode;
$staticTable = $attrTable;
} else {
/**
* That structure needed to avoid farther sql joins for getting attribute's code by id
*/
$typedAttributes[$attrTable][$_attribute->getId()] = $attributeCode;
}
}
/**
* Collecting static attributes
*/
if ($staticAttributes) {
$select = $adapter->select()->from($staticTable, $staticAttributes)
->where($this->getEntityIdField() . ' = :entity_id');
$attributesData = $adapter->fetchRow($select, array('entity_id' => $entityId));
}
/**
* Collecting typed attributes, performing separate SQL query for each attribute type table
*/
if ($store instanceof Mage_Core_Model_Store) {
$store = $store->getId();
}
$store = (int)$store;
if ($typedAttributes) {
foreach ($typedAttributes as $table => $_attributes) {
$select = $adapter->select()
->from(array('default_value' => $table), array('attribute_id'))
->where('default_value.attribute_id IN (?)', array_keys($_attributes))
->where('default_value.entity_type_id = :entity_type_id')
->where('default_value.entity_id = :entity_id')
->where('default_value.store_id = ?', 0);
$bind = array(
'entity_type_id' => $this->getTypeId(),
'entity_id' => $entityId,
);
if ($store != $this->getDefaultStoreId()) {
$valueExpr = $adapter->getCheckSql('store_value.value IS NULL',
'default_value.value', 'store_value.value');
$joinCondition = array(
$adapter->quoteInto('store_value.attribute_id IN (?)', array_keys($_attributes)),
'store_value.entity_type_id = :entity_type_id',
'store_value.entity_id = :entity_id',
'store_value.store_id = :store_id',
);
$select->joinLeft(
array('store_value' => $table),
implode(' AND ', $joinCondition),
array('attr_value' => $valueExpr)
);
$bind['store_id'] = $store;
} else {
$select->columns(array('attr_value' => 'value'), 'default_value');
}
$result = $adapter->fetchPairs($select, $bind);
foreach ($result as $attrId => $value) {
$attrCode = $typedAttributes[$table][$attrId];
$attributesData[$attrCode] = $value;
}
}
}
if (sizeof($attributesData) == 1) {
$_data = each($attributesData);
$attributesData = $_data[1];
}
return $attributesData ? $attributesData : false;
}
あなただけの情報の特定の部分を取り出す起こって何のモデルの読み込みを見ることができないとして。また、抽象リソースの一部であるということは、すべてのカタログリソースモデル(私は他のリソースモデルをチェックしていないが、他のリソースモデルもこれを見つけるのに驚くことではない)を利用できることを意味する。
これをMage_Catalog_Block_Navigationのオーバーライドで使用すると、モデルを読み込まなくても、カテゴリについて必要なすべての情報を呼び出すことができます。しかし、木を横断するには、ひどいことをする必要があります。
「パス」(/で展開)を使用すると簡単に親を取得できますが、子カテゴリを取得するためには子カテゴリを取得するためにはダーティにする必要があります。
$childrenQuery = "SELECT entity_id FROM catalog_category_entity WHERE path REGEXP '^.*\/" . $categoryId . "\/[[:digit:]]?[[:digit:]]?[[:digit:]]?[[:digit:]]?$'";
$resource = Mage::getSingleton('core/resource');
$readCxn = $resource->getConnection('core/read');
$children = $readCxn->fetchAll($childrenQuery);
if ($children[0]) {
return $children;
} else {
return;
}
全体的な難しさは、すべてのモデルとリソース・モデルの機能はそれらすべてちょうどENTITY_IDとの仕事は間違いなくちょうど痛みが可能であることを確認するために、カテゴリオブジェクトのインスタンスを期待するということです。
私はこれをやった唯一の理由は、私の場合のデフォルトのマゼンタのルートカテゴリがカテゴリの実際の機能的ルート(fun eh)ではなかったからです。標準のルートカテゴリを使用している場合は、ヘルパー関数を使用してキャッシュから情報を取得することをお勧めします。
ここからは、Mage_Catalog_Block_Navigationで関数を完成させ、テンプレートにメニューを組み立てるしかありません。そして、そこに行く。モデル - >ロードにアクセスすることなくカテゴリメニューを完成させることができます。
- 1. Type.GetMethodは、すべてのメソッドをループせずにジェネリックMETHODINFOを取得
- 2. SQLカテゴリ - すべてのparent_id名を取得できません
- 3. すべての記事をタグとカテゴリで取得する
- 4. すべてのリンクを取得せずにリンクテーブルを取得できますか?
- 5. foreachループ内でjquery foreach内の値を取得する方法
- 6. のForeachループ - foreachループですべてまたは何も
- 7. foreachを使用せずにstdクラスの値を取得
- 8. ビットマップの幅と高さをメモリにロードせずに取得
- 9. foreachループの項目の値を取得できません
- 10. すべての値を取得せずにカーソルを完了
- 11. バックボーンコレクションのすべてのモデルを取得
- 12. カテゴリとすべてのサブカテゴリからすべてのページを取得
- 13. MongoMapper:すべてのモデルを取得
- 14. すべてのリストを取得せずにサイトコレクションのすべてのサイトを取得しますか?
- 15. がハードコーディングせずに、すべての「LI」を取得し、それ
- 16. どのようにすべてのカテゴリとサブカテゴリを取得できますか?
- 17. リソースにロードせずにPHAssetのファイルサイズを取得しますか?
- 18. foreachループから各配列項目をテーブルデータとして取得
- 19. PythonでIndexErrorを取得せずにループする方法は?
- 20. Wordpressでグローバルからすべてのカテゴリを取得
- 21. SQLクエリを使用してすべてのMagentoカテゴリを取得
- 22. モジュール情報をロードせずに取得するには?
- 23. PHPのループの外でforeachループ変数の値を取得するには?
- 24. ロードされたモデルからすべてのオブジェクトのリストを取得
- 25. 小枝:ループせずにサブアレイのキーを取得しますか?
- 26. Active Directory:すべてのユーザー/グループをロードせずに、ドメイン内のユーザー数/グループ数を取得しますか?
- 27. foreachループ経由でFileInfoを取得するには?
- 28. foreachをループするたびに次の変数を取得
- 29. 他のすべてのディレクトリとサブディレクトリを取得せずにSubversion外部を取得する方法
- 30. ファイル内のすべてのデータをロードせずにJSON編集
このコードはどこに書きますか?私はphtmlファイルにコードを書いています。 – gt06