Heim  >  Artikel  >  Datenbank  >  Magento 1.8 Development 第六章:数据库和模块

Magento 1.8 Development 第六章:数据库和模块

WBOY
WBOYOriginal
2016-06-07 15:36:071394Durchsuche

在这一章,我们将学习一下内容: 注册资源模型 注册资源连接 安装和升级脚本 创建一个单一的表模型 使用Magento Collection来查询数据 介绍 在上一章,我们学习了怎么Magento数据库是怎么工作的,我们如何连接数据库并且如何跟数据库交互。 在这一章, 我们

在这一章,我们将学习一下内容:

       注册资源模型

       注册资源连接

      安装和升级脚本

      创建一个单一的表模型

      使用Magento Collection来查询数据

介绍

在上一章,我们学习了怎么Magento数据库是怎么工作的,我们如何连接数据库并且如何跟数据库交互。

在这一章,我们将把我们上一章学习的知识去执行一些更实际的任务。我们将让我们第4章创建的模块与数据库的交互。

我们将创建Magento模块和一个与之交互的数据表


注册资源模型

我们要做的第一步就是注册资源模型,编写资源模型来与数据库进行业务逻辑的交互。

准备工作

我们必须在config.xml 文件中添加另外的配置块.打开 app/code/local/Packt/Helloworld/etc/config.xml 文件

怎么做

下面一步步来分析怎么在Packt_Helloworld模块中来注册资源模型

<global>
	<blocks>
		<helloworld>
			<class>Packt_Helloworld_Block</class>
		</helloworld>
	</blocks>
	<helpers>
		<helloworld>
			<class>Packt_Helloworld_Helper</class>
		</helloworld>
	</helpers>
	<models>
		<helloworld>
			<class>Packt_Helloworld_Model</class>
		</helloworld>
		<helloworld_resource>
			<class>Packt_Helloworld_Model_Resource</class>
		</helloworld_resource>
	</models>
</global>

3、创建文件夹 app/code/local/Packt/Helloworld/Model/Resource

        <models>
		<helloworld>
			<class>Packt_Helloworld_Model</class>
		</helloworld>
		<helloworld_resource>
			<class>Packt_Helloworld_Model_Resource</class>
		</helloworld_resource>
	</models>


5、使用wiz命令行工具测试你的配置

wiz devel-models | grep helloworld

你现在将看到如下输出:

Magento 1.8 Development  第六章:数据库和模块

上面的命令将显示所有与helloworld想匹配的注册的模块。

它是如何工作的

这个Magento模型使用了业务逻辑,例如,通过一个观察者模式包含的函数可以调用事件或者定时任务。

一个Magento对象能够代表一个实体,比如一个产品、客户和分类。这个模型代表一个实体通过继承Mage_Core_Abstract类。这个类有使用资源模型的逻辑。例如,save()函数在这个类里面已经定义好了。

当你在Mage_Core_Model_Abstract类中看到save()函数,你会看到它调用了getResource()函数,这个getResource()函数将返回一个资源模型的实体。

资源模型使用实体与数据库交互,Magento特定的业务逻辑都是通过这个实体来调用和操作。

如果你想获得一个资源模型实例,你可以使用Mage::getResourceModel()或者Mage::getResourcdeSingleton()方法去获得一个类的实例,我们可以通过Magento类名的的一个参数获得。


注册资源连接

在这一节,我们将在这个模块中配置read和write适配器。这些适配器在模型中用于连接数据库。

准备

下面我们将一步一步在Packt_Helloworld模块中创建read和write连接:

<resources>
	<helloworld_read>
		<connection>
			<use>core_read</use>
		</connection>
	</helloworld_read>
</resources>

<resources>
	<helloworld_write>
		<connection>
			<use>core_write</use>
		</connection>
	</helloworld_write>
	<helloworld_read>
		<connection>
			<use>core_read</use>
		</connection>
	</helloworld_read>
</resources>
3、完成上面步骤之后在后头清空cache

它是如何工作的

每一个模型在Magento中都分派了read和write适配器,默认的read适配器是core_read,默认的write适配器是core_write。

通常情况下,在Magento数据库中的表在models均使用core_read和core_write适配器进行操作。


更多...

你可以注册和配置一个特殊的数据库模型来连接其他的数据库,如果你打开下面的URL,你可以得到一个好的教你怎么做的教程:
http://www.solvingmagento.com/accessing-an-external-database-fromyour-magento-module/

sql安装和升级的脚本

如果你的模块使用的是自定义的数据库表,你需要在你的产品服务器上做一些改变。Magento会自动根据安装或更新脚本执行SQL。

在这里,我们将要扩展Packt_Helloworld模块的install脚本.这个install脚本将把属性添加到所有的产品中。

准备

在这里,我们将在模块的数据库文件夹中工作。从Packt_Helloworld模块中打开我们的数据库文件夹来写代码。

怎么做

下面将逐步描述在我们模块中创建安装脚本的过程:

<helloworld_setup>
	<setup>
		<module>Packt_Helloworld</module>
		<class>Mage_Eav_Model_Entity_Setup</class>
	</setup>
	<connection>
		<use>core_setup</use>
	</connection>
</helloworld_setup>
上面的代码将初始化一个名称为helloworld_setup的过程

2、在安装脚本中创建文件夹,这个文件夹名称在app/code/local/Packt/Helloworld/sql/helloworld_setup.

