Magento获取数据案例

下面的模块是获取一个或者多个数据的使用方法

  1. 建立modules
D:\www\ysqmagento1938\app\etc\modules\Infinity_Helloworld.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Infinity_Helloworld>
            <active>true</active>
            <codePool>local</codePool>
        </Infinity_Helloworld>
    </modules>
</config>

2 . 建立config.xml 配置文件

D:\www\ysqmagento1938\app\code\local\Infinity\Helloworld\etc\config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <!--设置模块 start-->
    <modules>
        <Infinity_Helloworld>
            <version>0.2.0</version>
        </Infinity_Helloworld>
    </modules>
    <!--end-->
    <!--设置前端路由start-->
    <frontend>
        <routers>
            <helloworld>
                <use>standard</use>
                <args>
                    <module>Infinity_Helloworld</module>
                    <frontName>helloworld</frontName>
                </args>
            </helloworld>
        </routers>  
    </frontend>
    <!--end-->

    <global>
        <!--数据模型设置start-->
        <models>
            <helloworld>
                <class>Infinity_Helloworld_Model</class>
                <resourceModel>helloworld_mysql4</resourceModel>
            </helloworld>
            <helloworld_mysql4>
                <class>Infinity_Helloworld_Model_Resource_Mysql4</class>
                <entities>
                    <blogpost>
                        <table>blog_posts</table>
                    </blogpost>
                </entities>
            </helloworld_mysql4>
        </models>
        <!--数据模型设置end-->

        <resources>
            <!--写读适配器,默认可以不设置-->
            <helloworld_write>
                <connection>
                    <use>default_write</use>
                </connection>
            </helloworld_write>
            <helloworld_read>
                <connection>
                    <use>default_read</use>
                </connection>
            </helloworld_read>
        </resources>
    </global>
</config>

3. 建立控制器

D:\www\ysqmagento1938\app\code\local\Infinity\Helloworld\controllers\BlogController.php
D:\www\ysqmagento1938\app\code\local\Infinity\Helloworld\controllers\IndexController.php
D:\www\ysqmagento1938\app\code\local\Infinity\Helloworld\controllers\MessagesController.php

控制器下面建立方法

<?php
/**
 * Created by PhpStorm.
 * User: lollicup
 * Date: 2019/4/24
 * Time: 14:22
 */

class Infinity_Helloworld_BlogController extends Mage_Core_Controller_Front_Action {

    /**
     * 获取数据集合
     */
    public function showAllBlogPostsAction() {
        $posts = Mage::getModel('helloworld/blogpost')->getCollection();
        foreach($posts as $blog_post){
            echo '<h3>'.$blog_post->getTitle().'</h3>';
            echo nl2br($blog_post->getPost());
        }
    }
}
<?php

/**
 * @Author: ysqgit
 * @Date:   2019-04-20 16:20:09
 * @Last Modified by:   ysqgit
 * @Last Modified time: 2019-04-21 00:25:01
 */

class Infinity_Helloworld_IndexController extends Mage_Core_Controller_Front_Action {        
    public function indexAction() {
//        $blogpost = Mage::getModel('helloworld/blogpost');
//        var_dump($blogpost);
//        echo get_class($blogpost);

//        echo 'Hello World!';
//        $this->loadLayout();
//    	  $this->renderLayout();

        //获取单条数据
        $params = $this->getRequest()->getParams();
        $blogpost = Mage::getModel('helloworld/blogpost');
        echo("Loading the blogpost with an ID of ".$params['id']."<br />");
        echo "<br/>";
        $blogpost->load($params['id']);
        $data = $blogpost->getData();
        print_r($data);
        echo "<br/>";
        print_r($blogpost->getData('title'));
        echo "<br/>";
        print_r($blogpost->getOrigData());
        echo "<br/>";
        print_r($blogpost->getOrigData('title'));

    }

   //添加数据
    public function createNewPostAction() {
        $blogpost = Mage::getModel('helloworld/blogpost');
        $blogpost->setTitle('Code Post!');
        $blogpost->setPost('This post was created from code!');
        $blogpost->save();
        echo 'post created';
    }

    //修改数据保存
    public function editFirstPostAction() {
        $blogpost = Mage::getModel('helloworld/blogpost');
        $blogpost->load(1);
        $blogpost->setTitle("The First post  ttt!");
        print_r ($blogpost->save());
        echo 'post edited';
    }

    //删除数据
    public function deleteFirstPostAction() {
        $blogpost = Mage::getModel('helloworld/blogpost');
        $blogpost->load(1);
        $blogpost->delete();
        echo 'post removed';
    }


    public function goodbyeAction() {
    	//echo 'Goodbye World!';
    	$this->loadLayout();
    	$this->renderLayout();  
	}

	public function paramsAction() {
	    echo '<dl>';            
	    foreach($this->getRequest()->getParams() as $key=>$value) {
	        echo '<dt><strong>Param: </strong>'.$key.'</dt>';
	        echo '</dl><dl><strong>Value: </strong>'.$value.'</dl>';
	    }
	    echo '';
	}

}
<?php

/**
* @Author: ysqgit
* @Date: 2019-04-20 16:44:18
* @Last Modified by: ysqgit
* @Last Modified time: 2019-04-20 16:45:19
*/

class Infinity_Helloworld_MessagesController extends Mage_Core_Controller_Front_Action{

public function goodbyeAction()
{
echo 'Another Goodbye';
}

}

4、建立如下model文件

D:\www\ysqmagento1938\app\code\local\Infinity\Helloworld\Model\Blogpost.php

<?php
/**
 * Created by PhpStorm.
 * User: lollicup
 * Date: 2019/4/24
 * Time: 13:48
 */

class Infinity_Helloworld_Model_Blogpost extends Mage_Core_Model_Abstract
{
    protected function _construct()
    {
        $this->_init('helloworld/blogpost');
    }
}
D:\www\ysqmagento1938\app\code\local\Infinity\Helloworld\Model\Resource\Mysql4\Blogpost.php

<?php
/**
 * Created by PhpStorm.
 * User: lollicup
 * Date: 2019/4/24
 * Time: 13:52
 */

class Infinity_Helloworld_Model_Resource_Mysql4_Blogpost extends Mage_Core_Model_Mysql4_Abstract{
    protected function _construct()
    {
        $this->_init('helloworld/blogpost', 'blogpost_id');
    }
}
D:\www\ysqmagento1938\app\code\local\Infinity\Helloworld\Model\Resource\Mysql4\Blogpost\Collection.php
<?php
/**
 * Created by PhpStorm.
 * User: lollicup
 * Date: 2019/4/24
 * Time: 14:25
 */

class Infinity_Helloworld_Model_Resource_Mysql4_Blogpost_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract {
    protected function _construct()
    {
        $this->_init('helloworld/blogpost');
    }
}

Leave a comment

您的电子邮箱地址不会被公开。 必填项已用 * 标注