質問

これは、AWブログモジュールの一部です。 2つのテーブルがあります。

aw_blog
post_id | tags
1 tag1
2 tag2
3 tag3

aw_blog_post_cat
cat_id | post_id
1 1
1 3
2 3

私はそのSQLステートメントを持っています、そして私はMagento Wayでそれをやろうとしています:

SELECT `aw_blog`.`tags` FROM `aw_blog_post_cat` RIGHT JOIN  `aw_blog` on `aw_blog`.`post_id`=`aw_blog_post_cat`.`post_id` WHERE `aw_blog_post_cat`.`cat_id`=1

私はそのようなことを試しました:

$collection = Mage::getModel('blog/blog')->getCollection()
->getSelect()->joinRight(array("post_cat" => $this->getTable('aw_blog_post_cat')), 'main_table.post_id = post_cat.post_id', array()); 

それから リンクしかし、運はありません。実装されたモデルはありません aw_blog_post_cat テーブル

役に立ちましたか?

解決

Magentoのインストールにモジュールをインストールしていないため、テストしませんでしたが、ソースコードを確認しましたが、最新のコードかどうかはわかりません。ブログクラスのコレクションには、aw_blog_model_mysql4_blog_collectionカテゴリIDに基づいてコンテンツをフィルタリングするのに役立つメソッドがあります

class AW_Blog_Model_Mysql4_Blog_Collection
...
public function addCatFilter($catId)
{
    $this->getSelect()->join(
        array('cat_table' => $this->getTable('post_cat')),
        'main_table.post_id = cat_table.post_id',
        array()
    )
    ->where('cat_table.cat_id = ?', $catId);

    return $this;
}

次に、次のコードを使用できます。

$collection = Mage::getModel('blog/blog')->getCollection()
    ->addCatFilter(1);

$items = $collection->getItems();

だから今、あなたはすべての投稿アイテムを取得します。

他のヒント

Fishpigブログには素晴らしい記事があります カスタムクエリを書く。クエリリソースとタブネームを取得する方法を詳細に説明しています。

あなたの場合、それはこのようなものです

$resource = Mage::getSingleton('core/resource');
$read = $resource->getConnection('core_read');

$results = $read->fetchAll("SELECT * FROM {$resource->getTableName('blog/post_cat')}"); // change the query to your needs

var_dump($results);
ライセンス: CC-BY-SA帰属
所属していません magento.stackexchange
scroll top