3、创建安装脚本,安装脚本的名称规则为install-.php.在我们的项目,这个脚本的名称是install-0.0.1.php.

4、添加下面的内容到文件中测试执行过程:

die('test');

5、在前台页面清除缓存和刷新页面,你将在页面看到test.

tips:一个安装或者升级脚本时会在安装时运行一次。这里的版本号每次都将注册设置到数据表core_resource中。当你想再次测试时运行脚本。你能够删除或者改变版本。

6、添加下面的内容到你的安装脚本,这里会安装一个属性到所有的可用产品中.

<?php $installer = $this;
$installer->startSetup();
	$installer->addAttribute('catalog_product', 'helloworld_label',
	array(
		'group' => 'Helloworld',
		'type' => 'varchar',
		'label' => 'Helloworld label',
		'input' => 'text',
		'global' => Mage_Catalog_Model_Resource_Eav_
		Attribute::SCOPE_STORE,
		'visible' => true,
		'required' => false,
		'searchable' => false,
		'filterable' => false,
		'comparable' => false,
		'visible_on_front' => true,
		'unique' => false,
		'apply_to' => 'simple,configurable,virtual,
		bundle,downloadable',
		'is_configurable' => false
	)
);
$installer->endSetup();

上面的代码将创建helloworld_label产品属性,这个属性将应用到所有的产品中。这个属性将在后台的在Helloworld选项卡的group选项中显示。

7、清理缓存和刷新页面,这个安装脚本将自动的运行。

8、到后台打开一个产品去检查属性是否在产品中添加,你将看到一个Helloworld选项卡的属性,它类似于下面的截图:


Magento 1.8 Development  第六章:数据库和模块

9、当你的安装脚本执行了,你能在Magento的core_resource表中看到你的设置。所有的模块和版本号都在这个表中,所以Magento知道安装或者升级脚本有没有执行。

它是怎么工作的...

你想在工作中改变一些你的数据结构使用安装脚本是有用的,可能是下面的一些目的:

    从你的开发/测试环境到你的生成环境使你的站点容易的开发

    力所能及的修复数据库

    对数据库修改的概述

$this对象在这个类的安装脚本中有声明,我们是在config.xml里面注册定义的。在这个项目里面,它是Mage_Eav_Model_Entity_Setup类,这个类主要用于你想去添加EAV属性到实体中,例如在这里的产品或者分类。主要的所有设置类都继承默认的设置类,在这里是,Mage_Core_Model_Resource_Setup.

如果你想在你的安装脚本中做很多的事情,你可以创建你自己的设置类,这将从普通的设置类中扩展。

在安装程序文件中这个类中声明的函数可以调用这个$this.

在模型中创建一个平面表

在这里,我们将扩展我们的模块和平面数据库表,我们将创建一个升级脚本和创建一个表的指令。当这个表创建,我们将通过添加所需的Magento模型、资源模型和集合来完成设置,我们将在Magento ORM中创建一个自定义Magento实体和所有的特性

准备

在这里,我们将要编码和数据库,打开你的IDE在模块文件夹和访问你的数据库客户端。

怎么做...

下面的步骤是创建一个数据表应用到相关联的Magento模型:

1、在app/code/local/Packt/Helloworld/etc/config.xml中配置表名。

<helloworld_resource>
	<class>Packt_Helloworld_Model_Resource</class>
	<entities>
		<subscription>
			<table>helloworld_subscription</table>
		</subscription>
	</entities>
</helloworld_resource>

上面的代码将定义引用helloworld_subscription表的实体

3、创建升级脚本文件app/code/local/Packt/Helloworld/sql/helloworld_setup/upgrade-0.0.1-0.0.2.php

4、添加下面的代码到升级脚本文件,这个代码是创建表及其一些字段的命令

<?php $installer = $this;
$installer->startSetup();
$table = $installer->getConnection()->newTable($installer->getTable('helloworld/subscription'))->addColumn('subscription_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
	'identity' => true,
	'unsigned' => true,
	'nullable' => false,
	'primary' => true,
	), 'Subscription id')->addColumn('created_at', Varien_Db_Ddl_Table::
	TYPE_TIMESTAMP, null, array(
	'nullable' => false,
	), 'Created at')->addColumn('updated_at', Varien_Db_Ddl_Table::
	TYPE_TIMESTAMP, null, array(
	'nullable' => false,
	), 'Updated at')->addColumn('firstname', Varien_Db_Ddl_Table::TYPE_TEXT,64, array(
	'nullable' => false,
	), 'First name')
	->addColumn('lastname', Varien_Db_Ddl_Table::TYPE_TEXT,
	64, array(
	'nullable' => false,
	), 'Last name')
	->addColumn('email', Varien_Db_Ddl_Table::TYPE_TEXT,
	64, array(
	'nullable' => false,
	), 'Email address')
	->addColumn('status', Varien_Db_Ddl_Table::TYPE_TEXT,
	32, array('nullable' => false,'default' => 'pending',), 'Status')->addColumn('message', Varien_Db_Ddl_Table::TYPE_TEXT,'64k', array('unsigned' => true,'nullable' => false,), 'Subscription notes')->addIndex($installer->getIdxName('helloworld/subscription',array('email')),array('email'))->setComment('Helloworld subscriptions');
$installer->getConnection()->createTable($table);
$installer->endSetup();

5、清理缓存刷新前台页面,当你刷新你的数据库客户端的表时,你将看到helloworld_subscription表在列表中.
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn