my controller .
$times=$this->Time->find('list',array('fields'=>$this->Time->virtualFields['name_price'] ));
$this->set('time',compact($times));
and i added below line in my model Time
public $virtualFields = array('name_price' => 'concat(Time.varaddress1, "-", Time.varaddress2)');
and my ctp file is below
<?echo $this->Form->input('intaddressid', array(
'options' => $time,'label'=>false,'empty' => '(Select Information)'
,'class' => 'form-control border_none'
));?>
but now in output i got nothing in select filed.!!
and when i echo $time i got `Array ( )
and i want in select field
<option>varaddress1,varaddress2</option>
You need to change your controller code like as :
$times=$this->Time->find('list',array('fields'=>array('Time.id','Time.name_price')));
$this->set(compact('times'));
Related
I am using the cakeDC search plugin for cakephp and I need to search by full name. I have stored the name in the database as separate first and last names. How would I concat the first and last name and then perform my search.
So my form input will search either member number or full name.
cakephp version 2.3.5
public $filterArgs = array(
'member_no' => array(
'type' => 'like' ,
'field' => array(
'member_no',
'name'
)
)
);
Update:
Here is what it would look like as a sql query:
SELECT
CONCAT_WS(' ', firstName,lastName) AS name
FROM
table
WHERE
name LIKE '%$keywords%'
Thanks to Mark here is what I added to the Model. All works great now.
public $virtualFields = array(
'name' => 'CONCAT(Member.first_name, " ", Member.last_name)'
);
If you just want the example above in a clean way why not using the plugin as documented? allowing it to do all the work?
public $virtualFields = array(
'full_name' => 'CONCAT_WS(' ', firstName,lastName)'
);
public $filterArgs = array(
'search' => array(
'type' => 'like' ,
'field' => 'full_name'
)
);
That's all there is to it. Or what is the issue?
I am using cakephp2.0 and want to integrate comment plugin but i got nothing .I was using commentDc plugin but its not working as my requirements.Because i am integreating my users login system with xenforo and commentDc plugin use Auth component so its not working properly.
Please let me know is there any simple comment plugin which i can integrate and modify as my needs.
Thanks,
Here's how I set up comments:
Comments table fields:
id
parent_type, matches the model name of the parent
parent_id
content
user_id, the sender
In any model that you want to be commentable, at this your associations:
public $hasMany = array(
'Comment' => array(
'className' => 'Comment',
'foreignKey' => 'parent_id',
'conditions' => array('Comment.parent_type' => 'question')
)
);
This is a view element:
<?php
/*
set variables:
$data : data of the parent
$type : the type of the parent
*/
if(!isset($name)) {
$name = 0;
}
foreach($data['Comment'] as $comment){
echo '<div class="comment">'.$comment['content'].
' - '.$this->Html->link($comment['User']['username'],array('controller'=>'users','action'=>'view',$comment['User']['id']))
.'</div>';
}
echo $this->Form->create(null, array('url' => '/comments/add','id'=>'qCommentForm'));
echo $this->Form->input('Comment.parent_id', array('type'=>'hidden','value'=>$data[$type]['id']));
echo $this->Form->input('Comment.parent_type', array('type'=>'hidden','value'=>$type));
echo $this->Form->textarea('Comment.content',array('div'=>'false','class'=>'small','label'=>false));
echo $this->Form->submit(__('Leave comment'),array('div'=>'false','class'=>'small'));
echo $this->Form->end();
?>
Then, in the view view for your model, add this (assuming you named the element comment.ctp:
<?php echo $this->element('comment',array('data'=>$modelData,'type'=>'MyModel')) ?>
I am using cakephp 1.3 and i am receiving strange problem with pagination.I have imnplemented pagination for I have working pagination code in more than one controller .But my rest of the controller not showing next and previous button and numbers for pagination is working fine
I am using this code in my view
<?php
if(!empty($zipcodes)){
echo "<div class='pagination'>";
echo #$this->Paginator->prev('« Previous', null, null, array('class' => 'disabled'));
echo #$this->Paginator->numbers();
echo #$this->Paginator->next('Next »', null, null, array('class' => 'disabled'));
echo '<div style="float:right;padding:5px;color:#000">'.$this->Paginator->counter().'</div>';
echo "<div class='clear'></div>";
echo "</div>";
}
?>
And in my controller i have used pagination controller as i am paginating custom query results .I included pagination code in app_model.php.
My ontroller code is
var $paginate = array(
'Zipcode' => array('limit' => 5,
'order' => array('id' => 'desc'),
)
);
my query and operation
$tsql = " SELECT Zipcode.* ".
" FROM zipcodes AS Zipcode ".
" WHERE Zipcode.region_id=0 ";
$conditions = array(
'tsql'=>$tsql,
);
$tmp = $this->paginate('Zipcode',$conditions);
$this->set('zipcodes', $tmp);
can any one point out me what i am doing wrong.??And why pagination is working for only some controller .??? THanks in advance
First, change your paginate in the top of the controller. Remove Zipcode from it. It should just be:
var $paginate = array(
'limit' => 5,
'order' => array('Zipcode.id' => 'desc'),
);
Second, I am not sure what you are trying to accomplish with $tsql, but as far as I know, you cannot pass a SELECT statement as a condition. You need to write ORM specific conditions like this:
$this->paginate = array(
'conditions' => array('Zipcode.region_id' => 0),
);
Then to set zipcodes you can do this:
$this->set('zipcodes', $this->paginate('Zipcode'));
or this
$data = $this->paginate('Zipcode');
$this->set(compact('data'));
As a side note, echo #$this->Paginator->prev should be echo $this->Paginator->prev and all the # should be removed from those lines. That is poor coding practice. I'm not certain if you have the ignore in there for a reason, but it's bad.
Title seems a bit odd as I am trying to find way's to explain my dilema in layman's terms.
What I am trying to achieve is is from what I can gather, fairly simple but.. I just can't seem to place my finger on it.
I have a drop down selection menu which users can select a country of residence which resides in a helper - example below:
class CountryListHelper extends FormHelper {
var $helpers = array('Form');
function select($fieldname) {
$list = $this->Form->input($fieldname , array(
'type' => 'select', 'label' => 'Country of Residence', 'options' => array(
'' => 'Please select a country',
'AF' => 'Afganistan',
'AL' => 'Albania',
'DZ' => 'Algeria',
.................
),
'error' => 'Please select a country'));
return $this->output($list);
}
}
in the add.ctp:
<?php echo $this->CountryList->select('country');?>
Pretty simple stuff - on save it writes the acronym to the country field.
My issue is.. When pulling the data to view.ctp, how would I go about displaying the full country name as apposed to the acronym saved in the database without having to write the entire list down in view.ctp and matching the acronym to Country name there..
<dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Country of Residence'); ?></dt>
<dd<?php if ($i++ % 2 == 0) echo $class;?>>
<?php echo $user['User']['country']; ?>
</dd>
Any and all help is very much appreciated!
Add a new function to the helper that returns the full name of the country.
class CountryListHelper extends FormHelper {
var $helpers = array('Form');
var $countryList = array(
'AF' => 'Afganistan',
'AL' => 'Albania',
'DZ' => 'Algeria',
.................
);
function select($fieldname) {
$list = $this->Form->input($fieldname , array(
'type' => 'select', 'label' => 'Country of Residence',
'options' => $this->countryList,
'empty' => 'Please select a country',
'error' => 'Please select a country'));
return $this->output($list);
}
function fullName( $abbr ) {
return $this->countryList[ $abbr ];
// + error checking
}
}
I am trying to retrieve the id of a record in my database in the index() method of my cns_controller.php file. I want to use it for a find(). However, the $this->Cn->id is not returning a value, therefore the find() doesn't work either.
$uses = array('Cn', 'Release');
/*check non-null value in released_user_id,
showing Release tab has been signed off*/
$releaseSignedOff = $this->Release->find('all', array(
'conditions' => array('Release.cn_id =' => $this->Cn->id,
'Release.released_user_id !=' => null)
));
(sizeof($releaseSignedOff) > 0) ? $releaseSignedOff = true : $releaseSignedOff = false;
$this->set('releaseSignedOff', $releaseSignedOff);
Any ideas?
Thanks
I ended up not using my index view, instead I looked up the most recent record in my cns database table and redirected to the view view using the returned value as a parameter:
if (!$id) {
$most_recent_change_note = $this->Cn->find('first', array(
'order' => array('Cn.id DESC')
));
$this->redirect(array('controller' => 'cns',
'action' => 'view/'.$most_recent_change_note['Cn']['id']));
}
For the pagination, I ended up using the $neighbors feature of CakePHP:
function get_neighbors($id) {
$neighbors = $this->Cn->find('neighbors', array('field' => 'id',
'value' => $id));
$this->set('neighbors', $neighbors);
}
Then in my view view, I used those values to create links:
<div class="paging">
<?php echo ($neighbors['next']) ? $html->link('<< Prev',
array('controller'=>'cns',
'action'=>'view/'.$neighbors['next']['Cn']['id'])).' | ' : '<< Prev | ';
echo $html->link('Next >>', array('controller'=>'cns',
'action'=>'view/'.$neighbors['prev']['Cn']['id'])); ?>
</div>
Thanks to Charles and Ross for helping me reach this conclusion. :)