Re: creating a selectTag from an enum in the model

2006-07-21 Thread John Zimmerman [gmail]
I posted a solution for my enum problem as a snippet if anyone is interested.It needs some optimization and checking for robustness,  but it is a more generic solution that can be re-used in all your models/controllers.
http://cakeforge.org/snippet/detail.php?type=snippet&id=112If anyone has suggestions on how to make this more generic and to support more databases please post back with improvements.
I am only working with MySQLOn 6/12/06, naryga <[EMAIL PROTECTED]> wrote:
I've been dealing with the same issue.  If you use scaffolding, itworks just fine, producing a very nice select box.  If you use
bake.php, it produces LOT'S of errors.  Shold be consistent.  It seemslike there should be something in the manual about dealing with veriouscolumn types.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake PHP" 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  -~--~~~~--~~--~--~---


Re: creating a selectTag from an enum in the model

2006-06-12 Thread naryga

I've been dealing with the same issue.  If you use scaffolding, it
works just fine, producing a very nice select box.  If you use
bake.php, it produces LOT'S of errors.  Shold be consistent.  It seems
like there should be something in the manual about dealing with verious
column types.


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



Re: creating a selectTag from an enum in the model

2006-06-10 Thread John Zimmerman [gmail]
I will add it to the enhancement list then.The only reason I might consider it a bug is that if you bake the views/model/controller and the enum column is in your table, the code that is generated renders errors because of it.  I have not tested it with scaffolding however, but I assume the same would happen.
I baked this particular model/view/controller.Either way not a huge deal, just something that would be nice to have an "official" solution for.Thanks!
On 6/10/06, nate <[EMAIL PROTECTED]> wrote:
I don't really see it as a bug, since Cake makes the data available, asin the solution above, but I could see doing some sort of enhancement.

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


Re: creating a selectTag from an enum in the model

2006-06-10 Thread nate

I don't really see it as a bug, since Cake makes the data available, as
in the solution above, but I could see doing some sort of enhancement.


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



Re: creating a selectTag from an enum in the model

2006-06-09 Thread jzimmerman

Has anyone submitted a bug report on trac for this?

Just curious.  I just ran into the same situation.  I will try to
formalize a request to have this addressed if no one else has.


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



Re: creating a selectTag from an enum in the model

2006-05-13 Thread Daneel

OK. I have updated the code to use the internal description instead of
using a custom query:

function generateDifficultyList() {
foreach($this->_tableInfo->value AS $field) {
if($field['name'] == 'difficulty') {
$enum = $field['type'];
break;
}
}

foreach(split("','", substr($enum, 6, -2)) AS $num => $name)
$return[$name] = $name;

return $return;
}

I know, it uses a private variable but it is a Model private variable
and I'm using it inside the Model so I don't see any problem :-P.


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



Re: creating a selectTag from an enum in the model

2006-05-13 Thread Daneel

Seems that the scaffolding code gets the enum values from an internal
variable. I think that cake gets the content of this variable in the
DESC tablename queries it makes.

If we use this code we could save the "SHOW COLUMNS" queries.


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



Re: creating a selectTag from an enum in the model

2006-05-13 Thread Jon Bennett

hmm,

seems a bit odd to me, because when you use Scaffolding, Cake
automatically generates a select from ENUM columns, so work arounds
shouldn't be required.

in /cake/libs/controller/controller.php

