On Feb 20, 4:48 am, John Andersen <[email protected]> wrote:
> Thanks :)
> I just wonder, why the CakePHP book states one thing, when using MySQL
> and another when using PostgreSQL in the section "Create virtual
> fields" at:http://book.cakephp.org/#Creating-virtual-fields-1609
>
> Have you tried to do accordingly? That is, changing the statement in
> the component to:
>
> [code]
> $modelObj->virtualFields = array ('name' => 'CONCAT($modelObj->primaryKey, ": 
> ", $modelObj->displayField)');
>
> [/code]
>

The line I'm using to create the virtual field does follow the
recommendation of the book page for mySQL. Your suggested alteration
is, I know, intended to create a properly formed SQL CONCAT command
but it violates php syntax. My current code does create the proper
CONCAT command for mySQL and has proper php syntax.

These side issues aside though, I seem to have discovered my problem
and it points to a subtle (or perhaps glaring?) misunderstanding of
Objects on my part.

The problem started when I found I needed to pass the Model object in
to my Component as a parameter or the Component would not have it to
work with. So I did this in the calling controller:
   $model = new Aro();
Then I passed the Model object along to method tree_crud in the
TreeCrud Component like this:
   $this->set('treecrud_data',$this->TreeCrud->tree_crud($model));

In the Component I made the virtualField for the Model as described
earlier. I also tried to make the virtual field for the instance of
the Model named $model in the Controller, before calling the Component
method. Both approaches generated a returned data array in this form:
Array
(
    [Aro] => Array
        (
            [id] => 1
            [parent_id] =>
            [model] => Group
            [foreign_key] => 1
            [alias] => administrators
            [lft] => 1
            [rght] => 4
        )

    [0] => Array
        (
            [Aro__name] => 1: administrators
        )
)

The solution was to avoid creating and passing in a new instance of
the Model. So the proper code for the call was
      $this->set('treecrud_data',$this->TreeCrud->tree_crud($this-
>{$this->modelClass}));

Then, inside my Component I could create the virtual field in the same
way I described previously. Violà! I get the proper result array:
Array
(
    [Aro] => Array
        (
            [id] => 1
            [parent_id] =>
            [model] => Group
            [foreign_key] => 1
            [alias] => administrators
            [lft] => 1
            [rght] => 4
            [name] => 1: administrators
        )
...

Thanks, Don





-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to