我有一个帖子模型,我正在为下面的帖子插入标签。当我编辑时,可能会删除一些标签。那么删除标签并重新插入的正确方法是什么?
$post->setTitle($data['title']);
$post->setBody($data['body']);
$post->setSlug($data['slug']);
$tags = explode(',', $data['tags']);
// Want to remove the tags
foreach ($tags as $tag) {
$tagobj = TagQuery::create()->findOneByName($tag);
if (! $tagobj) {
$tagobj = new Tag();
$tagobj->setName($tag);
$tagobj->save();
}
$post->addTag($tagobj);
}
$post->save();
推进可以插入单个查询还是这是最糟糕的方法。
我在推进组中问过这个问题,但是:-( https://groups.google.com/d/msg/propel-users/x6PH_DwLtVE/H84o1cu4W4kJ
完整的源代码在这里
目标是在删除一个标签或添加一个标签时重新保存标签。该怎么办 ?.
第一优先。
优化是第二要务。
更新2:
我用收到的回复修改了代码,如下所示
$tags = explode(',', $data['tags']);
foreach ($tags as $tag) {
$tagobj = TagQuery::create()->findOneByName($tag);
if (! $tagobj) {
$tagobj = new Tag();
$tagobj->setName($tag);
$tagobj->save();
}
}
// var_dump($tags);
$tagcollection = TagQuery::create()->findByName($tags);
// var_dump($tagcollection);
// exit;
$post->setTags($tagcollection);
现在我得到数组到字符串的转换错误。
Notice: Array to string conversion in /var/www/harisample/vendor/propel/propel/src/Propel/Runtime/Connection/StatementWrapper.php on
line 171 Call Stack: 0.0001 131940
{main}() /var/www/harisample/web/index.php:0 0.0243 1259056
Aura\Framework\Bootstrap\Web->exec() /var/www/harisample/web/index.php:13 0.0243 1259108
Aura\Framework\Web\Controller\Front->exec() /var/www/harisample/package/Aura.Framework/src/Aura/Framework/Bootstrap/Web.php:71 0.0243 1259436
Aura\Framework\Web\Controller\Front->request() /var/www/harisample/package/Aura.Framework/src/Aura/Framework/Web/Controller/Front.php:168 0.0314 1694584
Aura\Web\Controller\AbstractPage->exec() /var/www/harisample/package/Aura.Framework/src/Aura/Framework/Web/Controller/Front.php:222 0.0316 1699500
Aura\Web\Controller\AbstractPage->action() /var/www/harisample/package/Aura.Web/src/Aura/Web/Controller/AbstractPage.php:168 0.0316 1699576
Aura\Web\Controller\AbstractPage->invokeMethod() /var/www/harisample/package/Aura.Web/src/Aura/Web/Controller/AbstractPage.php:206 0.0316 1699960
ReflectionMethod->invokeArgs() /var/www/harisample/package/Aura.Web/src/Aura/Web/Controller/AbstractPage.php:231 0.0316 1699976
Hari\Sample\Web\Post\Page->actionEdit() /var/www/harisample/package/Aura.Web/src/Aura/Web/Controller/AbstractPage.php:231 0.0856 5802116 1
Hari\Sample\Model\Base\Post->save() /var/www/harisample/package/Hari.Sample/src/Hari/Sample/Web/Post/Page.php:127 0.0874 5808356 1
Hari\Sample\Model\Base\Post->doSave() /var/www/harisample/package/Hari.Sample/src/Hari/Sample/Model/Base/Post.php:930 0.0881 5813420 1
Hari\Sample\Model\Base\PostTagQuery->delete() /var/www/harisample/package/Hari.Sample/src/Hari/Sample/Model/Base/Post.php:1000 0.0881 5813700 1
Propel\Runtime\ActiveQuery\ModelCriteria->delete() /var/www/harisample/package/Hari.Sample/src/Hari/Sample/Model/Base/PostTagQuery.php:557 0.0881 5814628 1
Propel\Runtime\ActiveQuery\Criteria->doDelete() /var/www/harisample/vendor/propel/propel/src/Propel/Runtime/ActiveQuery/ModelCriteria.php:1324 0.0883 5817716 1
Propel\Runtime\Connection\StatementWrapper->execute() /var/www/harisample/vendor/propel/propel/src/Propel/Runtime/ActiveQuery/Criteria.php:2408 0.0883 5817772 1
PDOStatement->execute() /var/www/harisample/vendor/propel/propel/src/Propel/Runtime/Connection/StatementWrapper.php:171
谢谢