CHtml::listData findall returns last element - yii

I have a dropdown list. Within the dropdownlist, I use listdata to retrieve the data from another table. But strangely, it only gets the last item in the table.
$form->dropDownList($model,'status_id',CHtml::listData(OrderStatus::model()->findAll(),'status_id', 'status'))
chtml::listdata strangely only shows this array(1) { [""]=> string(9) "Delivered" } while in the table there are 7 rows/id, where delivered is the last entry. What happened to the others?
Another odd thing is that $model->status_id is actually id 1, so it shouldn't display 'Delivered', it should be showing 'New'.

Take a look at this:
Example 1: Generating a list data for categories
// you can use here any find method you think proper to return your data from db*/
$models = categories::model()->findAll();
// format models resulting using listData
$list = CHtml::listData($models, 'category_id', 'category_name');
print_r($list);
HTML Output (Example):
array("1" => "Arts", "2" => "Science", "3" => "Culture");

See if you have by any chance a default scope.
Just do a debug of OrderStatus::model()->findAll() and see if it returns 7 records or just 1.

Your chtml::listdata strangely showing
array(1) { [""]=> string(9) "Delivered" }
Is because you must have same entries in status_id column of all the rows in OrderStatus which is blank/null as per above array.
In below call
CHtml::listData(OrderStatus::model()->findAll(),'status_id', 'status')
status_id is key( index of array ) for your generated list array & its getting overwritten by same value everytime, thats whay its showing only one & last value.

Related

How many rows got inserted into SQL database with ts-postgres?

I use ts-postgres and INSERT INTO to add new rows to my table.
import { Client } from 'ts-postgres';
let query = '...';
let res = await Client.query(query, [username, email]);
The result I get from Client.query is the following result:
Result {names: Array(0), rows: Array(0), status: "INSERT 0 1"}
Result {names: Array(0), rows: Array(0), status: "INSERT 0 0"}
In the first case 1 line got added, in the second 0. Do I really need to parse the status string in order to see how many rows got added?
Yep, that's something you have to deal with when working at low level (without ORM)
So here's a simple function to check for inserted rows
checkInserted(result): number {
const status = result.status.split();
return parseInt(status[status.length-1]);
}
you can customize it according to your requirements
It looks like the answer is yes, you really do need to parse the status string.
According to the Postgres protocol documentation, when the database finishes executing an insert command, it sends a CommandComplete message back to the client. The CommandComplete message consists of a byte that identifies the message type, a length, and a "tag," which is a string:
For an INSERT command, the tag is INSERT oid rows, where rows is the
number of rows inserted. oid used to be the object ID of the inserted
row if rows was 1 and the target table had OIDs, but OIDs system
columns are not supported anymore; therefore oid is always 0.
That tag is the status that you are seeing. There's nothing else in the CommandComplete message.
The Node Postgres client does include a rowCount member in result, but if you look at the code, you will see that it just parses it out of the string. The Java JDBC driver also parses the string.

NetSuite sublist column order (suitescript 2)

Is there any way to add a sublist field via a user event script and define its column position? By default the field is added as the last column. It appears you can move form fields around, but can't change sublist column order.
We can able to edit sublist column postion.
But it can be vary based on your records either transaction or suitlet script or any other things just elaborate your question pls.
// For This Example we create a first Field
var field1 = form.addField({
id : 'textfield1',
type : serverWidget.FieldType.TEXT,
label : 'Text'
});
// For This Example we create a first Field
var field2 = form.addField({
id : 'textfield2',
type : serverWidget.FieldType.TEXT,
label : 'Text'
});
form.insertField({
field : field2,
nextfield : 'textfield1'
});

Get the difference from data in Swift and data in database