function generateFieldNames( $data = null, $doCreateOptions = true  )
{
 // major truncation of code!
case "enum":
 $fieldNames[ $tabl['name']]['type'] = 'select';
 $fieldNames[ $tabl['name']]['options'] = array();
 $enumValues = split(',', $fieldLength );
 foreach ($enumValues as $enum )
 {
 $enum = trim( $enum, "'" );
 $fieldNames[$tabl['name']]['options'][$enum] = $enum;
 }
 $fieldNames[ $tabl['name']]['selected'] =
$data[$model][$tabl['name']];
 break;

duno if that helps at all!

doh

jb


-- 


jon bennett
t: +44 (0) 1225 341 039 w: http://www.jben.net/
iChat (AIM): jbendotnet Skype: jon-bennett

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



Re: creating a selectTag from an enum in the model

2006-05-13 Thread Daneel

Regarding the performance difference, I know that for the queries like
"SHOW COLUMNS ...", MySQL doesn't use indexes. Also, the result has to
be parsed in the PHP code. Instead, if you use a separate table, MySQL
can use indexes (if defined :-) and you don't have to parse the result.

On the other hand, using a enum column saves the DB to make an
additional JOIN when making SELECTs.

So, the use of an enum or a separate table, depends on the relation
between the number of SELECTs and the number of times you need to query
the enum column definition :-)


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



Re: creating a selectTag from an enum in the model

2006-05-12 Thread calzone

Wow, we cross-posted.

Looks like your approach is basically the same as mine.

:)


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




Re: creating a selectTag from an enum in the model

2006-05-12 Thread calzone

No replies?   I guess I'm imagining things then and this isn't
supported by cake?

To help make this topic a better reference source should future people
have a question on this, use the following code in your model:


function getMyEnum()
{
$rows=$this->query("SHOW COLUMNS FROM myTable LIKE
'myEnumColumnName'");
foreach ($rows[0][0] as $key => $value)
{
if ($key=='Type')
{
 preg_match_all("/'(.*?)'/", $value, $matches);
 $arryEnum= $matches[1];
}
}
return $arryEnum;
}

(Obviously, replacing getMyEnum, myTable and myEnumColumn with whatever
your situation dictates.

And then in the controller, add:

$this->set('Myvar',$this->MyModel->getMyEnum());


This could be abstracted further by passing the table name and column
name to the function, but then, maybe such functionality might make a
good addition to the cake model?

However, the lesson I've learned from this is that you still require a
separate db query to load the enum array.  In that case, maybe it would
just be better to store enum values in a separate table with their own
model.  I don't know what kind of performance difference there may be
between the two approaches. But in general, I avoid enum because
changing the values involves an ALTER TABLE and the sql is
non-portable.  But there are cases where it is useful, and retrieving
selected values doesn't involve a join.


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



Re: creating a selectTag from an enum in the model

2006-05-12 Thread Daneel

I also faced this issue and the only solution I found was to use a
custom query. That is I created a funcion in the model that queries the
database for the enum values and creates an array based on it:

function generateDifficultyList() {
$ret = $this->query("SHOW COLUMNS FROM `recipes` LIKE
'difficulty';");

// WORKARROUND: The $ret array has different keys if
using MySQL.4 or 5
$key = array_keys($ret[0]);

foreach(split("','", substr($ret[0][$key[0]]['Type'],
6, -2)) AS $num => $name)
$return[$name] = $name;

return $return;
}

Then in the Controller I can do $this->set('difficulties'
$this->Recipe->generateDifficultyList()).

Hope this helps.


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



creating a selectTag from an enum in the model

2006-05-11 Thread calzone

I could have sworn I saw this covered at some point, but now I can't
find it here, in the wiki, the api, or the manual.

I have a model with a db table, say cars, and one of my columns is an
enum, say, type('coupe','sedan','wagon','truck').

I'm trying to build a selectTag in my view that is generated from the
enum list.

I tried putting this in the Model:

function getTypes()
{
$this->set('Types', 
$this->type->generateList());
}

And then in the controller, using $this->Car->getTypes();

And I also tried doing this just from the controller without using the
model.  Nothing's working.  I haven't even gotten to the stage of using
it to populate my selectTag, which is going to be expecting a 2D array
while the enum list is only going to be 1D.

What am I doing wrong as far as generateList() and how will I shoehorn
a 1D array into a 2D socket (or will it be handled for me automatically
--(i hope!)-- by creating a key equal to the value?


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