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

Doctrine 关系表中的排序字段(一对多),如何插入排序编号?,doctrine,database-relations,Doctrine,Database Relations,我有两个表,content和images(以及一对多关系的ContentImages表,实际上是3个表) 以下代码保存了该关系(在action>updateContentFromRequest()中): 我更改了模型,在ContentImages表中包含一个排序字段: content_id image_id sort (numeric) 排序号只是一个数字(0,1,2,3等) 如何保存分拣号?我不想在图像表中添加排序字段,因为当图像在更多内容项中重复使用时,这可能会造成困难。当一个内容项是新的

我有两个表,content和images(以及一对多关系的ContentImages表,实际上是3个表)

以下代码保存了该关系(在action>updateContentFromRequest()中):

我更改了模型,在ContentImages表中包含一个排序字段:

content_id
image_id
sort (numeric)
排序号只是一个数字(0,1,2,3等)


如何保存分拣号?我不想在图像表中添加排序字段,因为当图像在更多内容项中重复使用时,这可能会造成困难。当一个内容项是新的,我还不知道ID,所以我有点困惑

我所做的事情与上面的有所不同,因此不能完全确定这是您所要求的,但您不能用所需的东西保存对象吗

$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();

希望有帮助。

您应该在联接表中添加一对多的关联,如:

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();
然后,您可以使用

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

这应该可以做到:)

如果您已经生成了模型,您可以将orderBy参数添加到设置方法中:

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

orderBy应该会做这个把戏

我想你已经掌握了一些东西,非常聪明!)我目前通过重写(自动生成的)saveContent函数做了一个变通,我可以在那里请求内容的id,并对附加的图像进行更新。嘿,Marc,nice Q。。有一次我被难住了。。我也重写了saveRelation方法并设置了排序
$content->ContentImages->setSort('your sort');
$this->hasMany('PICTURE as PICTURES', array(
    'local' => 'BRAND_ID',
    'foreign' => 'PICTURE_ID',
    'refClass' => 'BRAND_PICTURE',
    'orderBy' => 'your_field DESC'
));