Php phalcon-通过循环插入数据无效
我试图使用模型在phalcon框架中保存数据。我的实际目标是循环数组并使用该循环插入它们。但问题是它总是插入数组的最后一个值Php phalcon-通过循环插入数据无效,php,mysql,model,phalcon,phalcon-routing,Php,Mysql,Model,Phalcon,Phalcon Routing,我试图使用模型在phalcon框架中保存数据。我的实际目标是循环数组并使用该循环插入它们。但问题是它总是插入数组的最后一个值 Example: $tags = [1,2,3,4] 但是当我执行代码时,它只在数据库中插入4个 //grab the tag model $tagModel = new Tags(); //loop through the tag array foreach($tags as $tag){
Example: $tags = [1,2,3,4]
但是当我执行代码时,它只在数据库中插入4个
//grab the tag model
$tagModel = new Tags();
//loop through the tag array
foreach($tags as $tag){
$tagModel->tag_name = $tag;
$tagModel->save();
}
这个问题有什么解决办法吗
提前感谢。
$tagModel
链接到数据库中的单个条目,因此您可以在第一次提交中创建一个条目,然后在其余部分中更新它
要修复此问题,只需在每个实例中创建一个新的Tags()实例:
//loop through the tag array
foreach($tags as $tag){
$tagModel = new Tags();
$tagModel->tag_name = $tag;
$tagModel->save();
}
我在发布问题后找到了解决方案。请提交答案。谢谢你的帮助