2

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

tshepang
  • 12,111
  • 21
  • 91
  • 136
Marc
  • 65
  • 7

3 Answers3

1

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

Dmitry
  • 11
  • 1
0

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.

Tom
  • 30,090
  • 27
  • 90
  • 124
  • I think you're on to something there, pretty smart! :) I currently made a workaround by overriding the (autogenerated) saveContent function, I can request the id of the content there and do an update on the attached images. – Marc Apr 01 '10 at 07:09
  • hey Marc, nice Q.. had stumped me once.. i too overrode the saveRelation method & set the sort – Prasad Jul 19 '10 at 08:45
0

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 :)

DuoSRX
  • 4,179
  • 3
  • 26
  • 32