how to add search by date cakePHP - cakephp

I have the following code that displays a list of details from my Database.
It is paginated as well.
I want to add a search function that someone can search the provided data using " Date From - Date To "
Currenty source :
$tapplicant= $this->paginate = array(
'fields' => array(
'Tapplicant.*',
'Toutcome.*'
),
'joins' => array(
array(
'table' => 'toutcome',
'alias' => 'Toutcome',
'type' => 'INNER',
'conditions' => array('Tapplicant.AppID = Toutcome.AppID' )
)
),
'conditions' => array(
'Tapplicant.AppDate >=' => date('y-m-d')
),
'order' => array('Tapplicant.AppDate' => 'DESC'),
'limit' => 15
);
$this->set('tapplicant', $this->paginate());
I display the data as followed :
<?php foreach ($tapplicant as $tapplicant) : ?>
<tr>
<td><?=$tapplicant['Tapplicant']['AppID']; ?></td>
<td><?=$tapplicant['Tapplicant']['AppAffID']; ?></td>
<td><?=$tapplicant['Tapplicant']['FirstName']; ?></td>
<td><?=$tapplicant['Tapplicant']['LastName']; ?></td>
<td><?=$tapplicant['Tapplicant']['AppDate']; ?></td>
<td><?=$tapplicant['Tapplicant']['AppDomain']; ?></td>
<td>£<?=$tapplicant['Toutcome']['LenderCommission']; ?></td>
<td><?=$tapplicant['Toutcome']['LenderName']; ?></td>
</tr>
After doing some research and trying things like :
$this->Post->find('all', array('conditions'=>array('AppID'=>5, 'DATE(AppDate)'=>'CURDATE()')));
I cant get it to work.
If someone could please assist on help with the controller and view side.

Use BETWEEN:
$conditions = array(
'Tapplicant.AppDate BETWEEN ? AND ?' => array(
$start,
$end,
)
);
Read more about complex find conditions in CakePHP 2 here: http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions

Related

Cakephp foreach data for options in select (form)

in my project I have database
"Products" hasAndBelongsToMany "Sizes"
"ProductsSize" belongsTo "Products"
"ProductSize" belongsTo "Size"
In ProductsSize, one product_id may have many size_id.
hence, i want to do a form that have a select type input that list down all the size_id where the product_id = $id.
What i have done is:
in controller:
$size = $this->Product->ProductsSize->find('all', array(
'conditions' => array('ProductsSize.product_id' => $id),
'fields' => array('ProductsSize.size_id'),
));
in view:
<?php echo $this->form->create('Cart', array('action' => 'add')); ?>
<?php echo $this->form->input('size_id', array('label' => 'Size', 'options' => $size)); ?>
then i got error: Undefined index: ProductsSize
but when i put foreach, the data shown:
<?php foreach ($size_apparel as $size): ?>
<?php echo $size['ProductsSize']['size_id'];?><br/>
<?php endforeach; ?>
can anyone please help me to do the foreach in options.
The problem is that you are using an invalid array for the options. You are requesting:
$size = $this->Product->ProductsSize->find('all', array(
'conditions' => array('ProductsSize.product_id' => $id),
'fields' => array('ProductsSize.size_id'),
));
What you should be requesting is:
$size = $this->Product->ProductsSize->find('list', array(
'conditions' => array('ProductsSize.product_id' => $id),
));
This will return the options for the form field in the format it expects which is:
array(
{id} => {size},
{id} => {size},
{id} => {size},
{id} => {size},
...
)
I think you meant to say
<?php echo $this->form->input('size_id', array('label' => 'Size', 'options' => $size)); ?>
note 'options' instead of 'value'
$List = Set::classicExtract($size, '{n}.ProductSize.product_id');
$List will contain key value(ProductSize.product_id) pair.
OR you can change your query as
$size = $this->Product->ProductSize->find('list', array(
'conditions' => array('ProductSize.product_id' => $id),
));
Hope it will work for you

CakePhP association not displaying as expected

