第四篇 Magento的模型和ORM基础(总结处理单条数据)(二)

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. 创建model文件夹以及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');
    }
}

4. 创建indexController.php文件获取相关数据信息

<?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() {
        $params = $this->getRequest()->getParams();
        $blogpost = Mage::getModel('helloworld/blogpost');
        echo "<pre>";
        print_r($blogpost);
        echo "</pre>";
        echo "<br>";
        echo get_class($blogpost);
        echo "<br>";
        $blogpost->load($params['id']);
        echo "<pre>";
        print_r($blogpost->getData());
        echo "</pre>";
        echo "<br>";
        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('Code33 Post333!');
        $blogpost->setPost('3333This 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 '';
	}
?>

访问indexController.php文件的上面方法,将获得如下结果 。

http://local_mgt1938.com/index.php/helloworld/index/index/index?id=2

http://local_mgt1938.com/index.php/helloworld/index/createNewPost

http://local_mgt1938.com/index.php/helloworld/index/deleteFirstPost

http://local_mgt1938.com/index.php/helloworld/index/editFirstPost?id=2

Leave a comment

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