2014-06-26 37 views
4

我有Magento集合mymodule/class。我在我的Magento數據庫中有兩個表classstudent如何在Magento Collection中添加字段選擇查詢作爲字段

Class表:

id  clas_name 

學生表:

id class_id student_name 

class_id學生表是在類表的id外鍵。

我要讓mymodule/class收集,以便SQL看起來如下:

select a.*, (select count(id) FROM studetn 
       WHERE class_id = a.id) as student_count 
FROM class a 

或者,我要讓mymodule/class從上面的SQL。

如何做任何建議?

回答

8

看起來你必須在查詢中添加列。在你已經收集場景中,我們假設像你$集合變量:

$collection = Mage::getModel("mymodule/class")->getCollection() 

所以,你必須修改查詢功能:

$collection->getSelect()->columns(
     array(
      'student_count' => new Zend_Db_Expr('(SELECT count(id) FROM student WHERE class_id=main_table.id)' 
     )); 

上述表達式爲你寫會生成查詢。

+0

@好作品克里希納,我會給你upvote –

+0

正是我在找什麼。 – AnkitK

+0

但過濾器不工作.. –

0

首先創建模塊控制文件模塊名稱

代碼

<?xml version="1.0"?> 
<config> 
    <modules> 
     <Amit_Custommodule> 
      <codePool>community</codePool> 
      <active>true</active> 
     </Amit_Custommodule> 
    </modules> 
</config> 

路徑config.xml中的是應用程序/代碼/社區/艾米特/ Custommodule作爲

Amit_Custommodule.xml at app/etc/modules/ 

/etc/

由於我們定義了codepool是community,所以它的路徑是app/code/community

這裏阿米特文件夾是模塊的名稱空間和Custommodule模塊名

<?xml version="1.0" ?> 
<config> 
    <modules> 
     <Amit_Custommodule> 
      <version>1.0.0</version> 
     </Amit_Custommodule> 
    </modules> 
    <global> 
     <models> 
      <custommodule> 
       <class>Amit_Custommodule_Model</class> 
       <resourceModel>custommodule_resource</resourceModel> 
      </custommodule> 
      <custommodule_resource> 
       <class>Amit_Custommodule_Model_Resource</class> 
       <entities> 
        <custommodule> 
         <table>studetn</table> 
        </custommodule> 
       </entities> 
      </custommodule_resource> 
     </models> 

    </global> 

    </config> 

型號文件Custommodule.php路徑是app/code/community/Amit/Custommodule/Model,它的代碼

<?php 
class Amit_Custommodule_Model_Custommodule extends Mage_Core_Model_Abstract 
{ 
    public function _construct() 
    { 
     $this->_init('custommodule/custommodule'); 
    } 

} 

如果我們定義模型,然後我們需要定義資源一級模型Custommodule.php

我的模塊資源類是Custommodule.php app/code/community/Amit/Custommodule/Model/Resource /和代碼

<?php 
class Amit_Custommodule_Model_Resource_Custommodule extends Mage_Core_Model_Resource_Db_Abstract 
{ 
    /** 
    * Initialize resource model 
    * 
    * @return void 
    */ 
    public function _construct() 
    { 
     $this->_init('custommodule/custommodule', 'id'); 
    } 
    public function getMyCount($std) 
{ 
    $Table = Mage::getSingleton('core/resource')->getTableName('custommodule/custommodule'); 

    $select = $this->getReadConnection()->select() 
     ->from(
      array('main_table' => $Table), 
      array(new Zend_Db_Expr('COUNT(main_table.id)')) 
     ) 
     ->where('main_table.id = :id'); 

    $bind = array('id' => (int)$std->getId()); 
    $counts = $this->getReadConnection()->fetchOne($select, $bind); 

    return intval($counts); 
} 

} 

這兩個文件作出終審模塊Mage::getModel("custommodule/custommodule")->load($primaryKeyOfTable);

現在我們需要的是模塊是取表中的所有數據的類集合文件

收集文件路徑爲Collection.php app/code/community/Amit/Custommodule/Model/Resource/Custommodule也是其代碼

<?php 
class Amit_Custommodule_Model_Resource_Custommodule_Collection 
extends Mage_Core_Model_Resource_Db_Collection_Abstract{ 
    protected function _constuct(){ 
     $this->_init('custommodule/custommodule');  
    } 
} 

$Collection=Mage::getModel("custommodule/custommodule")->Collection(); 

foreach($Collectio as $each) 
{ 
$each->getMyCount(); 
} 

更多細節 http://www.amitbera.com/create-an-magento-extension-with-custom-database-table/

+0

非常感謝阿米特的解釋。這是一個偉大的文章。但這不是我正在看的答案。 – Elisa

+0

@krishna代碼是工作物品... Elisa你的問題也很好。我給兩個upvote –

相關問題