質問
私は私のコントローラから私のモデルに変数を渡す方法を見つけようとしている最後の数日を過ごしました。私はいくつかのフォーム入力を取り、それらに基づいて製品コレクションを構築する非常に単純な製品フィルタを構築しようとしています。私は構築された形式を持っていて、私はAjaxを使っています:
jQuery.ajax(
{
url: formURL,
type: "POST",
data: {
location : location,
width : width
},
success: function() {
alert('form good');
},
error: function() {
alert('form issue');
}
});
.
コントローラに投稿する:
class Custom_GateSelector_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$gate_location = $this->getRequest()->getPost('location');
$gate_width = $this->getRequest()->getPost('width');
}
}
.
それから私のモデルで利用可能になる2つの変数が必要です:
class Custom_GateSelector_Model_Products extends Mage_Catalog_Model_Product
{
public function getItemsCollection()
{
$topStairs = 'yes';
$gateWidth = 29.00;
$rootcatID = Mage::app()->getStore()->getRootCategoryId();
$collection = $this->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('gate_max_width', array('gt' => $gateWidth))
->addAttributeToFilter('category_id', array('in' => $rootcatID))
->addAttributeToFilter('type_id', array('eq' => 'simple'))
->addAttributeToSort('price', 'DESC')
->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED));
return $collection;
}
}
.
Mage::register('gate-location', $gate_location);
メソッドを試しましたが、何らかの理由からモデルクラスでアクセスできません。
解決 3
だから私の髪を引き出す長い週の後、私はコントローラからモデルへの情報を渡す最善の方法がないことを認識しました。私が使用できることを実感させた無関係な質問に答えるためのBen @ Sonassiのおかげで私は
Mage::app()->getRequest()->getParam('location');
.
モデルのアクション内の投稿変数を取得して、必要なものにアクセスする。
他のヒント
だからあなたは基本的にあるクラスから別のクラスに変数を渡したいですか?
それがあなたが達成したいものなら、なぜそれらをメソッドパラメータとして渡さないのですか?getItemsCollection
は、任意の親クラスで定義されていない - その署名を自由に変更することができます。
public function getItemsCollection($gateLocation, $gateWidth)
{
// …
}
.
それを回避したい場合は、その後コレクションを変更することができます。
$collection = $gsProduct->getItemsCollection();
$collection->addAttributeToFilter('gate_max_width', array('gt' => $gateWidth))
. モデル
の場合このCustom_GateSelector_Model_Products
のindexAction()
その後、モデル上のパラメータを取得します。
$gate_location = Mage::app()->getRequest()->getPost('location');
$gate_width = Mage::app()->getRequest()->getPost('width');
.
Magentoシステムとのとおり、現在のコントローラを別のページに取得できない
所属していません magento.stackexchange