Re: Please help with the structure of my code

2007-06-19 Thread dmorris

Thanks Grant.

That looks like a good way of doing things. I'll have a play doing it
that way, and see what I come up with.



--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Please help with the structure of my code

2007-06-18 Thread dmorris

Hi all.

I am an experienced php developer but a very inexperienced Cake
developer. I have previously written my own MVC code (though i suspect
I blurred the M,V and the C a bit much ;-) )

I am struggling with working out the best way of doing things, and I
can't find any decent structure of code discussions.

Rather than waffling, I'll give a specific example.

I have a Locations model which has an id, and city and a postcode.

I created a locations/find, which was passed the data from a form.

I then started to extend the find function so that it could also take
in data from the url eg /locations/find/leeds

Is this the optimal way of doing it, since my find function now has to
deal with the data in a different way.

The other option would be to create /locations/city/leeds, but I
believe this would require another view, and that seems like
duplication of effort.

If anyone could point me at a worked example with discussion ala the
blog tutorial from the manual, but that goes into *alot* more breadth
- I'm happy reading functions specs, i just wan't to make sure I am
doing things in as close to an optimal way as I can, or else I believe
I will lose the rapid part of the Rapid application development!

Many thanks for your help..

Regards

Duncan


--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---



Re: Please help with the structure of my code

2007-06-18 Thread Grant Cox

With in-query search like this I use named parameters - ie
/locations/find/term:leeds/field:city

This way the parameter order is not important, and it is easy to
substitute default values for anything not provided.  My specific
implementation is something like:


function index()
{
$conditions = array();

// get any conditions from the search / find url parameters
$url_conditions = $this-_url_conditions('MyModel');
if ( !empty($url_conditions) ){
$conditions[] = $url_conditions;
}

$data = $this-MyModel-findAll( $conditions );

$view_data = array(
'data' = $data
);
$this-set( $view_data );
}


function _url_conditions( $model )
{
$conditions = array();

// check if named parameters were provided
$passed_search_term = val( @$this-passedArgs['search'], @$this-
passedArgs['find'] );

if ( !empty($passed_search_term) ){
$passed_search_term = urldecode($passed_search_term);

// we want to search for this value
loadModel( $model );
$this-{$model} = new $model();

$search_fields = array();
$search_conditions = array();

if ( !empty($this-passedArgs['field']) ){
// a specific field was given
$search_fields = array( 
$this-passedArgs['field'] );

} else {
// use all of the model's search fields
if ( !empty($this-{$model}-searchFields) and 
is_array($this-
{$model}-searchFields) ){
$search_fields = 
$this-{$model}-searchFields;
} else {
$search_fields = array( 'title', 
'name', 'description' );
}
}

// now check that these fields exist
foreach ( $search_fields as $search_field ){
if ( $this-{$model}-hasField($search_field) ){
$field_type = 
$this-{$model}-getColumnType($search_field);

if ( $field_type == 'string' or 
$field_type == 'text' ){
$search_conditions[ 
$model.'.'.$search_field ] = 'LIKE %'.
$passed_search_term.'%';
} else {
$search_conditions[ 
$model.'.'.$search_field ] =
$passed_search_term;
}
}
}

if ( !empty($search_conditions) ){
if (count($search_conditions)  1){
$conditions['OR'] = $search_conditions;
} else {
$conditions = $search_conditions;
}
}
}

return $conditions;
}


--~--~-~--~~~---~--~~
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?hl=en
-~--~~~~--~~--~--~---