I am using CJuiDateTimePicker in my yii application but can't save data into mysql. How can Save data into MySQL
CODE::
<?php Yii::import('application.extensions.CJuiDateTimePicker.CJuiDateTimePicker');
$this->widget('CJuiDateTimePicker',array(
'model'=>$model, //Model object
'attribute'=>'pp_date', //attribute name
'language'=> '',
'mode'=>'date',
'options'=>array(
'showAnim'=>'fold',
'changeMonth' => 'true',
'changeYear' => 'true',
'showOtherMonths' => 'true',
'selectOtherMonths' => 'true',
'showOn' => 'both',
),
));?>
Any Helps appreciated
Check whether the Dat and Time format are mysql format
dd-mm-yyyy hh:mm:ss
Related
I have been trying to fix an issue but to no avail but i am sure i will find a solution here. I am using Kartik 2.0 Select extension to do a multiple select. Fine, all working when inserting into the database but i am unable to retrieve the saved records to be displayed as selected back in the select field.
//I have included the kartik widgets already
use kartik\widgets\Select2;
<label>Desired Specialization(s)</label>
<?= $form->field($spec, 'id')->label(false)->widget(Select2::classname(), [
'data' => $model->getAllSpecializations(),
'options' => ['placeholder' => 'You can choose more than one specialization ...'],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true
],
]);
?>
</div>
Please, your reply will be appreciated. Thanks
I think you need to add the saved values as the initial data? Like so:
'value' => $savedDataArray, // initial value
http://demos.krajee.com/widget-details/select2#usage-tags
After much digging into the code, i found a way on how to display selected database values into a multi-select option using Yii Select2
My Model
public function getCandidateLanguage()
{
$langValues = (new \yii\db\Query())
->select('c.language_id AS id, l.lang_name')
->from('candidate_language c ')
->innerJoin('languages l','c.language_id = l.id')
->where('c.candidate_id='.$this->candidate_id)
->orderBy('c.language_id')
->all();
return \yii\helpers\ArrayHelper::map($langValues,'id','lang_name');
}
My View
use kartik\widgets\Select2;
<?php
//the line below is to fetch the array key of $model->getCandidateLanguage() array
$lang->id = array_keys($model->getCandidateLanguage()); // value to initialize
echo Select2::widget([
'model' => $lang,
'attribute' => 'id',
'data' => $model->getAllLanguages(),
'options' => ['placeholder' => 'Choose multiple languages'],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true,
'tags' => true,
],
]);
?>
Hope it help someone who is facing the same issue.
I'm trying to display all images that have a certain custom field from the types plugin set to true. It would also work to filter them by post_content or post_excerpt but none of my attempts have worked so far.
<?
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_content' => 'foo',
'numberposts' => -1
);
?>
<? print_r(get_posts($args)); ?>
This get's all images allthough only one has the post_content foo. My attempt to use WP_Query failed miserably as well.
Any help is appreciated!
WP_Query method :
$args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'meta_query' => array(
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'LIKE',
),
),
);
$query = new WP_Query( $args );
I am presuming that the reason why you failed with WP_Query is due to the following condition.
Codex states : The default WP_Query sets 'post_status'=>'publish', but attachments default to 'post_status'=>'inherit' so you'll need to explicitly set post_status to 'inherit' or 'any' as well.
http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
get_posts method :
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'meta_key' => 'custom-field',
'meta_value' => 'custom value',
'numberposts' => -1
);
print_r(get_posts($args));
The only draw back with this method is that the meta_value needs to exactly match what was entered in the custom field. If you still like to use get_posts then use the meta_query as shown in WP_Query example above.
How to pass grid row id from CGridView to filter values in another CGridView opened in Dialog
My View Code for Form Grid(see screenshot),
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'document-grid',
'dataProvider'=>$model->search(),
'columns'=>array(
array(
'class' => 'CButtonColumn',
'template' => '{edit}{DocumentDelete}',
'buttons' => array(
'DocumentDelete' => array(
'imageUrl'=>Yii::app()->request->baseUrl.'/images/delete.png',
'url'=>'Yii::app()->createUrl("baseContact/DocumentDelete", array("id"=>$data->crm_document_id))',
),
'edit' => array(
'imageUrl'=>Yii::app()->request->baseUrl.'/images/update.png',
'url'=>'Yii::app()->createUrl("baseContact/edit", array("id"=>$data->crm_document_id))',
),
),),
'crm_document_id',
'name',
'doc_type',
'delivery_method',
'content_subject',
'content_body',
'is_active',
),
)); ?>
View Code for Popup Grid,
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'ManagedocumentAttach-grid',
'dataProvider'=>DocumentAttachmentModel::model()->search(),
//'filter'=>$model,
'columns'=>array(
array(
'name'=>'',
'value'=>'CHtml::checkBox("cid[]",null,array("value"=>$data->crm_document_attachment_id,"id"=>"cid_".$data->crm_document_attachment_id))',
'type'=>'raw',
'htmlOptions'=>array('width'=>5),
//'visible'=>false,
),
'crm_document_id',
'name',
'type',
),
)); ?>
how to pass Form edited row id to Popup GridView ?
Clearly you use AJAX to get the popup so i would first would make sure the ID of the edit button is the same as the ID from the item you want to open. Then you can do the following JS (using JQUERY)
$(".edit").on("click", function() {
var id = $(this).attr("id");
$.ajax({
type:"POST",
url: "controller/action/"+id;
success: function(data) {
//open dialog box and fill it with data
}
});
You could also add the id as data so you can get it with $_POST instead of it being a variable defined by the function. If you write the JS in a php document you can use $this->createUrl, but that is just whatever you prefer.
If with this you can not solve your problem then let us see how you implemented it right now.
i am not sure ... but i have a technique to do it....
if i have to do something like this ....
i will give class by htmlOptions and after that i will get value which should be an id to open popup..
example
<?php $this->widget('bootstrap.widgets.TbGridView', array(
'dataProvider' => $dataProvider ,
'type' => TbHtml::GRID_TYPE_BORDERED,
'template' => "{items}",
'columns' => array(
array(
'name' => 'vendor_configuration_id',
'header' => $dataProvider->model->getAttributelabel('vendor_configuration_id'),
'htmlOptions' => array('class'=>'idClass'),
),
array(
'name' => 'menu_type',
'header' => $dataProvider->model->getAttributelabel('menu_type'),
'htmlOptions' => array(),
),
?>
now jquery for it
$('.idClass').on("click",function(){
var neededId = $(this).html();
alert(neededId );
//openn popup based on this id or caal ajax to retrive data based on this
});
CJUIDatePicker not filtering value for dd/mm/yyyy format but filtering if i gave DB date format manually like shown in image, I have attached all codes including View and Model for Search panel and Date Widget, Please look into this,
My Model Search(),
public function search()
{
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('crm_base_contact_id',$this->crm_base_contact_id);
$criteria->compare('name',$this->name,true);
$Date = date('Y-m-d',strtotime($this->created)); // get proper Y-m-d
$startOfDay = $Date . ' 00:00:00'; // from start of the day
$endOfDay = $Date . ' 23:59:59'; // until end of the day
// the rows between these
$criteria->addBetweenCondition('created', strtotime($startOfDay) , strtotime($endOfDay) );
$criteria->compare('createdby',$this->createdby,true);
$criteria->compare('description',$this->description);
$criteria->compare('is_active',$this->is_active);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
My View for CGridView,
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'basecontact-grid',
'dataProvider'=>$model->search(),
//'filter'=>$model,
'columns'=>array(
array('class'=>'CButtonColumn',),
'name',
'crm_base_contact_id',
array(
'name'=>'is_active',
'filter'=>CHtml::dropDownList('Event[is_active]', '',
array(
''=>'',
'1'=>'Y',
'0'=>'N',
)
),
'value' =>'($data->is_active==1)?"Y":"N"',
),
'created',
'createdby',
'description',
),
)); ?>
My View Code for Search column,
<?php $this->widget('zii.widgets.jui.CJuiDatePicker',array(
'model'=>$model,
'id'=>'Search-Created',
'attribute'=>'created',
'options'=>array(
'dateFormat'=>'dd/mm/yy',
'showAnim'=>'fold',
'buttonImage'=>Yii::app()->baseUrl.'/images/icons.date.png',
'buttonImageOnly'=>true,
'buttonText'=>'',
'showAnim'=>'fold',
'showOn'=>'button',
'showButtonPanel'=>false,
),
));?>
you should probably change the date format. set it like this, 'dateFormat'=>'dd-mm-yy',
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'model'=>$model,
'attribute'=>'dob',
// additional javascript options for the date picker plugin
'options' => array(
'showAnim' => 'fold',
'dateFormat'=>'dd/mm/yyyy',
),
'htmlOptions' => array(
'style' => 'height:20px;'
),
));
Update:
In your model, define it as following, in my case dob is the attribute name. you can have your own instead.
This will change your dateformat from "dd/mm/yyyy" to "yyyy-mm-dd internally before you run any find.
Ref : http://www.yiiframework.com/doc/api/1.1/CActiveRecord#beforeFind-detail
protected function beforeFind()
{
$this->dob=date('Y-m-d', strtotime($this->dob));
parent::beforeFind();
}
update 2
Replace your search method with this,
public function search() {
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria = new CDbCriteria;
$criteria->compare('crm_base_contact_id', $this->crm_base_contact_id);
$criteria->compare('name', $this->name, true);
$criteria->compare('created', '07/12/2013'); //date('Y-m-d', strtotime($this->created))
$criteria->compare('createdby', $this->createdby, true);
$criteria->compare('description', $this->description);
$criteria->compare('is_active', $this->is_active);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
Update 3
change this line $criteria->compare('created', '07/12/2013'); to
$criteria->compare('created', date('Y-m-d', strtotime($this->created));
I have tried to implement the sorting using BootGridView but it does not seem to work. Here is the line of code.
<?php $this->widget('bootstrap.widgets.BootGridView', array(
'id'=>'user-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'user_id',
array('name'=>'user_type',
'value'=>'User::getType($data->user_type)'),
'username',
array('header'=>'Name',
'type'=>'raw',
'value'=> 'CHtml::link($data->firstname." ".$data->lastname, array("view", "id"=>$data->id))'
),
array('name' => 'status',
'type' => 'raw',
'value' => '$data->status == 1 ? "Active" : CHtml::link("In Active", "javascript:void(0);", array("id" => "active_" . $data->id, "onClick"=>"js:activate($data->id)"))'
),
/*
'email',
'dob',
'profession',
'hobby',
'height',
'weight',
'weight_taken_on',
'login_attempt',
'registration_date',
*/
array(
'class'=>'bootstrap.widgets.BootButtonColumn',
),
),
)); ?>
The code does not seem to sort the table as expected. What could be the possible problem in this. ?
Sorting is configured in the data provider, not in the grid view. So you should look into the search() method of your model. You need something like this:
return new CActiveDataProvider('User',array(
// ...
'sort' => array(
'attributes' => array(
'name',
'email',
// ...
Have a look at CSort and especially the attributes property there to learn more about sort options you can use here.