我在尝试通过对象 $product 为我的商店导入一些 xml 时遇到问题。当我尝试填写 $product 成员时,一切正常,它在 cca 中循环。我的 xml 中有 800 种产品。但是当我在循环末尾添加方法 save 时,它只需要前 10 个产品并且不会做更多。
我试图将 sleep(1) 放在 $product->save() 之后,因为我认为 SQL 会因为大量收入查询而断开连接,但事实并非如此两者都无济于事。
你们知道为什么会这样吗?下面是我的代码(我没有在这里粘贴 xml 文件,因为我认为它不重要)。
使用 Prestashop 1.6.0.9。
<?
include('../config/config.inc.php');
include('../init.php');
if (file_exists('exportgoods.xml'))
{
$xml = simplexml_load_file('exportgoods.xml');
foreach ($xml->PRODUCTS->PRODUCT as $jmena_produktu)
{
if($jmena_produktu->PRODUCER == "2"):
$reference=$string = str_replace(' ', '', $jmena_produktu->NAME);
$id_product = (int)Db::getInstance()->getValue('SELECT id_product FROM '._DB_PREFIX_.'product WHERE reference = \''.pSQL($reference).'\'');
$product = $id_product ? new Product((int)$id_product, true) : new Product();
$product->reference = $reference;
$product->price = (float)$jmena_produktu->PRICEWITHDPH;
$product->id_category_default = 2;
$product->category = 15;
$product->name[1] = (string)$jmena_produktu->NAME;
$product->description[1] = (string)$jmena_produktu->LONGDESCRIPTION;
$product->description_short[1] = (string)$jmena_produktu->DESCRIPTION;
$product->link_rewrite[1] = Tools::link_rewrite($reference);
if (!isset($product->date_add) || empty($product->date_add))
$product->date_add = date('Y-m-d H:i:s');
$product->date_upd = date('Y-m-d H:i:s');
$id_product ? $product->updateCategories(array(2,15)) : $product->addToCategories(array(2,15));
#Here start my problem
$product->save();
echo 'Product <b>'.$product->name[1].'</b> '.($id_product ? 'updated' : 'created').'<br />';
endif;
}
}
else
{
exit('Failed to open exportgoods.xml.');
}
?>
最佳答案
我记得有过类似的问题,我也在使用 simplexml_load_file()。我不确定那是否是当时的问题,但这里没有任何问题:
$data = file_get_contents('exportgoods.xml');
$xml = new SimpleXMLElement($data);
有时 XML 元素相互嵌套,与您在其上执行 var_dump($xml) 或 print_r($xml) 时看到的不同。 如果您问我,XML 是一种奇怪的野兽。
当你只打印出$jmena_produktu->NAME时,你会看到什么,只是为了检查它是否比 10 更远。像这样:
foreach ($xml->PRODUCTS->PRODUCT as $jmena_produktu)
{
echo $jmena_produktu->NAME . '</br>';
}
exit;
注意 exit,因此它不会执行其他代码。
关于php - Prestashop 1.6.0.9 - 循环中的 $product->save() 只需要 10 个产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27961906/
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
从给定URL下载文件并立即将其上传到AmazonS3的更直接的方法是什么(+将有关文件的一些信息保存到数据库中,例如名称、大小等)?现在,我既不使用Paperclip,也不使用Carrierwave。谢谢 最佳答案 简单明了:require'open-uri'require's3'amazon=S3::Service.new(access_key_id:'KEY',secret_access_key:'KEY')bucket=amazon.buckets.find('image_storage')url='http://www.ex
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request
我使用Nokogiri(Rubygem)css搜索寻找某些在我的html里面。看起来Nokogiri的css搜索不喜欢正则表达式。我想切换到Nokogiri的xpath搜索,因为这似乎支持搜索字符串中的正则表达式。如何在xpath搜索中实现下面提到的(伪)css搜索?require'rubygems'require'nokogiri'value=Nokogiri::HTML.parse(ABBlaCD3"HTML_END#my_blockisgivenmy_bl="1"#my_eqcorrespondstothisregexmy_eq="\/[0-9]+\/"#FIXMEThefoll
我已经看到了一些其他的问题,尝试了他们的建议,但没有一个对我有用。我已经使用Rails大约一年了,刚刚开始一个新的Rails项目,突然遇到了问题。我卸载并尝试重新安装所有Ruby和Rails。Ruby很好,但Rails不行。当我输入railss时,我得到了can'tfindgemrailties。我当前的Ruby版本是ruby2.2.2p95(2015-04-13修订版50295)[x86_64-darwin15],尽管我一直在尝试通过rbenv设置ruby2.3.0。如果我尝试rails-v查看我正在运行的版本,我会得到同样的错误。我使用的是MacOSXElCapitan版本10
ActiveRecord用于在每次调用保存方法时调用after_save回调,即使模型没有更改并且没有生成插入/更新查询也是如此。这实际上是默认行为。在大多数情况下这没问题。但是一些after_save回调对模型是否实际保存的事情很敏感。有没有办法确定模型是否实际保存在after_save中?我正在运行以下测试代码:classStage 最佳答案 ActiveRecordusetocallafter_savecallbackeachtimesavemethodiscalledevenifthemodelwasnotchangedan
考虑一下:现在这些情况:#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2我需要用其他字符串输出URL。我如何保证&符号不会被转义?由于我无法控制的原因,我无法发送&。求助!把我的头发拉到这里:\编辑:为了澄清,我实际上有一个像这样的数组:@images=[{:id=>"fooid",:url=>"http://
我刚刚安装了带有RVM的Ruby2.2.0,并尝试使用它得到了这个:$rvmuse2.2.0--defaultUsing/Users/brandon/.rvm/gems/ruby-2.2.0dyld:Librarynotloaded:/usr/local/lib/libgmp.10.dylibReferencedfrom:/Users/brandon/.rvm/rubies/ruby-2.2.0/bin/rubyReason:Incompatiblelibraryversion:rubyrequiresversion13.0.0orlater,butlibgmp.10.dylibpro
我正在处理http://prepwork.appacademy.io/mini-curriculum/array/中概述的数组问题我正在尝试创建函数my_transpose,它接受一个矩阵并返回其转置。我对写入二维数组感到很困惑!这是一个代码片段,突出了我的困惑。rows=[[0,1,2],[3,4,5],[6,7,8]]columns=Array.new(3,Array.new(3))putscolumns.to_s#Outputisa3x3arrayfilledwithnilcolumns[0][0]=0putscolumns.to_s#Outputis[[0,nil,nil],[
我想在heroku.com上查看我的应用程序日志的内容,所以我关注了thisexcellentadvice并拥有我所有的日志内容。但是我现在很想知道我的日志文件实际在哪里,因为“log/production.log”似乎是空的:C:\>herokuconsoleRubyconsoleforajpbrevx.heroku.com>>files=Dir.glob("*")=>["public","tmp","spec","Rakefile","doc","config.ru","app","config","lib","README","Gemfile.lock","vendor","sc