So in your case you would define something like this in your Tree
model:
var $virtualFields = array(
'name' => 'CONCAT(Tree.cultivar, "-", Tree.stage)'
);
Then in your find:
$crop = $this->Tree->find('list', array('fields' => array('id',
'name')));
However you can further simplify your query because id and name are
defaults for find('list')
$crop = $this->Tree->find('list');
Have fun,
Nick
On Mar 9, 4:44 am, nurvzy <[email protected]> wrote:
> If you're on 1.3 use Virtual Fields
> --http://book.cakephp.org/view/1608/Virtual-fields.
>
> So this is a pretty common gotcha with CakePHP. If you use one of
> those SQL tricks the result will be outside cake's array scope. If
> you were to do a find('all'....) on that you would see your CONCAT
> field in the key 0 of $crop. As such, when using find('list') the key
> 0 is not within CakePHP's model scope which in turn is ignored giving
> you what you see now.
>
> Virtual Fields (new in 1.3) to the rescue. You can define these
> special "not real" fields and they react like you would expect any
> normal field would within a find, condition, or pagination. Pretty
> cool!
>
> Hope that helps,
> Nick
>
> On Mar 9, 3:45 am, Eleazar <[email protected]> wrote:
>
> > Quick update: the following also produces the above result
> > $crp = $this->Tree->find('list',array('fields'=>array('id','cultivar
> > AS name')));
>
> > but
>
> > $crp = $this->Tree-
>
> > >find('list',array('fields'=>array('id','cultivar')));
>
> > works fine. Again, I'm on 1.3 beta, maybe this has been resolved?
>
> > Thanks
>
> > On Mar 9, 12:40 pm, Eleazar <[email protected]> wrote:
>
> > > Hi, I have the following code:
>
> > > $crop =
> > > $this->Tree->find('list',array('fields'=>array('id','CONCAT(Tree.cultivar,\'
> > > -
>
> > > \' ,Tree.stage) AS name')));
> > > debug($crop);
>
> > > It produces the following SQL:
>
> > > SELECT `Tree`.`id`, CONCAT(Tree.cultivar,' - ' ,Tree.stage) AS name
> > > FROM `trees` AS `Tree` WHERE 1 = 1
>
> > > When I debug $crop I get this:
> > > Array
> > > (
> > > [1] =>
> > > )
>
> > > There is only one entry in the Tree table so the 1 is fine, but there
> > > is no data... when I run the query directly in phpmyadmin I get
> > > id name
> > > 1 apples - year1
>
> > > The fact that $crop has a key but no value seems to be a bug? I'm not
> > > sure how to report this if indeed it is. I'm using 1.3 beta.
>
> > > Any help or comments would be appreciated!
Check out the new CakePHP Questions site http://cakeqs.org and help others with
their CakePHP related questions.
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