关系表中的排序字段(一对多),如何插入排序号?

发布于 2024-08-27 09:08:40 字数 950 浏览 8 评论 0原文

我有两个表,内容和图像(还有一个用于一对多关系的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我们只是彼此的过ke 2024-09-03 09:08:40

如果您已生成模型,则可以将 orderBy 参数添加到 setUp 方法中:

$this->hasMany('PICTURE as PICTURES', array(
    'local' => 'BRAND_ID',
    'foreign' => 'PICTURE_ID',
    'refClass' => 'BRAND_PICTURE',
    'orderBy' => 'your_field DESC'
));

orderBy 应该可以解决问题

If you have generated models you can add to your setUp method the orderBy parameter:

$this->hasMany('PICTURE as PICTURES', array(
    'local' => 'BRAND_ID',
    'foreign' => 'PICTURE_ID',
    'refClass' => 'BRAND_PICTURE',
    'orderBy' => 'your_field DESC'
));

orderBy should do the trick

嘿嘿嘿 2024-09-03 09:08:40

您应该在连接表上添加一对多关联,例如:

ContentImages:
  relations:
    Image:
      local: image_id
      foreign: id
    Content:
       local: content_id
       foreign: id

因此,您将能够像这样直接查询连接表:

$q = Doctrine_Query::create()
  ->from('Content c')
  ->leftJoin('c.ContentImages ci')
  ->leftJoin('c.Images i')
  ->execute();

然后您可以使用访问 ContentImages

$content->ContentImages->setSort('your sort');

这应该可以解决问题:)

You should add One to Many associations on your join Table, like:

ContentImages:
  relations:
    Image:
      local: image_id
      foreign: id
    Content:
       local: content_id
       foreign: id

As such, you will be able to query directly the join table like this :

$q = Doctrine_Query::create()
  ->from('Content c')
  ->leftJoin('c.ContentImages ci')
  ->leftJoin('c.Images i')
  ->execute();

Then you can access to ContentImages using

$content->ContentImages->setSort('your sort');

This should do the trick :)

月朦胧 2024-09-03 09:08:40

我做的事情与你上面做的有点不同,所以不完全确定这就是你所要求的,但是你不能用它所需的任何东西来保存对象吗?

$association = // load up existing entry from ContentImages using whatever method
$association->setSort($the_sort_I_want_to_add);
$association->save();

或查询它...

$association = // load up existing entry from ContentImages using whatever method
$my_unknown_sort = $association->getSort();

希望有帮助。

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?

$association = // load up existing entry from ContentImages using whatever method
$association->setSort($the_sort_I_want_to_add);
$association->save();

Or querying it...

$association = // load up existing entry from ContentImages using whatever method
$my_unknown_sort = $association->getSort();

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文