A table in database have two column: ID and Value
In my project there are another data in a dictionary that key is ID and value is Value.
I want to get difference: Data that are in the dictionary and are not in the database. If these both data were in database, I could use SQL commands "Except" or "Not Exist" to get the difference like below image.
What is the best way to do this?
I use SQLiteDB that the result of query is a dictionary like this:
[["ID":"id1", "Value": "val1"], ["ID":"id2", "Value": "val2"],...]
Also notice that both columns should be considered while compare these two data (dictionary and data in db).
// here we get intersection of new data and data we have
let intersection = dataSet1.filtered { data in dataSet2.contains(where: { $0.id == data.id })}
// delete elements from dataSet1 which belongs to intersection
let dataSet1MinusDataSet2 = dataSet1.filter { data in intersection.contains(where: { data.id == $0.id })}
I've written code here without any Xcode so errors in syntax is possible but I think you will get the idea

pdo print info using given value pdo

function getCars($memebrNo) {
// STUDENT TODO:
// Change lines below with code to retrieve the cars of the member from the database
$stmd=$dbh->prepare('SELECT name FROM PeerPark.Car JOIN PeerPark.Member WHERE memberNo=:memberNo');
$stmd->bindParam(':memberNo', $memberNO, PDO::PARAM_STR);
$stmd->execute;
$result=$stmd->fetchAll(PDO::FETCH_COLUMN);
var_dump($result);
these are my codes, but i want to print like the following, besides can someone tell me am i using the memberNo given in function the right way? Thanks
$results = array(
array('car'=> 'Gary'),
array('car'=> 'Harry' )
);
To get a result like that, your query should be:
$stmd=$dbh->prepare('SELECT name AS car FROM PeerPark.Car JOIN PeerPark.Member WHERE memberNo=:memberNo');
and then you should fetch the results with:
$result = $stmd->fetchAll(PDO::FETCH_ASSOC);
PDO::FETCH_COLUMN returns an index array containing all the values of the column. PDO::FETCH_ASSOC returns a 2-dimensional array where each element is an associative array mapping the column names to their values.

How can I store the results of a SQL query as a hash with unique keys?

I have a query that returns multiple rows:
select id,status from store where last_entry = <given_date>;
The returned rows look like:
id status
-----------------
1131A correct
1132B incorrect
1134G empty
I want to store the results like this:
$rows = [
{
ID1 => '1131A',
status1 => 'correct'
},
{
ID2 => '1132B',
status2 => 'incorrect'
},
{
ID3 => '1134G',
status3 => 'empty'
}
];
How can I do this?
What you are looking for is a hash of hash in Perl. What you do is
Iterate over the results of your query.
Split each entry by tab
Create a hash with the id as key and status as value
Now to store the hash created by each such query you create another hash. Here the key could be something like 'given_date' in your case so you could write
$parent_hash{given_date}=\%child_hash
This will results in the parent hash having a reference of each query result.
For more you can refer to these resources:
http://perldoc.perl.org/perlref.html
http://www.thegeekstuff.com/2010/06/perl-array-reference-examples/
Have a look at DBI documentation.
Here is part of script that does what you want:
my $rows;
while(my $hash_ref = $sth->fetchrow_hashref) {
push #$rows, $hash_ref;
}
You can do this by passing a Slice option to DBI's selectall_arrayref:
my $results = $dbh->selectall_arrayref(
'select id,status from store where last_entry = ?',
{ Slice => {} },
$last_entry
);
This will return an array reference with each row stored in a hash. Note that since hash keys must be unique, you will run into problems if you have duplicate column names in your query.
This is the kind of question that raises an immediate red flag. It's somewhat of an odd request to want a collection (array/array reference) of data structures that are heterogeneous---that's the whole point of a collection. If you tell us what you intend to do with the data rather than what you want the data to look like, we can probably suggest a better solution.
You want something like this:
# select the data as an array of hashes - retured as an arrayref
my $rows = $dbh->selectall_arrayref($the_query, {Slice => {}}, #any_search_params);
# now make the id keys unique
my $i = 1;
foreach my $row ( #$rows) {
# remove each column and assign the value to a uniquely named column
# by adding a numeric suffix
$row->{"ID" . $i} = delete $row->{ID};
$row->{"status" . $i} = delete $row->{status};
$i += 1;
}
Add your own error checking.
So you said "save as a hash," but your example is an array of hashes. So there would be a slightly different method for a hash of hashes.