I have got data without "id" field from database. But when I parse the data to json string and show it on screen, I saw an "id" field on json string. How to remove this "id" field?
Here is my code:
-report_logic.rb
def fetch_data ()
#datalist=WorkEarlyOverTime
#datalist = #datalist.joins("INNER JOIN `works` ON `work_early_over_times`.`work_id` = `works`.`id`")
#datalist = #datalist.select('`work_early_over_times`.work_id AS 1次協力会社名','`work_early_over_times`.working_temp_person AS 職種名','`work_early_over_times`.working_temp_hour AS 作業場所','`work_early_over_times`.at_time_overtime_start AS 作業内容','`works`.`contents`')
#datalist = #datalist.where(work_id: $work_id)
return #datalist
end
- report_controller.rb
def work_late
report_logic = ReportLogic.new(params)
#work_late = report_logic.fetch_data()
render action: :work_late
end
- work_late.json.jbuilder
json.一覧 #work_late
When I show the string I expected the output is:
{"一覧":[
{"1次協力会社名":1,
"職種名":"0",
"作業場所":"0.00 ",
"作業内容":"2000-01-01T19:00:00.000+00:00",
"contents":"作業内容1"}
]}
but the actual output is:
{"一覧":[
{"id":null,
"1次協力会社名":1,
"職種名":"0",
"作業場所":"0.00 ",
"作業内容":"2000-01-01T19:00:00.000+00:00",
"contents":"作業内容1"}
]}
As your desired output is a JSON, you could try .as_json by excepting the id field as follows:
#datalist.select('`work_early_over_times`.work_id AS 1次協力会社名','`work_early_over_times`.working_temp_person AS 職種名','`work_early_over_times`.working_temp_hour AS 作業場所','`work_early_over_times`.at_time_overtime_start AS 作業内容','`works`.`contents`').as_json(:except => :id)
Your detailed fetch_data function will be like this:
def fetch_data ()
#datalist=WorkEarlyOverTime
#datalist = #datalist.joins("INNER JOIN `works` ON `work_early_over_times`.`work_id` = `works`.`id`")
#datalist = #datalist.select('`work_early_over_times`.work_id AS 1次協力会社名','`work_early_over_times`.working_temp_person AS 職種名','`work_early_over_times`.working_temp_hour AS 作業場所','`work_early_over_times`.at_time_overtime_start AS 作業内容','`works`.`contents`').as_json(:except => :id)
#datalist = #datalist.where(work_id: $work_id)
return #datalist
end
If id is always null then just ignore all nils:
json.ignore_nil!
json.一覧 #work_late
If that's not the case then you have to filter out the id before serializing it. One way would be:
json.一覧 #work_late.serializable_hash.except("id")
Related
I have several tables that have JSON arrays stored within fields.
Using PHP PDO I am able to retrieve this data without issue using:
$query1 = $database->prepare("SELECT * FROM module_settings
WHERE project_token = ? AND module_id = ? ORDER BY id DESC LIMIT 1");
$query1->execute(array($page["project_token"], 2));
$idx = $query1->fetch(PDO::FETCH_ASSOC);
$idx["settings"] = json_decode($idx["settings"]);
This returns a string like:
{"mid":"","module_id":"1","force_reg_enable":"1","force_reg_page_delay":"2"}
Attempting to gather the same data via PhalconPHP
$result = Modulesettings::findFirst( array(
'conditions' => 'project_token = "' . $token . '"' ,
'columns' => 'settings'
) );
var_dump($result);
Provides a result of
object(Phalcon\Mvc\Model\Row)#61 (1) { ["settings"]=> string(167) "{"text":"<\/a>
<\/a>
","class":""}" }
What do I need to do different in Phalcon to return the string as it is stored in the table?
Thank you.
You have 2 approach
First :
Get the settings with this structure :
$settings = $result->settings;
var_dump($settings);
Second :
First get array from resultset, then using the array element :
$res = $result->toArray();
var_dump($res['settings']);
Try it.
You can decode json right in your Modulesettings model declaration:
// handling result
function afterFetch() {
$this->settings = json_decode($this->settings);
}
// saving. Can use beforeCreate+beforeSave+beforeUpdate
// or write a Json filter.
function beforeValidation() {
$this->settings = json_encode($this->settings);
}
See my example below where I put 'something' I'm just confused on what to map:
array_ids = ['1','2']
array = array_ids.map(something).join(',')
So when I do:
order_sql = "FIELD(ID,#{array})"
I get this:
order_sql = "FIELD(ID,'1','2')"
You can do something like:
array_ids = ['1','2']
array = array_ids.map { |id| "'#{id}'" }.join(',')
p array
# => "'1','2'"
order_sql = "FIELD(ID,#{array})"
p order_sql
# => "FIELD(ID,'1','2')"
Hope that helps!
I am trying to perform the following:
def query = Scholarship.withCriteria {
eq('activeInd', "A")
}
allInfo = query.list(sort: "name", order: "asc")
return allInfo
}
I was previously using this, but now I need to change it to something like above:
def allInfo = Scholarship.list(sort: "name", order: "asc")
I need to get all scholarship objects with activeInd field = A. Then I need to use .list on them to get all those scholarships in a list/array in ascending order based off the name. The error I am running into is as follows:
No signature of method: java.util.ArrayList.list() is applicable for argument types: () values: [] Possible solutions: last(), last(), first(), first(), is(java.lang.Object), toList()
Because you are using list() on the result of withCriteria. This can resolved by either of the three ways:
def result = Scholarship.createCriteria().list(sort: 'name', order: 'asc') {
eq('activeInd', "A")
//alternatively
//order('name', 'asc')
}
or
def result = Scholarship.withCriteria {
eq('activeInd', "A")
order('name', 'asc')
}
or
def result = Scholarship.where {
activeInd == 'A'
}.list(sort: 'name', order: 'asc')
I would prefer the third approach using where.
I have a simple example:
result = db.command({
'findandmodify' : 'Task',
'remove' : True
})
print result
How to get first inserted element ? ( order by _id )
The find_one method in pymongo should return the first document in the collection: http://api.mongodb.org/python/current/tutorial.html#getting-a-single-document-with-find-one
My problem is:
I would like to get all advertistments where id of Description column is equals with Advertistment's column.
Let's say that Advertistment column is connected with Description column.
I would like to gain all description's id where one of its column called type_of_house is equals m.
Then show all advertistment where advertistment's id is equals with description's id.
In short way: advertistment shows info about houses, descriptions store houses type D and M and I want show all advertistments with houses type of M.
This is correct sql:
SELECT * FROM advertistment, description WHERE advertistment.id_advertistment = description.id_description AND description.type_of_house = "m"
I have no idea how write it into zend. I tried something like that. This function I wrote in model folder.
public function takeAll() {
$select = $this->_db->select();
$select->from(array('a' => 'advertistment', 'd' => 'description'));
$select->where('a.id_advertistment = d.id_description AND d.type_of_house = m');
return $select->query()->fetchAll();
}
You're actually quite close. This should work:
public function takeAll() {
$select = $this->_db->select();
$select->from(array('a' => 'advertistment'));
$select->join(array('d' => 'description'), 'a.id_advertistment = d.id_description');
$select->where('d.type_of_house = ?', 'm');
return $this->_db->fetchAll($select);
}