I am new to Cakephp and I am having trouble with the following association. What I am after is listing Parts in a table, along with their associated Source vendor. So in the View/index.ctp the table I am currently getting is
ID Source Part #
1 1 Part00001
but I would like the association to display
ID Source Part #
1 Vendor 1 Part00001
where the Source.name is from the mstr_sources db table, not the mstr_parts table.
Below are snippets from all the pertinent code
class MstrPart extends AppModel {
public $name = 'MstrPart';
public $belongsTo = 'MstrSource';
}
class MstrPartsController extends AppController {
public function index() {
$this->set('parts', $this->paginate());
}
}
// <!-- File: /app/View/Users/index.ctp -->
<?php foreach ($parts as $part): ?>
<tr>
<td><?php echo $part['MstrPart']['id']; ?></td>
<td><?php echo $part['MstrPart']['mstr_source_id']['name']; ?></td>
//Also tried the following, still fails
//<td><?php echo $part['MstrPart']['MstrSource']['name']; ?></td>
<td><?php echo $part['MstrPart']['name']; ?></td>
</tr>
// Output of the error message context.
$viewFile = 'C:\Dev\UniServerZ\www\campman\app\View\MstrParts\index.ctp'
$dataForView = array(
'parts' => array(
(int) 0 => array(
'MstrPart' => array(
[maximum depth reached]
),
'MstrSource' => array(
[maximum depth reached]
)
)
)
)
$parts = array(
(int) 0 => array(
'MstrPart' => array(
'id' => '1',
'mstr_source_id' => '1',
'created' => '2013-12-18 16:10:35',
'modified' => '2013-12-18 16:10:35',
'name' => 'Part00001',
'description' => 'Just some basic text, not the real thing.'
),
'MstrSource' => array(
'id' => '1',
'created' => '2013-12-19 12:23:22',
'modified' => '2013-12-19 12:23:22',
'name' => 'Vendor 1'
)
)
)
$part = array(
'MstrPart' => array(
'id' => '1',
'mstr_source_id' => '1',
'created' => '2013-12-18 16:10:35',
'modified' => '2013-12-18 16:10:35',
'name' => 'Part00001',
'description' => 'Just some basic text, not the real thing.'
),
'MstrSource' => array(
'id' => '1',
'created' => '2013-12-19 12:23:22',
'modified' => '2013-12-19 12:23:22',
'name' => 'Vendor 1'
)
)
include - APP\View\MstrParts\index.ctp, line 35
View::_evaluate() - CORE\Cake\View\View.php, line 929
View::_render() - CORE\Cake\View\View.php, line 891
View::render() - CORE\Cake\View\View.php, line 460
Controller::render() - CORE\Cake\Controller\Controller.php, line 952
Dispatcher::_invoke() - CORE\Cake\Routing\Dispatcher.php, line 192
Dispatcher::dispatch() - CORE\Cake\Routing\Dispatcher.php, line 160
[main] - APP\webroot\index.php, line 108
Is there something obvious that I am missing?
I think you would just need to change this line
<td><?php echo $part['MstrPart']['mstr_source_id']['name']; ?></td>
To this
<td><?php echo $part['MstrSource']['name']; ?></td>
EDIT: I'll explain a bit.
In cakephp, all joined models (either joined via the 'join' tag within the Model->find, a containable behavior, or via the default bindings established in the model file) are included in the results array via a key of their model name. *it's their model name, not the table name. See cakephp naming conventions for more.
Since you want the column from mstr_source and not mstr_part, you want to reference the data inside the MstrSource dictionary and not MstrPart

Paginator->sort using contain at CakePHP 2.2

