Php 在magento中创建包含2列的自定义模型表
我在模块公司/发货代理上设置了一个模型 我的配置看起来像这样Php 在magento中创建包含2列的自定义模型表,php,sql,json,ajax,magento,Php,Sql,Json,Ajax,Magento,我在模块公司/发货代理上设置了一个模型 我的配置看起来像这样 <?xml version="1.0"?> <config> <modules> ... </modules> <admin> <routers> ... </routers> </admin> <global> <models>
<?xml version="1.0"?>
<config>
<modules>
...
</modules>
<admin>
<routers>
...
</routers>
</admin>
<global>
<models>
<Shippingagent>
<class>COMPANY_Shippingagent_Model</class>
<resourceModel>Shippingagent_Mysql4</resourceModel>
</Shippingagent>
<Shippingagent_mysql4>
<class>COMPANY_Shippingagent_Model_Mysql4</class>
<entities>
<Shippingagent>
<table>Shippingagent</table>
</Shippingagent>
</entities>
</Shippingagent_mysql4>
</models>
<resources>
<Shippingagent_setup>
<setup>
<module>COMPANY_Shippingagent</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</Shippingagent_setup>
<Shippingagent_write>
<connection>
<use>core_write</use>
</connection>
</Shippingagent_write>
<Shippingagent_read>
<connection>
<use>core_read</use>
</connection>
</Shippingagent_read>
</resources>
</global>
</config>
我的模型如下所示:
public function setAgentAction() {
$json = $this->getRequest()->getParam('json');
$json = json_decode($json);
foreach($json as $k => $v) {
if($k == 'brand')
$brand = $v;
}
//Send $json to the model
$data = array(
'title'=>$brand
);
$model = Mage::getModel('COMPANY_Shippingagent/Shippingagent')->setData($data);
try {
$insertId = $model->save()->getId();
echo "Data successfully inserted. Insert ID: ".$insertId;
} catch (Exception $e){
echo $e->getMessage();
}
}
}
class COMPANY_Shippingagent_Model_Mysql4_Shippingagent extends Mage_Core_Model_Mysql4_Abstract
{
public function _construct()
{
$this->_init('Shippingagent/Shippingagent', 'Shippingagent_id');
}
}
<?php
$installer = $this;
$installer->startSetup();
$installer->run("
-- DROP TABLE IF EXISTS {$this->getTable('COMPANY_Shippingagent/Shippingagent')};
CREATE TABLE {$this->getTable('COMPANY_Shippingagent/Shippingagent')} (
`Shippingagent_id` int(11) unsigned NOT NULL auto_increment,
`title` varchar(255) NOT NULL default '',
PRIMARY KEY (`Shippingagent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
$installer->endSetup();
我的桌子是这样的:
public function setAgentAction() {
$json = $this->getRequest()->getParam('json');
$json = json_decode($json);
foreach($json as $k => $v) {
if($k == 'brand')
$brand = $v;
}
//Send $json to the model
$data = array(
'title'=>$brand
);
$model = Mage::getModel('COMPANY_Shippingagent/Shippingagent')->setData($data);
try {
$insertId = $model->save()->getId();
echo "Data successfully inserted. Insert ID: ".$insertId;
} catch (Exception $e){
echo $e->getMessage();
}
}
}
class COMPANY_Shippingagent_Model_Mysql4_Shippingagent extends Mage_Core_Model_Mysql4_Abstract
{
public function _construct()
{
$this->_init('Shippingagent/Shippingagent', 'Shippingagent_id');
}
}
<?php
$installer = $this;
$installer->startSetup();
$installer->run("
-- DROP TABLE IF EXISTS {$this->getTable('COMPANY_Shippingagent/Shippingagent')};
CREATE TABLE {$this->getTable('COMPANY_Shippingagent/Shippingagent')} (
`Shippingagent_id` int(11) unsigned NOT NULL auto_increment,
`title` varchar(255) NOT NULL default '',
PRIMARY KEY (`Shippingagent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
$installer->endSetup();
创建表不应依赖于代码的其余部分。安装脚本应在任何路由之前运行。您的意思是“未插入到表中”吗?是的,这就是我的意思。请检查您的数据库是否已创建表。如果没有,则从数据库的core_资源表中删除安装项,这样,当您在管理面板中进行任何刷新时,脚本文件将再次运行,但您可以尝试在安装脚本中退出echo,以确保。我可以看到他们的安装脚本文件有问题。请看一下“获取表名”功能的方式。它已经修复。config.xml中的xml文件似乎有问题。创建表不应依赖于其余代码。安装脚本应在任何路由之前运行。您的意思是“未插入到表中”吗?是的,这就是我的意思。请检查您的数据库是否已创建表。如果没有,则从数据库的core_资源表中删除安装项,这样,当您在管理面板中进行任何刷新时,脚本文件将再次运行,但您可以尝试在安装脚本中退出echo,以确保。我可以看到他们的安装脚本文件有问题。请看一下“获取表名”功能的方式。它已经修复。似乎config.xml中的xml文件有问题