In my Symfony 4 project, I have 2 entities: Question and PossibleAnswer. A question can have many possible answers and an answer can be used in many different questions.
I want to POST a question, I use FOSRestBundle. This is how I would format the body of my request:
{
"title": "my question",
"possibleAnswers": [
{
"id": 1,
"title": "my first answer"
}
]
}
At least this is how FOSRestBundle formats the answer when I create the data directly in the database and GET the question.
But I keep getting the same error:
Validation Failed, possibleAnswers This value is not valid
I use Symfony forms to validate my POST and the fieldType I use for the possibleAnswers field of Question is an EntityType with the option multiple set to true. Maybe that's where the error comes from.
So my question is: How do I format the body of my request to add a valid possibleAnswers field ? and if I am doing it right then, which form type should I use for it to work ?
Sorry for not adding any code, the context is actually quite more complex but I'm pretty sure either the JSON Format of my field or the form type is wrong.
[edit]
here are all the form types I tried for the possibleanswers field
//->add('possibleAnswers', EntityType::class, array('data' => [], 'required' => false, 'multiple' => true, 'class' => PossibleAnswer::class))
->add('possibleAnswers', CollectionType::class, array('required' => false, 'entry_type' => EntityType::class, 'entry_options' => array('class' => PossibleAnswer::class)))
//->add('possibleAnswers', CollectionType::class, array('required' => false, 'entry_type' => PossibleAnswerType::class))
//->add('possibleAnswers', CollectionType::class, array('required' => false, 'entry_type' => PossibleAnswerType::class, 'entry_options' => array('class' => PossibleAnswer::class)))
here are all the JSON formats I tried to select the possibleanswers:
{
"title": "a postman created question",
"answerConditions": [
{
"id": 1,
"title": "already existing question",
}
]
}
{
"title": "a postman created question",
"answerConditions": [
{
"possibleanswer_id": 1
}
]
}
{
"title": "a postman created question",
"answerConditions": [
{
"id": 1
}
]
}
{
"title": "a postman created question",
"answerConditions": [
1
]
}
If you want to create the PossibleAnswer when posting the Question, you need to replace in your form the EntityType by a CollectionType.
I often use it in my POST request when I want to post the sub-object (here the PossibleAnswer) when I post the parent (Question) :
Question form
$builder
->add('possibleAnswers', CollectionType::class, array(
'entry_type' => PossibleAnswerType::class,
'allow_add' => true,
))
;
PossibleAnswer form :
$builder
->add('title')
;
And the json looks like :
{
"title": "my question",
"possibleAnswers": [
{
"title": "my first answer"
}
]
}
Hope it can help.
Edit :
If you want to link a PossibleAnswer to a Question while creating you Question you can use this :
Question form :
$builder
->add('possibleAnswer', EntityType::class, array(
'class' => YOUR_ENTITY::class,
'multiple' => true
));
And you json should only contains the id of the entity you want to link to. It should be like this :
{
"title": "a postman created question",
"answerConditions": [
1
]
}
Related
I am on Laravel 7.x and I have two models (CustomerOrder composed of many CustomerOrderLines) with parent - child relationship. Parent (CustomerOrder) model has a json type field among its fields.
CustomerOrderResource.php:
return [
'id' => $this->id,
'wfx_oc_no' => $this->wfx_oc_no,
'qty_json' => json_decode($this->qty_json)
];
CustomerOrderLineResource.php:
return [
'id' => $this->id,
'description' => $this->description,
'customer-order' => $this->customerOrder
];
CustomerOrder->GET request returns properly formatted data as:
"data": {
"id": 11,
"wfx_oc_no": 12,
"qty_json": {
"L": "20",
"M": "30",
"S": "20",
"XL": "100"
}
}
But for CustomerOrderLine->GET, the response is as:
"data": {
"id": 15,
"description": "test desc",
"customer-order": {
"id": 11,
"wfx_oc_no": 12,
"qty_json": "{\"L\": \"20\", \"M\": \"30\", \"S\": \"20\", \"XL\": \"100\"}"
}
}
json field is not properly formatted. It seems it doesn't go through Resource class. Please let me know, how can I get this fixed?
FYI
CustomerOrderLine.php:
public function parent()
{
return $this->belongsTo(CustomerOrder::class);
}
Finally managed to get it solved using json cast. The field was included in $casts array in the model.
I want to create an API in the below format. But I am unable to do it. I am using the API resource. I have tried using different queries but I am not getting the exact solution.Please help me.
Thank you
what I have tried
$marks = StudentMarksResource::collection(StudentsMark::whereIn('academic_id',$ids)->whereIn('student_id',$studentid)->get());
my API resource file
public function toArray($request)
{
return [
'exam_type' => $this->exam_type,
'details' => [
/* 'class_id' => Course::find($this->class_id),
'batch_id' => Batch::find($this->batch_id),
'student_id' => Student::find($this->student_id), */
'subject' => $this->subject,
'marks' => $this->marks,
'marksgrade' => $this->marksgrade,
'total' => $this->total,
'grade' => $this->grade,
'percentage' => $this->percentage,
'year' => $this->year,
],
];
}
what I want is this
data": [
{
"exam_type": "Unit Test 1",
"details": {
//subject1 details
},
{
//subject2 details
},
{
//subject3 details
},
"exam_type": "Unit Test 2",
"details": {
//subject1 details
},
{
//subject2 details
},
{
//subject3 details
},
},
what I am getting for the above code
"data": [
{
"exam_type": "Unit Test 1",
"details": {
//subject1 marks details
}
},
{
"exam_type": "Unit Test 1",
"details": {
//subject2 marks details
}
},
Here is something you can try:
First get exam types
$exam_types = StudentsMark::select('exam_type', 'student_id', 'academic_id')->whereIn('academic_id',$ids)->whereIn('student_id',$studentid)->get();
$marks = StudentMarksResource::collection($exam_types);
Now inside resource that's when you can retrieve the details
#do not forget to import StudentsMark
#and you can make a resource for details, just to make clean code
return [
'exam_type' => $this->exam_type,
'details' => StudentsMark::where('academic_id', $this->academic_id)
->where('student_id', $this->student_id)
->get();
];
I am trying to send some data along to Algolia through the toSearchableArray. Any strings I have stored in my DB are sending along fine, but I hit a roadblock when trying to push nested JSON data along—the information is being sent as a string with characters escaped.
This is a sample of the nested object that I am storing in my table (MySQL with a JSON data type):
[
{
"id": 19,
"name": "Mathematics",
"short": "Math"
},
{
"id": 23,
"name": "Science",
"short": "Science"
},
{
"id": 14,
"name": "Health and Life Skills",
"short": "Health"
}
]
My model looks like this:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Resource extends Model
{
use Searchable;
protected $primaryKey = 'objectID';
public function toSearchableArray()
{
$data = $this->toArray();
$data['grades'] = explode(';', $data['grades']);
$data['units'] = explode(';', $data['units']);
return $data;
}
}
I get an output that looks like this:
array:22 [
"objectID" => 1
"name" => "Resource #1"
"slug" => "resource-1"
"write_up" => """
This is an example write up.
"""
"author" => "johnny"
"type_name" => "Lesson Plan"
"language" => "English"
"grades" => array:3 [
0 => "Kindergarten"
1 => "Grade 1"
2 => "Grade 4"
]
"subjects" => "[{"id": 19, "name": "Mathematics", "short": "Math"}, {"id": 23, "name": "Science", "short": "Science"}, {"id": 14, "name": "Health and Life Skills", "short": "Health"}]"
"units" => array:2 [
0 => "Unit A"
1 => "Unit B"
]
"main_image" => "https://dummyimage.com/250x325/000000/fff.png&text=Just+a+Test"
"loves" => 88
"downloads" => 280
"created_at" => "2018-01-01 13:26:47"
"updated_at" => "2018-01-02 10:10:32"
]
As you can see, the 'subjects' attribute is being stored as a string. I know there is attribute casting in 5.5 (I am running 5.5), but I am not too clear on how I would implement the example they have for Array & JSON Casting in my work above. https://laravel.com/docs/5.5/eloquent-mutators#attribute-casting
Would anyone be willing to show me an example?
I'd rely on Attribute Casting for this, add a $casts property in your model and it will be done automatically.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Resource extends Model
{
use Searchable;
protected $primaryKey = 'objectID';
protected $casts = [
'subjects' => 'array',
];
public function toSearchableArray()
{
// Same function as you posted
}
}
You can also do it manually in your toSearchableArray method with $data['subjects'] = json_decode($this->subjects, true);
I answered with more details on this other posts: https://discourse.algolia.com/t/laravel-array-json-casting-to-algolia/4125/2
I've got a question regarding JSON in Logstash.
I have got a JSON Input that looks something like this:
{
"2": {
"name": "name2",
"state": "state2"
},
"1": {
"name": "name1",
"state": "state1"
},
"0": {
"name": "name0",
"state": "state0"
}
}
Now, let's say I want to add a field in the logstash config
json{
source => "message"
add_field => {
"NAME" => "%{ What to write here ?}"
"STATE" => "%{ What to write here ?}"
}
}
Is there a way to access the JSON Input such that I get a field Name with value name1, another field with name 2 and a third field with name 3. The first key in the JSON is changing, that means there can only be one or many more parts. So I don't want to hardcode it like
%{[0][name]}
Thanks for your help.
If you remove all new lines in your input you can simply use the json filter. You don't need any add_field action.
Working config without new lines:
filter {
json { source => message }
}
If you can't remove the new lines in your input you need to merge the lines with the multiline codec.
Working config with new lines:
input {
file {
path => ["/path/to/your/file"] # I suppose your input is a file.
start_position => "beginning"
sincedb_path => "/dev/null" # just for testing
codec => multiline {
pattern => "^}"
what => "previous"
negate => "true"
}
}
}
filter {
mutate { replace => { "message" => "%{message}}" } }
json { source => message }
}
I suppose that you use the file input. In case you don't, just change it.
Output (for both):
"2" => {
"name" => "name2",
"state" => "state2"
},
"1" => {
"name" => "name1",
"state" => "state1"
},
"0" => {
"name" => "name0",
"state" => "state0"
}
I am using the latest ELK (Elasticsearch 1.5.2 , Logstash 1.5.0, Kibana 4.0.2)
I have a question that
sample .json
{ "field1": "This is value1", "field2": "This is value2" }
longstash.conf
input {
stdin{ }
}
filter {
json {
source => "message"
add_field =>
{
"field1" => "%{field1}"
"field2" => "%{field2}"
}
}
}
output {
stdout { codec => rubydebug }
elasticsearch {
host => "localhost"
index => "scan"
}
}
Output:
{
"message" => "{ \"field1\": \"This is value1\", \"field2\": \"This is value2\" }",
"#version" => "1",
"#timestamp" => "2015-05-07T06:02:56.088Z",
"host" => "myhost",
"field1" => [
[0] "This is value1",
[1] "This is value1"
],
"field2" => [
[0] "This is value2",
[1] "This is value2"
]
}
My question is 1) why the field result appear double in the result? 2) If there is nested array , how is it should reference in the logstash configure?
Thanks a lot!
..Petera
I think you have misunderstood what the json filter does. When you process a field through the json filter it will look for field names and corresponding values.
In your example, you have done that with this part:
filter {
json {
source => "message"
Then you have added a field called "field1" with the content of field "field1", since the field already exists you have just added the same information to the field that was already there, it has now become an array:
add_field =>
{
"field1" => "%{field1}"
"field2" => "%{field2}"
}
}
}
If you simplify your code to the following you should be fine:
filter {
json {
source => "message"
}
}
I suspect your question about arrays becomes moot at this point, as you probably don't need the nested array, and therefore, won't need to address it, but in case you do, I believe you can do this like so:
[field1][0]
[field1][1]