How Cake PHP pass parameters from one controller action to another view - cakephp

I have a search method in my WorkersController.php like following:
public function search() {
$conditions = array();
if(!empty($this->request->data)){
foreach($this->request->data['Search'] as $field => $search_condition ) {
if(!empty($search_condition))
$conditions["$field LIKE "] = "%$search_condition%";
}
}
if(!empty($conditions)){
$this->Worker->recursive = 0;
$workers = $this->Worker->find('all',array('conditions' => $conditions));
}
$this->redirect(array('action' => 'index','search' ));
}
IN the method I call redirect(), then the page goes to index.ctp, where I want to fetch $workers like this:
if($this->request->params['pass']==array('search')){
if (empty($workers)){
echo('No result found!');
}else{
foreach ($workers as $worker){
//do something
}
}
}
But I just can't fetch $workers, how can I pass it from search() to index.ctp?
Thanks a lot!

You could try and use Session for this case.
//in controller1
$this->Session->write('worker', $workers);
//in controller2
$workersData = $this->Session->read('worker');

yes you can use as per below syntax to
$this->redirect(array('controller' => 'workers', 'action' => 'index', 'pass' => 'param', 'pass1' => 'param1'));
for more detail you can use doc

Related

Create new hasMany children from parent controller

I have two controllers "Events" and "Activities" and both have many "Attendees".
$this->hasMany('Attendees')
->setClassName('Attendees')
->setForeignKey('foreign_id')
->setConditions(array('Attendees.class' => 'Activity'))
->setDependent(true);
I am using a class and a foreign_id in my Attendees table to link them. I would like to create addAttendee() function in my ActivitiesController for example to add a new attendee, but I am not sure how to proceed.
public function addAttendee($id = null)
{
$activity = $this->Activities->get($id, ['contain' => ['Venues', 'Contacts']]);
if ($this->request->is('post'))
{
??
}
$this->set(compact('activity'));
}
I found some documentation on saving with association but not on creating new association.
You can create a new Attendee entity then link it to your $activity.
$data = ['first_name' => 'blah', 'last_name' => ... fields]);
$attendee = $this->Attendee->newEntity($data);
$this->Attendee->link($activity, [$attendee]);
source: https://api.cakephp.org/4.2/class-Cake.ORM.Association.HasMany.html#link()
I proceed this way. It's working but not sure if it's the right way to proceed ??
public function addAttendee($id)
{
$activity = $this->Activities->get($id, ['contain' => ['Venues', 'Contacts']]);
$attendee = $this->Activities->Attendees->newEmptyEntity();
if ($this->request->is('post'))
{
$attendee = $this->Activities->Attendees->patchEntity($attendee, $this->request->getData() + ['class' => 'retreat', 'foreign_id' => $id]);
if ($this->Activities->Attendees->save($attendee))
{
$this->Flash->success(__('The attendee has been saved.'));
return $this->redirect(['action' => 'view', $id]);
}
$this->Flash->error(__('The attendee could not be saved. Please, try again.'));
}
$this->set(compact('activity', 'attendee'));
}

Retrive data using cakephp

I am new to cakephp and i want to find data that has been created
This is my sql function works
Select Trv_No from Ticket_LO
This is my model in cakephp
$ticket = $this->Ticket->find('first', array('conditions' => array('AND' => array('Ticket.TRV_No' => $trv_no)));
if(empty($ticket))
{
$table_name = 'Ticket_L0';
$this->Ticket->setSource($table_name);
//$ticket = $this->Ticket->find('first', array('conditions' => array('Ticket.TRV_No' => $trv_no)));
$ticket = $this->Ticket->find('first', array('conditions' => array('AND' => array('Ticket.TRV_No' => $trv_no, 'Ticket.HIDDEN_STAT LIKE' =>'0'))));
if(empty($ticket)) { return false; } else { return true; }
}
else
{ return true; }
}
First things first,
You don't need to set $this->Ticket->setSource($table_name); in your model.
You can use $this->find
I'm not sure what do you really want. But, I guess you want something like that.
$ticket = $this->find('first', array(
'conditions' => array(
'Ticket.TRV_No' => $trv_no,
'Ticket.HIDDEN_STAT'=> 0 //if you want to find 0 only, you don't need LIKE. But, if you want some string, you can use something like 'Ticket.HIDDEN_STAT LIKE'=>'yourString%'
)
)
);
And, you can add your condition after that.
if(!empty($ticket)) return true;
else return false;

