I was wondering if anyone had ever tried creating an association that
links a single record from what would normally be a hasMany
association (or knew a clean solution to do so). In this case, I'm
working with an article with multiple versions; I'd like to create
what is essentially a hasOne relationship for an association called
CurrentVersion.

I've had a couple guesses on the best way to do this. The sample code
would be in the Area model for each example.

1) Set up a hasOne, setting the order correctly so that the record I
want is the first returned (and the one assigned to the association):

<?php

var $hasMany = array(
  'Version' => array(
    'className' => 'Version',
    'foreignKey' => 'area_id',
    'dependent' => true,
    'conditions' => array('Version.draft' => 0),
    'order' => 'Version.created DESC',
    )
  );
var $hasOne = array(
  'CurrentVersion' => array(
    'className' => 'Version',
    'foreignKey' => 'area_id',
    'dependent' => false,
    'conditions' => array(
      'CurrentVersion.draft' => 0
      ),
    'order' => 'CurrentVersion.created DESC'
    )
);

?>

2) Set up a hasMany with a limit of 1, setting the order correctly so
that the correct record is returned:

<?php

var $hasMany = array(
  'Version' => array(
    'className' => 'Version',
    'foreignKey' => 'area_id',
    'dependent' => true,
    'conditions' => array('Version.draft' => 0),
    'order' => 'Version.created DESC',
    ),
  'CurrentVersion' => array(
    'className' => 'Version',
    'foreignKey' => 'area_id',
    'dependent' => false,
    'limit' => 1,
    'conditions' => array(
      'CurrentVersion.draft' => 0
      ),
    'order' => 'CurrentVersion.created DESC'
    )
);

?>

Solution 1 makes sense to me, but when I try to do finds with
recursive associations, the associated data comes back corrupted (any
associated models underneath have the first character of each field's
value cut off).

I'd really like to make something more elegant than Solution 2,
because there really is (and will always only be) one matching record
for the association. Sure, I could access the data as $area
['CurrentVersion'][0], but that just doesn't seem ideal. A co-worker
of mine suggested adding something to convert the data in afterFind:

<?php

function afterFind($results) {
  foreach($results as $key => $value) {
    if (is_array($value['CurrentVersion'])) {
      $results[$key]['CurrentVersion'] = $value['CurrentVersion'][0];
    }
  }
  return $results;
}

?>

That does work, but I guess I just wondered if anyone had found
themselves working in this same sort of scenario and had found a
better solution. I'd love to hear your thoughts.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to