How to use whereIn for column array? - php

I have data from a table structured like so:
table: students
{
id:
name:
classes: [
{
name: "gym"
},
{
name: "science"
},
{
name: "math"
},
{
name: "english"
}
]
}
reference array: science, math
How do I return only records that match the values found in the reference array. For example, if the reference array contains math and science then all records returned from the student table will need to have those values in the classes array name field.
I'm trying to use whereIn, but it does not accept a column (array) and field as the first parameter.

This might help:
Student::select("*")->whereIn('classes', $referencearray)
->get();

Related

MongoDB update not working with existing array [duplicate]

In MongoDB how do you use $set to update a nested value?
For example, consider a collection people with the following document:
{
_id: ObjectId("5a7e395e20a31e44e0e7e284"),
name: "foo",
address: { street: "123", town: "bar" }
}
How do I update the street field embedded in the address document from "123" to "Main Street"?
Using the dot notation:
db.people.update({ }, { $set: { "address.street": "Main Street" } })
In addition to Niels' answer, also do verify the "type" of the nested value. In my case, it was a "string" formed from json. Though this might be unlikely, but do ensure that the value has the right type.

Parse JSON without relying on order

I have this JSON, I'm parsing. I can retrieve all the values of field_data in order in PHP, but I need to traverse and get the values I wan't by specifying the field name, without concern for order:
JSON
{
created_time: "2018-05-14T16:11:02+0000",
id: "555555555555",
field_data: [
{
name: "bedrooms_interested_in?",
values: [
"2"
]
},
{
name: "When_are_you_looking?",
values: [
"January 1s 2019"
]
},
{
name: "email",
values: [
"xxxx#domain.com"
]
},
{
name: "full_name",
values: [
"John Michael"
]
},
{
name: "phone_number",
values: [
"+15555555555"
]
}
]
}
PHP
This is grabbing the last 3 fields. I'm currently able to pull the fields_data in order like this, but how could I grab the value fields I want by specifying the name field I'm looking for?
$data = json_decode($response, true);
// var_dump($data);
file_put_contents('dump.txt', $response);
$lead_email = $data['field_data'][2]['values'][0];
$lead_fullname = $data['field_data'][3]['values'][0];
$lead_phone = $data['field_data'][4]['values'][0];
You can use array_column to re-index an array by a given column, which will give you easier and more reliable access to the data:
$data['field_data'] = array_column($data['field_data'], null, 'name');
This allows you to access fields by name instead of their numeric index, e.g.
echo $data['field_data']['email']['values'][0];
// xxxx#domain.com
See https://eval.in/1003874 for a full example
(Note, I added some quotes to your JSON property names, since they were missing in the question. I assume that's a formatting issue, since this wouldn't work at all otherwise)

How to use fractal transformer in Laravel?

I try to use the Fractal library from The PHP League in my project
I have model: Object with fields id, name. It has relation prototypes as one to one.
So, model Prototype has relation Fields. Where model Fields contains fields: name, id.
Also there is separated model: ObjectFields that contains values for each object fields. For example:
object_id | value
1 1
In result I need to get output JSON:
objects : {
"id" : 1,
"name" : "Weapon",
"prototype": {
"id" : 1,
"name" : "Prototype Name",
"fields" : {
"field_name" : ObjectFields.value
}
}
}
Look please on
"fields" : {
"field_name" : ObjectFields.value
}
so, It would be field name as key and value from model ObjectFields.
Now I do request using query as:
Object::with("prototype.field.name.value");
It returns me nested output object, but I need that last 3 realations will be in one object by key field.name

JMSserializer deserialize related Doctrine Entity

I have json like this:
"relatedCollection": [
{
id:1,
name: "something",
country: {
id:1
}
},
{
id:2,
name: "something 2",
country: {
id:1
}
}
]
Two related items that have a common country that exists in database. If I deserialize with JMSserializer, and it creates a two different instances of COUNTRY entity.
When you apply merge with doctrine, set properly country to "something", but not to "something 2". So the result after save is:
"relatedCollection": [
{
id:1,
name: "something",
country: {
id:1
}
},
{
id:2,
name: "something 2",
country: null
}
]
For Doctrine there are two different entities called Country that are detached. And doctrine attach the first and save it properly but not the second.
This only happens with relations ManyToOne, when you merge a collection with the same related entity id.
If you save country 1 and country 2, there'is no repeated country, so save property.
Any solution?
JMS Serializer just does the object deserialization, it does not handles the doctrine object for you.
You have to do any kind of merge, cascade merge, for example
http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/working-with-associations.html#transitive-persistence-cascade-operations
You can write a deserialization Listener\Subscriber to do auto-merge for you automatically

Create a blank array from a model in cakephp

hi,
I have a working model and I want to be able to just create an array from it but with only the key names and just have them empty.
To illustrate, my model Client comes from the clients table, and for example when I do this:
$this->Client->find( 'first' );
I get the following array (json encoded):
{
Client: {
id: "39",
name: "andrux",
phone: "1234567890",
email: "me#andrux.com",
city_id: "2"
},
City: {
id: "2",
city_name: "andruxville"
}
}
As you can see, I set this model to have a relationship with the City model, so I get both Client and City arrays as a result of my find method.
Now I need to get the same array but without values, and I just can't find an answer for this, I have tried some solutions but none have worked the way I want, for example I tried using array_map function and the schema method of the model but that just gives me the column names of the clients table, which I can set to null if I want but what about the City model?
The end result I want is the following:
{
Client: {
id: "",
name: "",
phone: "",
email: "",
city_id: ""
},
City: {
id: "",
city_name: ""
}
}
Anyone knows how to accomplish this? I rather find a way of doing this the cakephp way - if there is one - but any solution that gets me to my desired result will be greatly appreciated.
Thanks guys!
Well, if its ok, you could create a custom function in your model and pass in the result to it, like:
//can be added to your model
function reset_values($array, $replace_with) {
foreach ($array as $key => $arr) {
if(is_array($arr)) $res[$key] = reset_values($arr,$replace_with);
else $res[$key] = $replace_with;
}
return $res;
}
$resetedArr = reset_values($arr, null); //replace values with null
echo "<pre>"; print_r($resetedArr);

Categories