如何处理m2m关系(帖子和标签)by doctrine(在symfony)? [英] How to deal with m2m relationship(posts and tags) by doctrine(in symfony)?

查看:287
本文介绍了如何处理m2m关系(帖子和标签)by doctrine(在symfony)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帖子标签有一个多对多关系(几乎像stackoverflow),所以代码没有ORM应该是:

posts and tags have a many-to-many relationship(pretty much like stackoverflow),so the code to do it without an ORM should be:

$dml = "insert into posts(title,body,created) value($title,'{$_POST['post-text']}',now())";
mysql_query($dml,$con);
$pid = mysql_insert_id($con);
//deal with tags

if(isset($_POST['tagnames']))
{
 $tags = preg_split('/\s+/',trim($_POST['tagnames']));
 $list = "('".implode("',1),('", $tags)."',1)";
 $dml = "insert into tags(name,count) values $list on duplicate key update count=count+1";
 mysql_query($dml,$con);
 $list = "('" . implode("','", $tags) . "')";
 $dml = "insert into post_tags(pid,tagId) select $pid,id from tags where name in $list";
 mysql_query($dml,$con);
}

如何用doctrine这样做?

How to do it with by doctrine?

假设相关类已经从模式生成

Suppose that the related classes are already generated from schemas

困难在于重复密钥更新的 part。

The difficulty lies in the on duplicate key update part.

要进一步,有人在编辑帖子时可能会更难处理 ...

To step further,it can be more difficult to handle when someone is editing the posts...

推荐答案

而不是自己管理这种关系,你可能想考虑使用 sfDoctrineActAsTaggablePlugin ,这可能会使您的生活更轻松。

Instead of managing this relationship yourself you might want to consider using sfDoctrineActAsTaggablePlugin, which will probably make your life easier.

这篇关于如何处理m2m关系(帖子和标签)by doctrine(在symfony)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