Hey folks,

As a note, there are many solutions to a problem, and all of them are
good ones, this is my approach to the problem of not being able to do
something in cake.

After banging my head in trying to get generateList to concatenate two
fields (for the purpose of using the data in a selectTag, example
lastname, firstname) I found a solution in the cake group.
 See $displayField - Displaying multiple fields

After looking it over, and thinking, why in the world would I want PHP
to iterate through rows of data and have the chance of running out of
memory (and I had it happen) when I can get the database server to do
it for me, before PHP gets the result set (and a lesser chance of
running out of memory).

My solution is a four pronged approach.

First this works for mysql 5.x and php5.x dunno about MSSQL or other
versions of PHP.

First create a view of the table you wish to have, being sure to define
your concatenated field. Example:

To create full_name out of the users.lastname, users.firstname I
deteremed the following query would give me the results.

select *, concat(users.lastname,',',' ',users.firstname) as full_name
from users

The last column is your concatenated data, full_name.

To create your view do the following (be sure to adjust for your tables
and such)
create view usersFullNames as select *, concat(lastname, ',','
',firstname) as full_name from users;

There's the first prong of the attack.

Next I went in to my User model and defined $useTable to equal
'usersFullNames'

There's the second prong of the attack. Now here's the bad news, since
mysql does not allow updates on views with derived columns (full_name
is derived because it is using the concat to come up with an answer)
there cannot be an update to the view. But don't tell cake that, tell
it something else.

The third prong of the attack is thus:

In your model add the following:
        function beforeSave()
        {
                                //This points User to the real table of
users
                $this->setSource('users');
                return true;
        }

And for the fourth prong add the following to the model
        function afterSave()
        {
                                //After the save point User back to
view of usersFullNames
                $this->setSource('usersFullNames');
                return true;
        }
(remember, to adjust your setSource tablename to taste or else you will
get forked up)

So far I haven't hit any snags, but I'm sure I will, but crossing my
fingers that I wont.

And that puts a fork in it, I'm done.

Hope this inspires someone. :)


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to