I am using contain behavior and i want to be able to sort the data as always but I am having troubles.
It might be because the columns i want to sort are not columns on the paginate table but on the contain tables.
Subscriptions table only contains the "user_id" and the "post_id".
At the controller i have:
$this->paginate = array(
'conditions' => array('Subscription.user_id' => $this->Session->read('Auth.User.id')),
'paramType' => 'querystring',
'limit' => 20,
'contain'=> array('User', 'Post' => array('Priority', 'Status'))
);
$this->set('subscriptions', $this->paginate('Subscription'));
And in my view, this:
<!-- example of not working sort -->
<th class="td_subject"><?php echo $this->Paginator->sort('subject'); ?></th>
The data of subject, for example, is contained on the array $subscription with the normal format:
$subscription['Post']['subject']
And i print it this way:
foreach ($subscriptions as $subscription):
echo ...
echo '<td>'.h($subscription['Post']['subject'].'</td>';
echo ...
endforeach;
How can i use pagination in this case? Thanks.
Don't use contain in this case, manually join the tables:
$this->paginate = array(
'conditions' => array('Subscription.user_id' => $this->Session->read('Auth.User.id')),
'paramType' => 'querystring',
'limit' => 20,
'joins'=>array(
array(
'table'=>'users',
'alias'=>'User',
'conditions'=>array(
'Subscription.user_id = User.id
)
),
array(
'table'=>'posts',
...
),
array(
'table'=>'priorities',
...
),
array(
'table'=>'status',
...
)
)
);
Your view:
<?php echo $this->Paginator->sort('Post.subject', 'Subject'); ?>

Cakephp Undefined Index

When creating a view I am getting a undefined index error: Account on the line with $senderName['Account']['company_name']but when debugging the variable the array prints out
array(
(int) 0 => array(
'Account' => array(
'id' => '0',
'street' => 'SYSTEM',
'city' => 'SYSTEM',
'postcode' => '0',
'state' => 'SYS',
'country' => 'SYS',
'active' => true,
'company_name' => 'SYSTEM',
'abn' => '0'
),
'Template' => array(),
'User' => array(),
'Invoice' => array()
),
here is the code for my view
<?php foreach($invoice as $invoices):?>
<?php foreach($senderName as $senderName):?>
<?php foreach($receiverName as $receiverName):?>
<tr>
<tr>
<td align='center'><?php echo $senderName['Account']['company_name']; ?></td>
<td align='center'><?php echo $receiverName['Account']['company_name']; ?></td>
<td align='center'><?php echo $this->Form->Html->link($invoices['Invoice']['id'],
array('controller' => 'Invoices','action'=>'viewinvoice',$invoices['Invoice']['id'])); ?></td>
</tr>
<?php endforeach; ?>
<?php endforeach; ?>
<?php endforeach; ?>
and just in case here is my related function
$accounts2=$this->User->find('list', array(
'fields'=>array('account_id'),
'conditions' => array(
'id' => $this->Auth->user('id'))));
$invoices=$this->Invoice->find('all', array(
'conditions' => array(
'Invoice.receiver_id' => $accounts2)));
$sender=$this->Invoice->Find('list', array('fields'=>('sender_id')));
$receiver=$this->Invoice->Find('list', array('fields'=>('receiver_id')));
$senderName=$this->Account->Find('all', array(
'conditions' => array(
'id'=>array_values($sender))));
$receiverName=$this->Account->find('all', array(
'conditions' => array(
'id'=>array_values($receiver))));
debug($senderName);
$this->set('senderName', $senderName);
$this->set('accounts2', $accounts2);
$this->set('receiverName', $receiverName);
$this->set('sender',$sender);
$this->set('receiver',$receiver);
$this->set('invoice', $invoices);
}
You're computer is right. :)
$senderName['Account']['company_name']
does not exists.
$senderName['0']['Account']['company_name']
does.
The data comes in this format as they may be several account liked to senderName.
Edit:
Could you give the relationship in your models too?
I think you should review your code in view :
<?php foreach($senderName as $senderName):?>
<?php foreach($receiverName as $receiverName):?>
in foreach array_expression and value variable should be different but you have used same variable name, please use different names for this.

cakephp sort problem using join

why no work sort by "author"? Not throw any errors, just unsorted
function index()
{
$this->paginate = array(
'limit' => 5,
'order' => array(
'Post.id' => 'asc',
),
'fields' => array('Post.id', 'Post.title', 'User.name AS aut_name'),
'joins' => array(
array(
'table' =>'users',
'alias' =>'User',
'type' =>'LEFT',
'conditions' => array(
'Post.author = User.id'
)
)
)
);
$posts = $this->paginate();
$this->set('posts', $posts);
}
<tr>
<th><?php echo $paginator->sort('ID', 'id'); ?></th>
<th><?php echo $paginator->sort('Author', 'User.name'); ?></th>
<th><?php echo $paginator->sort('Tilte', 'title'); ?></th>
<th>Actions</th>
</tr>
The code suggests that the User.name field is being returned as "aut_name", maybe set the sort argument to use that field name instead?
<th><?php echo $paginator->sort('Author', 'aut_name'); ?></th>
Maybe you should do this way
in controller:
'fields' => array('Post.id', 'Post.title', 'User.name')
in view:
<th><?php echo $paginator->sort('Author', 'User.name'); ?></th>

Resources