关系表中的排序字段(一对多),如何插入排序号?
我有两个表,内容和图像(还有一个用于一对多关系的 ContentImages 表,所以实际上是 3 个表)。
以下代码保存关系(在操作 > updateContentFromRequest() 中):
$ids = $this->getRequestParameter('contentImages');
if( isset($ids) ){
$ImagesTable = Doctrine::getTable('Content')->getRelation('Images')->getTable();
$associationName = Doctrine::getTable('Content')->getRelation('Images')->getAssociationTable()->getOption('name');
$this->content->$associationName->delete();
foreach ($ids as $id){
$id = explode('/', $id);
$this->content->get('Images')->add($ImagesTable->find($id));
}}
我更改了模型以在 ContentImages 表中包含排序字段:
content_id
image_id
sort (numeric)
排序号就是一个数字(0、1、2、3 等)
$sort = $this->getRequestParameter('contentImagesSort');
如何保存排序号?我不想向图像表添加排序字段,因为当图像在更多内容项中重复使用时,这可能会造成困难。当内容项是新的时,我还不知道 ID,所以我有点困惑......
I have two tables, content and images (and a ContentImages table for the one to many relation, so that's actually 3 tables).
The following code saves the relation (in the action > updateContentFromRequest() ):
$ids = $this->getRequestParameter('contentImages');
if( isset($ids) ){
$ImagesTable = Doctrine::getTable('Content')->getRelation('Images')->getTable();
$associationName = Doctrine::getTable('Content')->getRelation('Images')->getAssociationTable()->getOption('name');
$this->content->$associationName->delete();
foreach ($ids as $id){
$id = explode('/', $id);
$this->content->get('Images')->add($ImagesTable->find($id));
}}
I changed the model to include a sort field in the ContentImages table:
content_id
image_id
sort (numeric)
The sort number is simply that, a number (0,1,2,3 etc)
$sort = $this->getRequestParameter('contentImagesSort');
How do I save the sort number? I do not want to add a sort field to the Image table because that could create difficulties when images are re-used across more content items. When a content item is new I do not know the ID yet so I'm a bit stumped...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您已生成模型,则可以将 orderBy 参数添加到 setUp 方法中:
orderBy 应该可以解决问题
If you have generated models you can add to your setUp method the orderBy parameter:
orderBy should do the trick
您应该在连接表上添加一对多关联,例如:
因此,您将能够像这样直接查询连接表:
然后您可以使用访问 ContentImages
这应该可以解决问题:)
You should add One to Many associations on your join Table, like:
As such, you will be able to query directly the join table like this :
Then you can access to ContentImages using
This should do the trick :)
我做的事情与你上面做的有点不同,所以不完全确定这就是你所要求的,但是你不能用它所需的任何东西来保存对象吗?
或查询它...
希望有帮助。
I do things a little a differently from what you've got above so not completely sure this is what you're asking, but can't you just save the object with whatever is needed for it?
Or querying it...
Hope that helps.