»
S
I
D
E
B
A
R
«
Sponsored Links


Using DISTINCT in a CakePHP find function
Nov 11th, 2009 by wood

Hello,

I am writing a CakePHP 1.2 app. I have a list of people that I want the user to be able to filter on different fields. For each filterable field, I have a drop down list. Choose the filter combination, click filter, and the page shows only the records that match.

In people_controller, I have this bit of code:

$first_names = $this->Person->find('list', array(
    'fields'=>'first_name',
    'order'=>'Person.first_name ASC',
    'conditions'=> array('Person.status'=>'1')
));
$this->set('first_names', $first_names);

(Status = 1 because I am using a soft delete.)

That creates an ordered list of all first_names. But duplicates are in there.

Digging around in the Cookbook, I found an example using the DISTINCT keyword and modified my code to use it.

$first_names = $this->Person->find('list', array(
    'fields'=>'DISTINCT first_name',
    'order'=>'Person.first_name ASC',
    'conditions'=> array('Person.status'=>'1')
));

This gives me an SQL error like this:

Query: SELECT `Person`.`id`, DISTINCT `Person`.` first_name` FROM `people` AS `Person`   WHERE `Person`.`status` = 1   ORDER BY `Person`.`first_name` ASC

The problem is obvious. The framework is adding Person.id to the query. I suspect this comes from using 'list'.

I will use the selected filter to create an SQL statement when the filter button is clicked. I don't need the is field, but can't get rid of it.

Thank you, Frank Luke

getting referer from auth in cakePHP
Nov 11th, 2009 by wood

I have a link on the main page that is only accessible if they are logged in. However, if this link is clicked, I want to show a custom error message on the login page (a custom 'Message.auth').

i.e. I want (pseudo code)

if (referer == '/users/reserve'){
    Message.auth = 'Please log in to reserve tickets';
}
else {
    Message.auth = 'Please log in to access that page';
}

Where would I put this bit of code?

Action called from many other places, how to handle a “Back” button?
Nov 11th, 2009 by wood

I have an action (view for example) in a controller that is called from multiple other actions in other controllers. How is the best way to create a “Back” button that will take me back to the page that got me here?

I’ve used named parameters like “back_controller” and “back_action” and that works fairly well but they get awkward when the page has a form that gets submitted. I have to be sure to pass those parameters as hidden fields or in the form url and then look for them after the form has been processed.

Is there some kind of stack or other solution that anyone else has come up with that handles this situation better? I see this problem in a lot of my projects and I’ve yet to come up with a good solution.

how to set a selected value in $form->select function? need help
Nov 11th, 2009 by wood

in my edit function, i need to reselect for the field again to save it. how can i added a 'selected'=>$addresscountry field in my $form->select function??

this is my code..

echo $country->select('Address.txtother_country','Please Choose Your Country'

which $country is 1 of the helper that include by the page, let user to select country. i need it automatic to refer back to the previous data which had save, means add a selected value in my edit function.

any 1 can help? thanks..

Cake PHP pagination problem
Nov 11th, 2009 by wood

I have a controller and view. in the view it will displays all the records frm the database. also there is a searchby form in the top of that view page. if we specify a search term, and submit the form , the view will only displays the records resulting from that search. the results are paginated. but the problem is when i perform a search and click on the next page of the results , it will shows all the results .. here is my code.

View page index.ctp

echo $form->create('Apartment', array('action'=>'index'));
echo form->input('searchBy',array('type'=>'select','options'=>array('Id'=>'Id','User'=>'User','time'=>'Updated time')));
echo $form->input('query', array('type'=>'text', 'label'=>'Search Term'));
echo $form->end(array('name'=>'submit', 'label'=>'Search'));




<th class="actions"><?php __('');?></th>
<th><?php echo $paginator->sort('id');?></th>
<th><?php echo $paginator->sort('Headline');?></th>
<th><?php echo $paginator->sort('Campaign','Campaign.Name');?></th>
<th><?php echo $paginator->sort('User', 'User.name');?></th>
<th><?php echo $paginator->sort('modified');?></th>
<th><?php echo $paginator->sort('status');?></th>
<th class="actions"><?php __('Actions');?></th>

Controller index function

if (!empty($this->data)) {
// Search 
switch($this->data['Apartment']['searchBy'])
{
case 'Id':
        $apartments = $this->paginate(NULL, array('Apartment.id' => $this->data['Apartment']['query']));
        break;
case 'User':
	$apartments = $this->paginate(NULL, array("User.name Like '%".$this->data['Apartment']['query']."%'"));
	break
case 'time':
	$apartments = $this->paginate(NULL, array("Apartment.modified Like '%".$this->data['Apartment']['query']."%'"));
	break;
}
}
else {
        $apartments = $this->paginate();
}


»  Substance: PHP Frameworks   »  SiteMap