Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

php - How to deal with m2m relationship(posts and tags) by doctrine(in symfony)?

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);
}

How to do it with by doctrine?

Suppose that the related classes are already generated from schemas

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...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...