How can I convert the result of Trips::model()->findAll() to an array?
I'm going on the assumption here that you only need to retrieve just the bare arrays, and not any associated model objects.
This will do it:
$model = Trips::model();
$trips = $model->getCommandBuilder()
->createFindCommand($model->tableSchema, $model->dbCriteria)
->queryAll();
This is like the Yii::app()->db->createCommand('SELECT * FROM tbl')->queryAll(); examples, except:
It'll ask the model for the table name; you won't need to write the table name in both the model and the query.
You can call scoping functions on $model first, eg.
$model = Trips::model()->short()->destination('Austin, TX');
Doing this means you can use the model's existing query shortcuts, instead of putting them in the query directly.
In contrast, the $trips = Trips::model()->findAll(); (using foreach) is a bit wasteful, in that you're pulling the rows from the database, setting up a bunch of objects, and then throwing them all away. It'll work fine for small result sets, but I wouldn't use that if you're looking at a long list of Trips.
Caveat:
If this is just a quick prototype, though, by all means use the createCommand() or findAll()-and-loop examples.
This is the right way to do, it follows Yii conventions
$trips = Trips::model()->findAll();
$arr = array();
foreach($trips as $t)
{
$arr[$t->id] = $t->attributes;
}
This is used when you have complex queries, those you find difficult to create with Yii conventions.
Yii::app()->db->createCommand('SELECT * FROM tbl')->queryAll();
For example, when you need to pass all the data from the model to an array. You cannot pass it directly as it does pass some ActiveRecord data information that you don't need.
This is same.
$array = CHtml::listData(Trips::model()->findAll(), 'trip_id', 'trip_name');
Easy and simple way: I use listData() method to make array to dropdown menus, and I think this will help you.. check this example:
code:
<?php
/*you can use here any find method you think
proper to return your data from db*/
$models = Trips::model()->findAll();
// format models resulting using listData
$tripsArray = CHtml::listData($models, 'id', 'name');
print_r($tripsArray);
?>
output:
array(
'1'=>'trip1',
'2'=>'trip2',
'3'=>'trip3',
)
$model = Trips::model()->findAll();
$arr = CHtml::listData($model, 'trip_id', 'trip_name');
var_dump($arr);
CHtml::listData() will return an array value.
You can create collections CMap continue to work with her
$collections = new CMap();
foreach (YourModel::model()->findAll(['index' => 'id']) as $key => $row) {
$collections->add($key,$row->attributes);
}
var_dump($collections ->toArray());
I'm pretty sure you can do this:
$trips = Trips::model()->findAll();
$arr = array();
foreach($trips as $t)
{
$arr[$t->id] = $t->attributes;
}
I'm assuming you have the attribute 'id' as your model's primary key.
i use $array = CJSON::decode(CJSON::encode($model)); to convert $model to $array.
You can use this.
$Trips::model()->findAll(array('index'=>'trip_id'));
if(count($Trips)>0)
{
$TripsArrayList=array();
foreach($Tripsas as $singleTrip)
{
$TripsArrayList[]=array('trip_id'=>$singleTrip->trip_id,'name'=>$singleTrip->name);
}
}
Your output will be
Array
(
[0] => Array
(
[trip_id] => 1
[name] => Nashik
)
[1] => Array
(
[trip_id] => 2
[name] => Puna
)
[2] => Array
(
[trip_id] => 3
[name] => Mumbai
)
)
$cats = Category::model()->findAll();
$count_cats = count($cats);
if($count_cats > 0){
$arr_category = array();
foreach($cats as $cat)
array_push($arr_category,$cat->attributes);
}
print_r($arr_category);
-> result
Array(
[0] => Array
(
[cat_id] => 2
[title] => Đương đại
[title_full] => Đương đại
[desc] =>
[alias] => duong-dai
[p_id] => 0
[type] => 1
[status] => 1
[sort_order] => 2
[selected] => 0
)
[1] => Array
(
[cat_id] => 164
[title] => Nhiệt đới
[title_full] => Nhiệt đới
[desc] =>
[alias] => nhiet-doi
[p_id] => 0
[type] => 1
[status] => 1
[sort_order] => 0
[selected] => 0
)
[...])
Assuming from your question that you want all the attributes, a more compact solution to give you all attributes hashed by id, you can use CActiveRecord's 'attributes' pseudoproperty as follows:
CHtml::listData(Trips::model()->findAll(), 'id', 'attributes')
In yii2 you can use asArray()
$someArray = Sometable::find()->select(['id', 'name', 'role'])->asArray()->all();
Use DAO for arrays
$array = Yii::app()->db->createCommand('SELECT * FROM tbl')->queryAll();
Don't used CHtml::listData for this. It has to be used for other purposes.
There is an index property of CDbCriteria which is suitable for you requirement.
//1st option
Trips::model()->findAll(array('index'=>'trip_id'));
//2nd option
$c = new CDbCriteria();
$c->index = 'trip_id';
Trips::model()->findAll($c);
Use simply:
$trips = Trips::model()->findAll();
$trips_array = CJSON::decode(CJSON::encode($trips));
Note: This is not good way but returns array
Related
When I use this code,
$views = array();
$views[]=['somevalue'=>'customerview.php'];
$views[]=['anothervalue'=>'ordersview.php'];
I get this,
Array
(
[0] => Array
(
[somevalue] => customerview.php
)
[1] => Array
(
[anothervalue] => ordersview.php
)
)
How can I make it get rid of the initial array without using array_shift or the like? Why is it putting the numerical array in the first place instead of just this,
Array
(
[somevalue] => customerview.php
[anothervalue] => ordersview.php
)
EDIT: How can I use the short syntax for this? Is that possible?
When you do this:
$views[] = ['somevalue' => 'customerview.php'];
you're saying, "Push another element onto the array, and assign to it the following value:
'somevalue' => 'customerview.php'
But this quantity is an array key and an array value. So what you're doing is inserting into your $views array a single element that itself contains an array key and array value. This explains the behavior you're seeing.
This should give you the results you want:
$views = array();
$views['somevalue'] = 'customerview.php';
$views['anothervalue'] ='ordersview.php';
Or, in shorthand:
$views = [
'somevalue' => 'customerview.php',
'anothervalue' => 'ordersview.php'
];
or you can do:
$value1 = 'first';
$value2 = 'second';
$array = array(
$value1 => 'customerview.php',
$value2 => 'ordersview.php'
);
$views is already an array so when you use $views[], you are adding another array into the existing array.
You need to use
$views = array(
'somevalue' => 'customerview.php',
'anothervalue' => 'ordersview.php'
)
I am looking to except single element from array. My array is:
Array
(
[0] => Array
(
[0] => Array
(
[COUNT(ID)] => 0
)
)
)
I have already use PHP functions basename and array_shift, but they didn't give me proper value. I want only single string COUNT(ID) value.
Here is my function using in cakephp model:
$res = $this->query("select COUNT(ID) from users where Username = '".$Username['Username']."'");
if ($res[0][0]['COUNT(ID)'] >= 1)
{
return false;
}
else
{
return true;
}
I don't need $res[0][0], I need only COUNT[ID]. Is there any easy way to find only COUNT[ID].
If you are using CakePHP, it's good to use model and inbuilt functions to get answer what you want...
$this->loadModel('User'); //If you are in controller
$total = $this->User->find('count', array(
'conditions' => array('Username' => $Username['Username'])
));
You'll get answer in single variable..!!
Your output is produced by code like this
$data = array(
array(
array(
'COUNT(ID)' => 0
)
)
);
You can access its value by calling directly
$data[0][0]['COUNT(ID)']
This could be wrong problem you are solving, as it seems like database query result that should not look like that and could be done with more ease. You should show your original function/problem, that this script is part of.
Let's say you array's name is $myArray, than you can assign the value of $myArray[0][0]['COUNT(ID)'] to $value in the way you usually assign a value: $value = $myArray[0][0]['COUNT(ID)'];.
If you want to delete an index from an array, use unset() like this: unset($myArray[0][0]['COUNT(ID)']). I hope this answers your question.
If you know the exact count of nesting in array you can just access it with indexes
$array[0][0]['COUNT(ID)']
If no, you might want to use the function which will recursively find you the first nested element that is not an array
function getStringFromArray($array) {
if (is_array($array)) {
foreach ($array as $key => $value) {
if (is_array($value)) {
return getStringFromArray($value);
} else {
return $value;
}
}
} else {
throw new Exception("Not an array given");
}
}
If you are sure that array structure will remain same then this could be useful :-
function array_values_recursive($ary)
{
$lst = array();
foreach( array_keys($ary) as $k ){
$v = $ary[$k];
if (is_scalar($v)) {
$lst[] = $v;
} elseif (is_array($v)) {
$lst = array_merge( $lst,
array_values_recursive($v)
);
}
}
return $lst;
}
$arr=array(array(array('COUNT(ID)'=>5)));
$res=array_values_recursive($arr);
echo '<pre>';print_r($res);
output :-
Array
(
[0] => 5
)
You can probably fetch it by associative array to reduce dimension but you can remove index. May be you can use foreach to automatically use indexs.
Try this code:
public $uses = array('User');
$count = $this->User->find('count', array(
'conditions' => array('Username' => $Username['Username'])
));
if ($count >= 1) {
return false;
} else {
return true;
}
You can use following technique for your solution. i did not get you what you want to do with your array but you can try below solution
$arr = Set::extract("/0/COUNT(ID)",$data);
where $data is your input array.
you will get following output
Array
(
[0] => 5
)
you can refer below link
Remove array key from array in cakephp
Its a very basic php question, I want to display a value with comma separated from.I know a procedure,I can get comma separated value by using comma explode. I just want to confirm will it run successfully or not.I am giving my output and array below : I need my output something like Sahbaj,test-name.
And my array :
Array
(
[0] => Array
(
[AdoPosition] => Array
(
[name] => Sahbaj
)
)
[1] => Array
(
[AdoPosition] => Array
(
[name] => test-name
)
)
)
My controller code is below :
$name = $this->AdoPosition->find('all',
array(
'fields'=>'AdoPosition.name',
'group'=>'AdoPosition.name'
));
pr($name);
Do that:
$name = $this->AdoPosition->find('list', array(
'fields' => array('AdoPosition.name', 'AdoPosition.name'),
'group' => array('AdoPosition.name')
));
$name = implode(',', $name);
The return is:
"Sahbaj,test-name"
make it simple
$names=Set::extract("/AdoPosition/name",$array);
OR
$names = $this->AdoPosition->find('list', array('fields'=>array('name')));
look at this.
Remove array key from array in cakephp
Another alternative is to ask mysql to join with comma like-
$data = $this->AdoPosition->query('SELECT GROUP_CONCAT(DISTINCT name) from your_table_name;');
For better efficiency try this.
$result = implode( ",", Set::classicExtract($name, '{n}.AdoPosition.name'));
echo $result; // Sahbaj,test-name
I'm trying to build an array and update a few fields in a loop.
This is my request->data
Array
(
[list] => Array
(
[4] => null
[2] => null
[3] => null
[5] => 3
)
)
It's the return value from a jquery serialized list. The key being the row id and the value being the rows parent_id.
So I loop through the array in the controller:
foreach ($this->request->data['list'] as $key => $value) {
(!isset($orders[$value])) ? $orders[$value]=0 : $orders[$value]++;
$data = array('id' => $key, 'parent_id' => (int)$value, 'display_order' => $orders[$value]);
$this->Category->save($data);
}
The $data array that I create in the loop is correct but the sql logs only shows SELECT COUNT(*) etc. for each row and no UPDATE commands.
I have tried all manner of ways to save this: using set() method, using $this->Category->id = $key; instead of directly adding the key in the data array.
Any ideas why this is not saving? I'm sure it is something simple...
i think u forgot the cake convention in the save method, you have to put all the field values inside an array with the Model name FirstCap too, something like this:
Array
(
[ModelName] => Array
(
[fieldname1] => 'value'
[fieldname2] => 'value'
)
)
Else you can use set for each and every value, and also youre forgetting to create one row for every insert youre making so try something like this:
foreach ($this->request->data['list'] as $key => $value) {
(!isset($orders[$value])) ? $orders[$value]=0 : $orders[$value]++;
$data = array( 'Category' => array('id' => $key, 'parent_id' => (int)$value, 'display_order' => $orders[$value]));
$this->Category->save($data);
}
Also you can set each and every id and then iterates over the values
foreach ($this->request->data['list'] as $key => $value) {
(!isset($orders[$value])) ? $orders[$value]=0 : $orders[$value]++;
$this->Category->id = $key;
$this->Category->set(array('parent_id' => (int)$value,
'display_order' => $orders[$value]
));
$this->Category->save();
}
try each and every answer but i think the last one will fit better at your problem.
From the CakePHP book.
When calling save in a loop, don’t forget to call create().
echo $data and match it with save() method's syntax.also do not slip to match data types of fields in table.
I would like to save several records for one model. This would have been done pretty easily with saveAll() if it hadn't been for a problem:
I have a notification form, where I select multiple users from a <select> and two fields, for subject and content respectively. Now, when I output what $this->data contains, I have:
Array([Notification] => Array
(
[user_id] => Array
(
[0] => 4
[1] => 6
)
[subject] => subject
[content] => the-content-here
)
)
I've read on Cake 1.3 book, that in order to save multiple records for a model, you have to have the $this->data something like:
Array([Article] => Array(
[0] => Array
(
[title] => title 1
)
[1] => Array
(
[title] => title 2
)
)
)
So, how do I 'share' the subject and content to all selected users?
First off, this database design needs to be normalized.
It seems to me like a Notification can have many Users related to it. At the same time, a User can have many Notifications. Therefore,
Introduce a join table named users_notifications.
Implement the HABTM Relationship: Notification hasAndBelongsToMany User
In the view, you can simply use the following code to automagically bring up the multi-select form to grab user ids:
echo $this->Form->input('User');
The data that is sent to the controller will be of the form:
Array(
[Notification] => Array
(
[subject] => subject
[content] => contentcontentcontentcontentcontentcontent
),
[User] => Array
(
[User] => Array
(
[0] => 4
[1] => 6
)
)
)
Now, all you have to do is called the saveAll() function instead of save().
$this->Notification->saveAll($this->data);
And that's the Cake way to do it!
Those values have to be repeat like this
Array([Notification] => Array(
[0] => Array
(
[user_id] => 4
[subject] => subjects
[content] => content
)
[1] => Array
(
[user_id] => 6
[subject] => subject
[content] => contents
)
)
)
$this->Notification->saveAll($data['Notification']); // should work
If you don't pass any column value, this cake will just ignore it
You're going to have to massage your form's output to suit Model::saveAll. In your controller:
function action_name()
{
if ($this->data) {
if ($this->Notification->saveMany($this->data)) {
// success! :-)
} else {
// failure :-(
}
}
}
And in your model:
function saveMany($data)
{
$saveable = array('Notification'=>array());
foreach ($data['Notification']['user_id'] as $user_id) {
$saveable['Notification'][] = Set::merge($data['Notification'], array('user_id' => $user_id));
}
return $this->saveAll($saveable);
}
The benefit here is your controller still knows nothing about your model's schema, which it shouldn't.
In fact, you could probably redefine saveAll in your model, which hands off correctly formatted input arrays to parent::saveAll, but handles special cases itself.
There might be a more cakey way of doing this, but I've used this kind of technique: First, change the form so that you get an array like this:
Array(
[Notification] => Array
(
[subject] => subject
[content] => contentcontentcontentcontentcontentcontent
),
[selected_users] => Array
(
[id] => Array
(
[0] => 4
[1] => 6
)
)
)
(Just change the multiselect input's name to selected_users.id)
Then loop through the user ids and save each record individually:
foreach( $this->data[ 'selected_users' ][ 'id' ] as $userId ) {
$this->data[ 'Notification' ][ 'user_id' ] = $userId;
$this->Notification->create(); // initializes a new instance
$this->Notification->save( $this->data );
}