2

content と images の 2 つのテーブルがあります (1 対多の関係のための 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');

ソート番号を保存するにはどうすればよいですか? Image テーブルに並べ替えフィールドを追加したくありません。これは、より多くのコンテンツ アイテムで画像を再利用するときに問題が発生する可能性があるためです。コンテンツが新しくてIDがまだわからないのでちょっと戸惑ってます…

4

3 に答える 3

1

モデルを生成した場合は、setUp メソッドに orderBy パラメータを追加できます。

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

orderBy はトリックを行う必要があります

于 2011-11-28T07:49:57.407 に答える
0

次のように、結合テーブルに1対多の関連付けを追加する必要があります。

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

これでうまくいくはずです:)

于 2010-04-01T19:35:03.033 に答える
0

私はあなたが上で得たものとは少し違うことをしているので、これがあなたが求めているものであるかどうかは完全にはわかりませんが、必要なものでオブジェクトを保存することはできませんか?

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

それが役立つことを願っています。

于 2010-03-31T20:45:31.417 に答える