I have solved my own problem.  The solution, for those as lost as I
was, lies in the Model::find function.  I recommend 
http://book.cakephp.org/view/449/find
for some light reading.  The solution in my case was to override the
Model::read( ) function in my Item AppModel.  Code follows:

function read($fields = null, $id = null)
{

  /* Clean up $id, make sure it's real
   * See line 998 of model.php as a reference. */
    if ($id != null)
    {
        $this->id = $id;
    }
    $id = $this->id;
    if (is_array($this->id))
    {
        $id = $this->id[0];
    }
    if ($id !== null && $id !== false)
    { /* $id works, lets find our Item... */
        $find_parameters = array (
            'conditions'=> array ('Item.itemid'=>$id),
            'recursive'=>1,
            'fields'=> array ('Item.itemid',
                    'Item.revision',
                    'Item.content',
                    'Item.type',
                    'Item.mimetype',
                    'Item.poster'
            ),
            'order'=>'Item.revision',
            'limit'=>1
        );
        $this->data = $this->find('first', $find_parameters);
        return $this->data;
    }
    else
    { /* Oops! */
        return false;
    }
}

I had been playing around with Model::query( ), but this is much less
flexible, and introduces potential issues down the road (with DBMS
changes, table name changes, etc.)  Model::query( ) also does not
return the Array of records, but returns instead the array containing
the result set directly from the query.

In short, I should have RTFM about two hours earlier.

If anyone can think of a reason why this is not wise, or knows of a
better way of doing it, suggestions are always welcome.

Cheers!

./sparroah

On Jan 21, 10:05 am, sparroah <[email protected]> wrote:
> I have a database organized to do automatic versioning of entries.  My
> table looks like this:
>
> `id` int(10) unsigned NOT NULL auto_increment,
> `itemid` varchar(5) NOT NULL,
> `revision` int(10) unsigned NOT NULL,
> ...
>
> That being said, `id` is not used for anything in the application
> itself, but only for Database tasks.  The `itemid` field is the field
> I wish to use for selecting the item, with the understanding that I
> select the entry with the most recent revision.  I have no issues
> writing the SQL for this, but where I get lost is how I would set up
> CakePHP to retrieve this.  For example, if I have an entry
>
> `id` = 5,
> `itemid` = "AAAAA",
> `revision` = 2,
> ...
>
> I want the URL to look like this:
>
> http://example.com/items/view/AAAAA
>
> and nothttp://example.com/items/view/5.
>
> Is this at all possible?  The Database is from a legacy application
> from which we are migrating, which precludes me modifying its
> structure (at first, anyway.)  Any help here is greatly appreciated.
>
> ./sparroah

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to