I have to make a webapp in Yii framework.
I want to work with select2 And want to use a column in my database..
This is my code
<?php
$users= User::model()->findAll(array(
'select'=>'name',
));
echo CHtml::textField('User','',array('id'=>'user', 'style'=>'width:300px;'));
$this->widget('ext.select2.ESelect2',array(
'selector'=>'#user',
'options' => array(
'tags'=> $names,
),
));
?>
When I use vardump to check what is in the variable $users then I get this:
Array
(
[0] => User Object
(
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[name] => "asd"
)
[_related:CActiveRecord:private] => Array
(
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] =>
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
[1] => User Object
(
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[name] => "assss"
)
[_related:CActiveRecord:private] => Array
(
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] =>
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
And a lot more.
But the only thing I want is this: [name] => "assss" .
I think I have too use an foreach loop or something like that because select2 wants a "clear" array.
Do you guys know how to do this?
You will get array
$array = CHtml::listData(User::model()->findAll(), 'user_id', 'user_name');
For example the following code
<?php echo $form->dropDownList($model, 'country', CHtml::listData(Country::model()->findAll(), 'id', 'name'), array('id' => 'country_list', 'multiple' => true)) ?>
<?php $this->widget('application.extensions.select2.ESelect2',
array(
'selector' => "#country_list",
'options' => array(
'dropdownCssClass' => 'bigdrop',
'width' => '60%',
)
))?>
<?php
$users= User::model()->findAll(array(
'select'=>'name',
));
echo CHtml::textField('User','',array('id'=>'user', 'style'=>'width:300px;'));
$this->widget('ext.select2.ESelect2',array(
'selector'=>'#user',
'options' => array(
'tags'=> $names,
),
));
?>
Related
<?php
[USED] => Array
(
[USER] => Array
(
[#attributes] => Array
(
[NAME] => locuz
[HOST] => srpth1cn03.local
[IP] => 10.106.2.48
[USED_LICENSES] => 1
[LOGIN_TIME] => 2014-12-10 07:34
[CHECKOUT_TIME] => 2014-12-10 07:34
)
)
)
I want to change above array to below:
[USED] => Array
(
[USER] => Array
(
[0] => Array
(
[#attributes] => Array
(
[NAME] => rdtank
[HOST] => it30992
[IP] => 10.106.21.134
[DENIED_LICENSES] => 1
[LOGIN_TIME] => 2014-12-09 15:55
[DENIAL_TIME] => 2014-12-09 15:55
)
)
)
)
Put a [] before you save attributes as below.
foreach($all_attributes as $attributes){
$sample_array['USED']['USER'][] = $attributes;
}
the code you wrote seems the result of 2 array dump.
so if you need to setup 2 variable in php that contains data you should use this code
<?php
$used = array(
'users' => array(
array(
'name' => 'locuz',
'host' => 'srpth1cn03.local',
'ip' => '10.106.2.48',
'used_licenses' => 1,
'login_time' => '2014-12-10 07:34',
'checkout_time' => '2014-12-10 07:34'
)
)
);
$denied = array(
'users' => array(
array(
'name' => 'rdtank',
'host' => 'it30992',
'ip' => '10.106.21.134',
'denied_licenses' => 1,
'login_time' => '2014-12-09 15:55',
'denial_time' => '2014-12-09 15:55'
)
)
);
hope this helps.
im using yii1 on my application.
i want to convert from cActivedataProvider to array
this is the code
$dataSS = new CActiveDataProvider('category', array(
'criteria' => array(
'condition' => 'menu=:menu',
'params' => array(':menu' => $menu),
),
'pagination' => false
));
$dataMenu = array();
foreach ($dataSS->getData() as $record) {
$dataMenu[] = array(
'label' => $record->name,
'url' => '#',
);
}
this is the result :
Array (
[0] => Array ( [label] => Food and Drink [url] => # )
[1] => Array ( [label] => Sleman [url] => # )
)
the result that i expected :
Array (
Array ( 'label' => 'Food and Drink', 'url' => '#' ) ,
Array ( 'label' => 'Sleman', 'url' => '#' ) ,
)
any suggestion?
Finally get the answer,
this is really my bad because i call the function on wrong way.
This is the wrong way :
'items' =>array(Category::model()->getMenu("2");),
and this is the correct way :
'items' =>Category::model()->getMenu("2"),
I'm using Yii 1.1.15 and am getting this error
Fatal error: Call to a member function getDbCriteria() on a non-object
the code below is in my view
<?php
$model = new Comment(); //name of my model Project refers to Mysql innoDB table tblproject.
$daten=$model::model();
$dataProvider=new CActiveDataProvider($daten->with(array('posts' => array('limit'=>6)))->findAll());
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view_latest_comment', //view file location
));
?>
the relation in my comment.php is as such
public function relations()
{
return array(
'user' => array(self::BELONGS_TO, $this->module->userModelClass, 'userId'),
'posts' => array(self::HAS_MANY, "CommentsPosts", array("commentId" => "id"))
);
}
UPDATE:
i also tried this, but it does not set the limit to 5
$model = new Comment();
$daten=$model::model();
$criteria = new CDbCriteria;
$criteria->limit=5;
$dataProvider=new CActiveDataProvider($daten, array('criteria'=>$criteria));
when i print_r($daten); i get this
Comment Object (
[_type:Comment:private] =>
[_key:Comment:private] =>
[_make:Comment:private] =>
[_model:Comment:private] =>
[_year:Comment:private] =>
[_new:Comment:private] =>
[_attributes:CActiveRecord:private] => Array ( )
[_related:CActiveRecord:private] => Array ( )
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] =>
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array ( )
[_validators:CModel:private] =>
[_scenario:CModel:private] =>
[_e:CComponent:private] => Array (
[onbeforesave] => CList Object (
[_d:CList:private] => Array (
[0] => Array (
[0] => CTimestampBehavior Object (
[createAttribute] => createDate
[updateAttribute] =>
[setUpdateOnCreate] =>
[timestampExpression] =>
[_enabled:CBehavior:private] => 1
[_owner:CBehavior:private] => Comment Object *RECURSION*
[_e:CComponent:private] =>
[_m:CComponent:private] => )
[1] => beforeSave ) )
[_c:CList:private] => 1
[_r:CList:private] =>
[_e:CComponent:private] =>
[_m:CComponent:private] => ) )
[_m:CComponent:private] => Array (
[commentable] => CommentableBehavior Object (
[mapTable] =>
[mapCommentColumn] => commentId
[mapRelatedColumn] =>
[mapMakeColumn] => make_code
[mapModelColumn] => model_code
[mapYearColumn] => year_made
[mapVariantColumn] => variant
[_enabled:CBehavior:private] => 1
[_owner:CBehavior:private] => Comment Object *RECURSION*
[_e:CComponent:private] =>
[_m:CComponent:private] => )
[CTimestampBehavior] => CTimestampBehavior Object (
[createAttribute] => createDate
[updateAttribute] =>
[setUpdateOnCreate] =>
[timestampExpression] =>
[_enabled:CBehavior:private] => 1
[_owner:CBehavior:private] => Comment Object *RECURSION*
[_e:CComponent:private] =>
[_m:CComponent:private] => ) )
[_new:CActiveRecord:private] => )
i'm trying to dynamically set a limit to the results returned, but can't seem to get it to work. any idea on what i'm doing wrong or missing? Thanks
Dataprovider require first parameter to be class name or model instance. In your view it's result of findAll().
Move with clause to second parameter, something like that:
<?php
$dataProvider = new CActiveDataProvider(Comment::model(), array(
'criteria' => array(
'with' => array('posts')
)));
$this->widget('zii.widgets.CListView', array(
'dataProvider' => $dataProvider,
'itemView' => '_view_latest_comment', //view file location
));
NOTE: CActiveDataProvider will do findAll etc. by itself. It just need to know what need to be searched for.
Here is php doc part from CActiveDataProvider source:
$dataProvider=new CActiveDataProvider('Post', array(
'criteria'=>array(
'condition'=>'status=1',
'order'=>'create_time DESC',
'with'=>array('author'),
),
'countCriteria'=>array(
'condition'=>'status=1',
// 'order' and 'with' clauses have no meaning for the count query
),
'pagination'=>array(
'pageSize'=>20,
),
));
CArrayDataProvider Object
(
[keyField] =>
[rawData] => Array
(
[24] => Array
(
[ids] => 24
[name] => trillia
)
[25] => Array
(
[ids] => 25
[name] => ahestina
)
[28] => Array
(
[ids] => 28
[name] => piyas
)
[59] => Array
(
[ids] => 59
[name] => sitesrs
)
[60] => Array
(
[ids] => 60
[name] => simons
)
[70] => Array
(
[ids] => 70
[name] => Mahesh Raj
)
[71] => Array
(
[ids] => 71
[name] => kathetrine
)
[72] => Array
(
[ids] => 72
[name] => babig
)
[73] => Array
(
[ids] => 73
[name] => symons
)
)
[caseSensitiveSort] => 1
[_id:CDataProvider:private] =>
[_data:CDataProvider:private] =>
[_keys:CDataProvider:private] =>
[_totalItemCount:CDataProvider:private] =>
[_sort:CDataProvider:private] =>
[_pagination:CDataProvider:private] => CPagination Object
(
[pageVar] => page
[route] =>
[params] =>
[validateCurrentPage] => 1
[_pageSize:CPagination:private] => 10
[_itemCount:CPagination:private] => 0
[_currentPage:CPagination:private] =>
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
from the above array i populate the cgridview , using the following code in view file
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'unen-custom-module-grid',
'dataProvider'=>$unenrollProvider,
//'dataProvider'=>Yii::app()->globals->getUsersByStandardnSection(1,3),
'itemsCssClass'=>'table table-striped flip-scroll',
'ajaxUpdate'=>true,
'template'=>'{summary}{pager}{items}{summary}{pager}',
'pager'=>array(
'header' => '',
'firstPageLabel' => '<<',
'prevPageLabel' => 'PREVIOUS',
'nextPageLabel' => 'NEXT',
'lastPageLabel' => '>>',
),
// 'selectableRows' => 2,
'columns'=>array(
array(
'header'=>'#',
'value'=>'++$row',
'htmlOptions'=>array(
'style'=>'width:50px;'
)
),
'name',
array(
'id' => 'unselectedIds',
// 'name' => 'ids',
'selectableRows' => 2,
'class' => 'CCheckBoxColumn',
//'value'=>'$this->ids'
),
),
));
?>
its working fine , but whenever i click the pagination the cgridview is automatically hiding , im not using $model->search() here ,
every time cgridview pagination sends get requests only, with out form post data you cant able to populate the cgrid again and again , so try to store the values in session and access it , i hope its usefull to you , try it
CGridView requires the result returned by CActiveDataProvider so try to do this in your search method in the corresponding model using CActiveDataProvider
I think you're experiencing a problem here for not having specified a keyField in your data provider. Try array('keyField'=>'ids) when creating the data provider instance. See also this answer.
The following question can either be solved by probably changing my use of the find method in Cake PHP OR using some PHP function. I would prefer to solve it using Cake but it doesn't matter too much. I have any array like this:
Array
(
[0] => Array
(
[Model] => Array
(
[id] => 14
[foo] => bar
)
)
[1] => Array
(
[Model] => Array
(
[id] => 15
[foo] => something
)
) .............
I just want to remove the Model index and just use the numeric one. The following function generated this array:
$arr = $this->Model->find('all', array('contain' => false ) );
I probably need to change the 'contain' part of the call. Basically, in addition to the data that appears under each Model index, I also have a second and third model and the contain = false just restricts Cake from getting data from the current model (Model).
If I understand your question correctly, I think CakePHP's Set::combine function will help http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::combine :
$result = Set::combine($your_array, '{n}.Model.id', '{n}.Model.data');
This will result in:
Array
(
[14] => Array
(
[foo] => bar
)
[15] => Array
(
[foo] => something
)
)
you have to write your own piece of code to modify your array , here is a function that will do what you want (you can improve it)
function reformArray($array,$modelName)
{
foreach($arr as $key => $value)
{
$newArr[] = $arr[$key][$modelName];
}
return $newArr;
}
you have to pass your array and the Model Name to this function and will return you the result
$a = array(
array(
'User' => array(
'id' => 2,
'group_id' => 1,
'Data' => array(
'user' => 'mariano.iglesias',
'name' => 'Mariano Iglesias'
)
)
),
array(
'User' => array(
'id' => 14,
'group_id' => 2,
'Data' => array(
'user' => 'phpnut',
'name' => 'Larry E. Masters'
)
)
),
array(
'User' => array(
'id' => 25,
'group_id' => 1,
'Data' => array(
'user' => 'gwoo',
'name' => 'The Gwoo'
)
)
)
);
RUN THIS TO REMOVE THE MODEL NAME USER
Set::extract($a, '{n}.User');
WILL RETURN THIS
$a = array(
array(
'id' => 2,
'group_id' => 1,
'Data' => array(
'user' => 'mariano.iglesias',
'name' => 'Mariano Iglesias'
)
),
array(
'id' => 14,
'group_id' => 2,
'Data' => array(
'user' => 'phpnut',
'name' => 'Larry E. Masters'
)
),
array(
'id' => 25,
'group_id' => 1,
'Data' => array(
'user' => 'gwoo',
'name' => 'The Gwoo'
)
)
);
In CakePHP 2:
$data = $this->Model->find('all');
$data = Set::extract('/Model/.', $data );
You can achieve this result by foreach also
foreach ($data as $k => &$t) {
$t = $t['Model']
}
It will be much easier with Object. Change the Array with Object.
Actually I had faced the same problem with nested array And I found the solution with Set::map($array);
If you don't want the model_name as nested array, You can prefer this solution.
$data = array(
array(
"IndexedPage" => array(
"id" => 1,
"url" => 'http://blah.com/',
'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
'get_vars' => '',
'redirect' => '',
'created' => "1195055503",
'updated' => "1195055503",
)
),
array(
"IndexedPage" => array(
"id" => 2,
"url" => 'http://blah.com/',
'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
'get_vars' => '',
'redirect' => '',
'created' => "1195055503",
'updated' => "1195055503",
),
)
);
$mapped = Set::map($data);
/* $mapped now looks like: */
Array
(
[0] => stdClass Object
(
[_name_] => IndexedPage
[id] => 1
[url] => http://blah.com/
[hash] => 68a9f053b19526d08e36c6a9ad150737933816a5
[get_vars] =>
[redirect] =>
[created] => 1195055503
[updated] => 1195055503
)
[1] => stdClass Object
(
[_name_] => IndexedPage
[id] => 2
[url] => http://blah.com/
[hash] => 68a9f053b19526d08e36c6a9ad150737933816a5
[get_vars] =>
[redirect] =>
[created] => 1195055503
[updated] => 1195055503
)
)
you may do this :
$tmp = $this->Model->find('all', array('contain' => false ) );
$arr = $tmp['ModelName'];