CakePHP Controller Method Variables

I am trying to define a variable that will pass into the Action of a Controller as follows
public function index($branch = null) {
//padmanabha-nagar
//jp-nagar
$branch = "padmanabha-nagar";
$this->loadModel('Branch');
// Check if the Branch is valid
if(!empty($branch)){
$this->Branch->recursive = -1;
$branch_result = $this->Branch->find('first', array('order' => array('Branch.name ASC'), 'conditions' => array('Branch.slug' => $branch)));
//debug($branch_result);
// Fetch the Branch ID
if(!empty($branch_result)){
$branch_id = $branch_result['Branch']['id'];
} else {
return $this->redirect(array('controller' => 'pages', 'action' => 'home'));
}
// Fetch the Gallery Images
$this->Gallery->recursive = -1;
$this->Paginator->settings = $this->paginate;
$galleries = $this->Gallery->find('all', array('conditions' => array('Gallery.branch_id' => $branch_id)));
//debug($galleries);
}
$this->set(compact('galleries', 'branch_result'));
}
But when i try to access $branch it does not seem to be set. Is there any other configuration i need to do so that i can get the content?
You are over writing the input variable
public function index($branch = null) {
//padmanabha-nagar
//jp-nagar
$branch = "padmanabha-nagar"; <= you are over writing the input variable
...
You could do this
$branch = !empty($branch)?$branch:"padmanabha-nagar";
Try to add branch to this line as follows:
$this->set(compact('galleries', 'branch_result', 'branch'));
You can create a variable to class using
$branches = null;
instead of to action, then use the variable using
$this->branches

CakePHP Pagination: how can I sort by multiple columns to achieve "sticky" functionality?

I see that this paginate can't sort two columns at the same time ticket is still open, which leads me to believe that what I'm trying to do is not possible without a workaround. So I guess what I'm looking for is a workaround.
I'm trying to do what many message boards do: have a "sticky" function. I'd like to make it so that no matter which table header link the user clicks on to sort, my model's "sticky" field is always the first thing sorted, followed by whatever column the user clicked on. I know that you can set $this->paginate['Model']['order'] to whatever you want, so you could hack it to put the "sticky" field first and the user's chosen column second. The problem with this method is that pagination doesn't behave properly after you do it. The table header links don't work right and switching pages doesn't work right either. Is there some other workaround?
User ten1 on the CakePHP IRC channel helped me find the solution. I told him that if he posted the answer here then I would mark it as the correct one, but he said I should do it myself since he doesn't have a Stack Overflow account yet.
The trick is to inject the "sticky" field into the query's "order" setting using the model's "beforeFind" callback method, like this:
public function beforeFind($queryData) {
$sticky = array('Model.sticky' => 'DESC');
if (is_array($queryData['order'][0])) {
$queryData['order'][0] = $sticky + $queryData['order'][0];
}
else {
$queryData['order'][0] = $sticky;
}
return $queryData;
}
What you can do is code it in the action. Just create the query you want when some parameters exist on the URL. (parameters has to be sent by GET)
For example:
public function posts(){
$optional= array();
if(!empty($this->params->query['status'])){
if(strlower($this->params->query['status']=='des')){
$optional= array('Post.status DESC');
}
else if(strlower($this->params->query['status']=='asc')){
$optional= array('Post.status ASC');
}
}
if(!empty($this->params->query['department'])){
//same...
}
//order first by the sticky field and then by the optional parameters.
$order = array('Post.stickyField DESC') + $optional;
$this->paginate = array(
'conditions' => $conditions,
'order' => $order,
'paramType' => 'querystring',
);
$this->set('posts', $this->paginate('Post'));
}
I have used something similar to filter some data using $conditions instead of $order and it works well.
You can use custom field for sorting and update pagination component.
Controller code
$order['Document.DATE'] = 'asc';
$this->paginate = array(
"conditions"=> $conditions ,
"order" => $order ,
"limit" => 10,
**"sortcustom" => array('field' =>'Document.DATE' , 'direction' =>'desc'),**
);
Changes in pagination component.
public function validateSort($object, $options, $whitelist = array()) {
if (isset($options['sort'])) {
$direction = null;
if (isset($options['direction'])) {
$direction = strtolower($options['direction']);
}
if ($direction != 'asc' && $direction != 'desc') {
$direction = 'asc';
}
$options['order'] = array($options['sort'] => $direction);
}
if (!empty($whitelist) && isset($options['order']) && is_array($options['order'])) {
$field = key($options['order']);
if (!in_array($field, $whitelist)) {
$options['order'] = null;
}
}
if (!empty($options['order']) && is_array($options['order'])) {
$order = array();
foreach ($options['order'] as $key => $value) {
$field = $key;
$alias = $object->alias;
if (strpos($key, '.') !== false) {
list($alias, $field) = explode('.', $key);
}
if ($object->hasField($field)) {
$order[$alias . '.' . $field] = $value;
} elseif ($object->hasField($key, true)) {
$order[$field] = $value;
} elseif (isset($object->{$alias}) && $object->{$alias}->hasField($field, true)) {
$order[$alias . '.' . $field] = $value;
}
}
**if(count($options['sortcustom']) > 0 )
{
$order[$options['sortcustom']['field']] = $options['sortcustom']['direction'];
}**
$options['order'] = $order;
}
return $options;
}
Easy insert 'paramType' => 'querystring',
Show Code Example:
$this->paginate = array(
'conditions' => $conditions,
'order' => array(
'Post.name' => 'ASC',
'Post.created' => 'DESC',
),
'paramType' => 'querystring',
);
$this->set('posts', $this->paginate('Post'));

Cakephp 2 model beforeSave() not being called

I can't figure out why this code isn't working. The beforeSave is not being called. It's supposed to fail the save and put some lines in the debug log, but it actually does save OK and no lines are written in the debug log.
<?php
class Link extends AppModel {
var $name = "Link";
var $belongsTo = array('Category' => array('className' => 'Category', 'foreignKey' => 'category_id'));
public function beforeSave(){
if ($this->data[$this->alias]['id'] == null) {
$this->log("new record", 'debug');
$link = $this->find('first',array('conditions' => array('Link.status = 1 AND Link.category_id = '.$this->data[$this->alias]['category_id']), 'order' => array('Link.order DESC') ));
if (is_null($link)) {
$this->data[$this->alias]['order'] = 1;
}else{
$this->data[$this->alias]['order'] = $link['Link']['order'] + 1;
}
}
else {
$this->log("old record", 'debug');
}
return false;
}
}
?>
I am launching a save in the controller like this:
public function add($category_id = null)
{
if ($category_id == null) {
$this->Session->setFlash(__('Category id cant be null'),'default', array('class' => 'error-message'));
$this->redirect(array('action' => 'index', 'controller' => 'categories'));
}
else
{
if($this->request->is('post'))
{
$this->Link->create();
$this->Link->set('category_id' => $category_id));
if($this->Link->save($this->request->data))
{
$this->Session->setFlash(__('The link has been saved'),'default', array('class' => 'success'));
$this->redirect(array('action' => 'index/'.$category_id));
}
else
$this->Session->setFlash(__('The link could not be saved. Please, try again.'),'default', array('class' => 'error-message'));
}
$this->set('category_id',$category_id);
}
}
Another question in StackOverflow points out that the beforeSave method needs to be declared in the Model. I've also done the same thing with another model.
Here's some general advice and some comments on your code for an answer:
1) If a model callback or any model method isn't working, make sure the correct model is being used and not the default model (AppModel). Check filename, class name, extension (in your case), and location.
2) You're using conditions array incorrectly (in this case).
array('conditions' => array('Link.status = 1 AND Link.category_id = '.$this->data[$this->alias]['category_id'])
You should really be doing:
array('conditions' => array('Link.status' => 1, 'Link.category_id' => $this->data[$this->alias]['category_id'])
3) Your redirect is being used wrong
$this->redirect(array('action' => 'index/'.$category_id));
Should be:
$this->redirect(array('action' => 'index', $category_id));
As written, your save() will always fail with this beforeSave(). beforeSave() must return true in order for the save function to succeed.
In fact, yours appears to always return false, guaranteeing a failed save.
From the Cake manual:
Be sure that beforeSave() returns true, or your save is going to fail.

Resources