The way I do it based on http://c7y.phparch.com/c/entry/1/art,mvc_and_cake and
http://www.pseudocoder.com/archives/2008/10/24/cakephp-custom-find-types/

I add to my app_model.php file (as per Matt's article)

function find($type, $options = array()) {
  $method = null;
  if(is_string($type)) {
    $method = sprintf('__find%s', Inflector::camelize($type));
  }

  if($method && method_exists($this, $method)) {
    return $this->{$method}($options);
  } else {
    return parent::find($type, $options);
  }
}

Then I can create custom find types in models that need them, like in
my User model I have

function __findNames($options = array()) {
                        $users = $this->find('all', array(
                                'fields'=>array('id','last_name','first_name'),
                                
'order'=>array('last_name','first_name'),'recursive'=>-1));
                                $users = Set::combine($users, 
'{n}.User.id',array('%s
%s','{n}.User.first_name','{n}.User.last_name'));
                return $users;
        
            }   

which then  I can call when I need it like

$usersListForSelectMenu = $this->Dingus->User->find('names',array
(stuff in here))





On Tue, Oct 28, 2008 at 6:43 AM, mario <[EMAIL PROTECTED]> wrote:
>
> Thanks. But Chad's suggestion works fine for me.
> I suggest you also try it. =)
>
>
> On Oct 28, 6:29 am, "Liebermann, Anja Carolin"
> <[EMAIL PROTECTED]> wrote:
>> Hi Mario,
>>
>> I have similar situations in my application and I guess there is no 
>> automatic way to do it.
>>
>> So what I do is get the data wth all fields and loop through them and create 
>> another array with value = id and shown sring firstname + family name.
>>
>> If there is a more elegant solution I don't know it.
>>
>> Anja
>>
>> -----Ursprüngliche Nachricht-----
>> Von: cake-php@googlegroups.com [mailto:[EMAIL PROTECTED] Im Auftrag von mario
>> Gesendet: Sonntag, 26. Oktober 2008 16:09
>> An: CakePHP
>> Betreff: concatenation in find('list')
>>
>> Hi guys,
>>
>> I have this following code snippet from my controller:
>>
>> $salespeople = $this->Campaign->Salesperson->find('list',
>>                                   array('order' => 'Salesperson.firstname 
>> ASC',
>>                                           'fields' => 
>> array('Salesperson.id', 'Salesperson.firstname')));
>>
>> In my view, I have this following line:
>>
>> echo $form->input('salesperson_id');
>>
>> This codes would create a combobox in my view that would have a key: 
>> salesperson.id and value: salesperson.firstname
>>
>> Now here is what I want to do:
>>
>> I also have a field 'lastname' under my salespeople table and I want it to 
>> be concatenated to the salespersons'
>> firstname (with a space between the firstname and lastname).
>>
>> I can't just have the firstname in my combobox because there is a high 
>> possibility that there would be duplicates for firstnames.
>> It would be better if firstname and lastname would be displayed in my view's 
>> combobox and still would match to a corresponding salesperson.id.
>>
>> Is there a way to do this?
>>
>> Thanks,
>> Mario
> >
>

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to