magento 常用的函數,magento常用函數_PHP教程

來源:互聯網
上載者:User

magento 常用的函數,magento常用函數


1.Magento eav_attribute表中source如何指定自訂資料來源
如果你引用的類名為yebihai_usermanage_model_entity_school你必須完整的給出地址,不能usermanage/entity_school,這樣預設是在Mage下面去找的。
如:

$setup->addAttribute('customer', 'school', array('type' => 'int','input' => 'select','label' => 'School','global' => 1,'visible' => 1,'required' => 0,'user_defined' => 1,'default' => '0','visible_on_front' => 1,'source'=> 'yebihai_usermanage_model_entity_school', //資料來源,text留空即可));


2.Magento getPrice()的結果小數點位元的處理
echo Mage::helper('core')->currency($_product->getPrice());
輸出格式:888.673 => 888.67

3.Magento config.xml中global節點中的block重寫與blocks下面的命名標籤必須小寫,如:


Yebihai_CategoryList_Block_Category_View Yebihai_CategoryList_Block


4.Magento擷取列表當前排序方式ASC or DESC?
擷取當前排序:$this->getAvailableOrders()
擷取當前分頁:$this->getCurrentPage()
列表頁的各種內容擷取都在:Mage_Catalog_Block_Product_List_Toolbar這個類裡面,有需要直接去這裡面找。

5.Magento Collection添加排序?

$subCategories = Mage::getModel('catalog/category')->getCollection();$subCategories-> setOrder('position', 'ASC');


6.Magento Collection where裡面的或條件如何??

$result = $selfread->select()->from( $table, array('id')) ->where( 'reid = '.$reid,'topid ='.$reid);//reid=$reid 或 topid=$reid


7.Magento操作某條資料下面的多個欄位,使用情境如下:
本人在做訂單備忘的時候在監聽類裡面通過Magento系統的addStatusHistoryComment方法把訂單內容能成功寫入sales_flat_order_status_history表,但是我的需求是還要修改is_visible_on_front此欄位的值,讓內容在前台可見。頭痛的問題來了,想了各種方法最後按下面的方式解決了。
監聽類全部源碼:

class Yebihai_CustomerOrderComments_Model_Observer {public function setCustomerOrderComments(Varien_Event_Observer $observer){$_order = $observer->getEvent()->getOrder();$_request = Mage::app()->getRequest();$_comments = strip_tags($_request->getParam('customerOrderComments'));if(!empty($_comments)){$_order->setCustomerNote('
訂單備忘: ' .$_comments);$_order->addStatusHistoryComment('訂單備忘: ' .$_comments)->setIsVisibleOnFront(1);}return $this;}}

8.Magento個人中心左側菜單控制
關於個人中心的主要功能都是在customer這個模組進行,需要修改相應的功能,直接去你的模板customer檔案夾去修改。
左側菜單模板路徑:customer/account/navigation.phtml
9.Magento把html轉換為逸出字元,用什麼方法?
core助手裡面有一個escapeHtml方法,使用如下:
Mage::helper('core')->escapeHtml("yebihai加油go");
方法實際位置:Mage_Core_Helper_Abstract這個類裡面。
ps:關於一些常用的操作方法都封裝在core這個模組,大家有需要可以分析一下源碼。

10.Magento動態建立block並且引用action?

下面是我一個模組的布局(Layout)設定檔,我現在需要通過Ajax動態調用checkoutcart,直接調用肯定是不行的,改如何解決乃?

simplecheckout/cart_item_rendererquickbuy/cart/item/item_view.phtml
第一步:通過ajax調用一個自訂控制器,如:
jQuery.post('getUrl('gouwuche/cart/updateQuickShoppingCar') ?>', function(data){jQuery('#shopping-cart-table thead').after(data);});第二步:在控制器的方法中動態建立block,如:public function updateQuickShoppingCarAction(){$block = $this->getLayout()->createBlock('checkoutrewrite/quickcart')->setTemplate('quickbuy/cart/cart.phtml');echo $block->toHtml();}
第三步:建立一個block檔案(quickcart),在這檔案中的construct方法中初始化設定檔中的action內容,如:
public function __construct(){parent::__construct();$this->addItemRender('simple', 'checkout/cart_item_renderer', 'quickbuy/cart/item/item_view.phtml');}

PS:在進行第二步的時候,cart.phtml模板已載入完成,第三步只是為了載入cart block下面的action。

11. Magento getTable方法參數注意那些事項?
執行個體,查詢資料庫指定表和條件的方法如下:

public function getExcelKucunJiage($id,$status){$selfread = $this->_getConnection('excelmanage_read');$table = $this->getTable('excelmanage/excelkucunjiage');$result = $selfread->select()->from( $table )->where( 'excel_id=?', $id)->where( 'is_active=?', $status);return $selfread->fetchRow($result);}

其中getTable方法的參數設定需要注意如下,excelmanage就是你的模組名稱,excelkucunjiage這個就是你操作的實體節點名稱,我的實體配置如下:

Yebihai_ExcelManage_Model_Resource_Mysql4 excelkucunjiage

“/”後面的參數就是來源於表前面的實體名稱。

12.如何更新資料表指定ID資訊?
$excelModel = Mage::getModel('excelmanage/excelkucunjiage')->load(1);
$excelModel->setExcelAdddate(Mage::getModel('core/date')->timestamp(time()));
$excelModel->setIsActive(0);
$excelModel->save();
上面的代碼就是修改ID為1的資料表資訊。
擴充:Magento如何添加修改指定表資訊?

13.如何更改產品列表預設排序欄位?
設定路徑在:系統-->目錄-->進階產品管理-->預設列表狀態

14.擷取一個資料集的條數?

  擷取_productCollection資料集條數,案例如下:

  $this->setStoreId($storeId);

  $this->_productCollection = Mage::getResourceModel('catalog/product_collection'); //擷取資料集

  $this->_productCollection = $this->_productCollection->addAttributeToSelect('*')

  ->addAttributeToSelect('manufacturer') //添加查詢屬性

  ->setStoreId($storeId) //設定商店

  ->addAttributeToFilter('cuxiaobiaoqian',array('eq'=>39)) //屬性過濾指定

  ->addStoreFilter($storeId) //添加商店過濾條件

  ->setPageSize(6); //擷取條數

15. 通過select()方法查詢指定資料表,如何控制讀取條數?

  代碼應用背景如下:

  $selfread = $this->_getConnection('yafo_bbs_setup'); //資料庫連接對象

  $table = $this->zixunTablePrefix."forum_post"; //待查詢表

  $result = $selfread->select()->from( array('a'=>$table), array('tid','subject')) //指定表和要查詢的欄位

  ->limit($size) //讀取指定條數

  ->order("a.dateline DESC") //指定排序條件

  ->where( $selfwhere ); //添加篩選條件

  return $selfread->fetchAll($result); //返回查詢結果

16.修改指定產品價格和分組價格(代碼操作)?

$selfPrc = Mage::getModel('catalog/product')->load(614);$selfData = $selfPrc->getData();$selfData['price'] = 25;$selfData['group_price'] = array(0 => Array( "website_id" => 0, "all_groups" => 0, "cust_group" => 0, "price" => 97.0000, "website_price" => 97.0000 ), 1=> Array ( "website_id" => 0, "all_groups" => 0, "cust_group" => 1, "price" => 27.0000, "website_price" => 27.0000 ), 2=> Array ( "website_id" => 0, "all_groups" => 0, "cust_group" => 2, "price" => 17.0000, "website_price" => 17.0000 ), 3=> Array ( "website_id" => 0, "all_groups" => 0, "cust_group" => 3, "price" => 67.0000, "website_price" => 67.0000 ), 4=> Array ( "website_id" => 0, "all_groups" => 0, "cust_group" => 4, "price" => 66.0000, "website_price" => 66.0000 ));$selfPrc->setData($selfData);$selfPrc->save();


17.修改指定產品庫存(代碼操作)?

$selfPrc = Mage::getModel('catalog/product')->load(614);$aa = Mage::getModel("cataloginventory/stock_item")->loadByProduct($selfPrc);$selfaa = $aa->getData();$selfaa['qty'] = 23;$aa->setData($selfaa);$aa->save();


18.如何輸出sql語句
$result = $selfread->select()->from(array('ft'=>$flatTable),array())

->join(array('pc'=>$prcCategory),'ft.entity_id=pc.entity_id',array('pc.value'))->where( 'ft.attribute_set_id=?', $attsetid)->where( 'pc.attribute_id=?', $attid)->group("pc.value");// echo $result; exit;//輸出sql語句


19.後台表單配置,如何在代碼裡面添加備忘?

$fieldset->addField('dict_grade', 'select', array('name' => 'dict_grade','label' => Mage::helper('catalogsearchrewrite')->__('Advanced Search Ciku Manage Grade'),'title' => Mage::helper('catalogsearchrewrite')->__('Advanced Search Ciku Manage Grade'),'type' => 'options','options' => Mage::getSingleton('catalogsearchrewrite/cikumanage')->getCikuGradeOptionArray(),'after_element_html' => Mage::helper('catalogsearchrewrite')->__('Keywords Grade Description.'), //after_element_html此屬性就是用來添加備忘'required' => true,));


20.執行個體化model,通過load方法如何擷取指定欄位指定內容的值?
$dictModel=Mage::getModel('catalogsearchrewrite/cikumanage')->load($dictname,'dict_name'); //參數1:指定值,參數2:指定欄位

$dictModel->getDictName(); //擷取返回的指定欄位值

http://www.bkjia.com/PHPjc/1135808.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1135808.htmlTechArticlemagento 常用的函數,magento常用函數 1.Magento eav_attribute表中source如何指定自訂資料來源 如果你引用的類名為yebihai_usermanage_model_entity_school你...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.