I've :
AdminValidateurController.php on line 43:
array:2 [▼
0 => Validateur^ {#986 ▼
-id: 5
-ordre: 1
-validateur: User^ {#773 ▶}
}
1 => Validateur^ {#988 ▼
-id: 6
-ordre: 2
-validateur: User^ {#1015 ▶}
}
]
A validator contains a User.
I would like to test if a certain User is part of the dump, the Validators board.
For that I have a Validator object, which I can recover the user with $ validateur->getValidateur (). And I want to test if it is already part of validateurs ($ validateurs).
For that, I did:
if (in_array($validateur->getValidateur(), $validateurs)) {
dd("oui");
}
But it doesn't work :s
If your array comes from a database you can check if the user is a validator using a query. For instance:
$exists = $validateurRepository->findOneBy(['validateur' => $validateur->getValidateur());
// Or find($validateur->getId()) if it's already persisted
if (null != $exists) {
//...
}
But if you already have the result as an array you can remap it to a User array and do the checking:
$users = array_map(function($e) { return $e->getValidateur(); }, $validateurs);
if (in_array($validateur->getValidateur(), $users)) {
dd("oui");
}
Related
Trying to get matching id's from a table and inserting them again in the same table under differnet relationship.
$contentPack = ContentPack::find($id);
$cloned_pack_goals = DB::table('content_pack_goal')->where('content_pack_id' , $contentPack->id)->get();
$cloned_pack_goal_ids = $cloned_pack_goals->goal_id;
Produces Exception
Exception
Property [goal_id] does not exist on this collection instance.
dd($cloned_pack_goals); outputs:
Illuminate\Support\Collection {#2466 ▼
#items: array:2 [▼
0 => {#3129 ▼
+"goal_id": 4
+"content_pack_id": 2
}
1 => {#2467 ▼
+"goal_id": 9
+"content_pack_id": 2
}
]
}
How to get goal_ids from the output to insert them into the same table again but with a different relation?
$newPack = $contentPack->replicate();
DB::table('content_pack_goal')->insert(['content_pack_id' => $newPack->id,'goal_id' => $cloned_pack_goal_ids]);
Am doing something wrong when getting the ID's and when inserting them. tried using ->first(); it works but only one id gets inserted
$cloned_pack_goals is a collection, so you need to exclude goal_ids from all collection records separately.
This snippet may help you:
$cloned_pack_goal_ids = DB::table('content_pack_goal')->where('content_pack_id' , $contentPack->id)->pluck('goal_id')->toArray();
foreach($cloned_pack_goal_ids as $key => $goal_id) {
DB::table('content_pack_goal')->insert(['content_pack_id' => $newPack->id,'goal_id' => $goal_id]);
}
To get an array of only the Ids, use pluck() and toArray()
$cloned_pack_goal_ids = DB::table('content_pack_goal')
->where('content_pack_id' , $contentPack->id)
->pluck('goal_id') // returns a collection of only Ids.
->toArray(); // returns an array from the collection.
Write your query in this format this will give you the require output:
$cloned_pack_goals = DB::table('content_pack_goal')->where('content_pack_id' , $contentPack->id)->get()->toArray();
$cloned_pack_goal_ids = $cloned_pack_goals[0]->goal_id;
I'm having a data set which is coming through request in my Laravel 6 application, I want to convert this to collection but it is not working out:
$request_status = json_decode($request->status);
$data = collect($request_status)->pluck('id');
dd($data);
This is giving me output of null;
When I do dd($request->status):
"[{"id":3,"type":"Awarded","name":"Awarded"}]"
No change happens in the data, if I do dd($data) I get:
Collection {#791 ▼
#items: array:1 [▼
0 => null
]
}
I tried doing json_decode($request->status, true) but no luck.
If you dd $request_status it returns
"[{"id":3,"type":"Awarded","name":"Awarded"}]"
as a string? Makes sense that the pluck('id') doesn't work then. If so, make sure that returns an array and it'll work.
When I try
$request_status = json_decode('[{"id":3,"type":"Awarded","name":"Awarded"}]');
$data = collect($request_status)->pluck('id');
dd($data);
it returns
Illuminate\Support\Collection^ {#631
#items: array:1 [
0 => 3
]
}
Not sure about correctness but, I have tried something
$request_status = json_decode($request->status,1);
$data = collect([$request_status])->pluck('id')->first();
dd($data);
This is my array $dataRep:
array:4 [▼
0 => Fields {#7051 ▶}
1 => Fields {#7328 ▶}
2 => Fields {#7334 ▶}
3 => Fields {#7340 ▶}
]
I like to store data with Symfony formbuilder in the database:
$dataEntity= new Data::class();
foreach ($dataRep as $dataField) {
$dataEntity->setContent("something");
}
$this->em->persist($dataEntity);
$this->em->flush();
But only one entry is stored in the database and not as expected 4 entries.
Move your code into your loop for it to run on each iteration:
foreach ($dataRep as $dataField) {
$dataEntity = new Data();
$dataEntity->setContent("something");
$this->em->persist($dataEntity);
}
$this->em->flush();
Note: You also had a syntax error that I just noticed (new Data::class()).
You are reseting the content on the same data class and then storing it.
Just foreach the whole process:
foreach ($dataRep as $dataField) {
$dataEntity = new Data();
$dataEntity->setContent("something");
$this->em->persist($dataEntity);
}
$this->em->flush();
//sample database
At first Scholar has 1,2,3,4,5,6 ids
Scholar
Id
1
2
3
4
5
6
Now 1 and 2 were taken under table Member
Member
Id
1 - taken
2 - taken
Next 3 and 4 were taken under table AddRequest
AddRequest
Id
3 - taken
4 - taken
now the Scholar table should only has the ids 5 and 6 which i want to display.
how can i resolve this problem using my code below
public function ListOrgaScholar($ship_id)
{
$members = Member::where('ship_id','=',$ship_id)->get();
$members = $members->toArray();
$scholar_ids = array_pluck($members, 'scholar_id');
$scholar_exits = Scholar::whereNotIn('scholar_id', $scholar_ids)->get();
$requests = AddRequest::where('ship_id','=',$ship_id)->get();
$requests = $requests->toArray();
$scholar_ids = array_pluck($requests, 'scholar_id');
$add_exits = Scholar::whereNotIn('scholar_id', $scholar_ids)->get();
if ($scholar_exits == true && $add_exits == true) {
$scholars = (new Scholar)->newQuery()->select('*');
$scholars = $scholars->get();
dd($scholars);
}else{
}
}
result of dd
Collection {#302 ▼
#items: array:7 [▼
0 => Scholar {#305 ▶}
1 => Scholar {#306 ▶}
2 => Scholar {#307 ▶}
3 => Scholar {#308 ▶}
4 => Scholar {#309 ▶}
5 => Scholar {#310 ▶}
6 => Scholar {#311 ▶}
]
}
This for Member and AddRequest Table
This for Scholar
After all checking regarding the members and the addRequests you are trying to select everything from the Scholar table that is the reason why you are getting all six records.
I would add two fields likeis-member and has-request to the scholar table and update the boolean values to true based on events like - is-member becomes true when the Scholar transitions to become a member and has-request becomes true when the Scholar add a request or something like that. So then it becomes very easy to get scholars who are neither members nor do they have any requests.
However in your case, I think you should try as below:
public function ListOrgaScholar($ship_id)
{
$scholar_ids = [];
$members = Member::where('ship_id','=',$ship_id)->get();
//$members = $members->toArray();
//$scholar_ids = array_pluck($members, 'scholar_id');
//$scholar_exits = Scholar::whereNotIn('scholar_id', $scholar_ids)->get();
foreach($members as $member){
$scholar_ids[] = $member->scholar_id;
}
$requests = AddRequest::where('ship_id','=',$ship_id)->get();
//$requests = $requests->toArray();
//$request_scholar_ids = array_pluck($requests, 'scholar_id');
//$add_exits = Scholar::whereNotIn('scholar_id', $scholar_ids)->get();
foreach($requests as $request){
$scholar_ids[] = $request->scholar_id;
}
$scholars = Scholar::whereNoIn('scholar_id', $scholar_ids)->get();
dd($scholars);
}
I'm using a twig for loop to display a list of elements. These elements come from a decoded json array, from an API.
I have a OneToMany relation between my user and these elements.
User needs to chose one of these elements, which will be added to the user with the addElement() function.
I tried to do so using a Symfony2 form in the loop, but it is only displayed on the first element. I also tried using a link to a controller function, but since none of these elements are persisted in my DB, I got this error:
"Unable to guess how to get a Doctrine instance from the request information."
Here's how I display my elements:
{% block itinerary %}
{% for element in elements %}
<aside class="flights-results__by-price col-md-3">
<span class="flights-results__price">{{ element.price ? element.price : 'Unknown' }}</span>
Delete
</aside>
{% endfor %}
{% endblock itinerary %}
Here is the function where I create and fill my elements :
public function getAvailabilities($availabilities, $planes, $airports)
{
$reservations = array();
foreach ($availabilities as $ar)
{
$leg = new Leg();
$leg->getId();
foreach($ar as $a)
{
$leg = $this->fillLeg($leg, $a);
foreach($a->availabilities as $aleg)
{
$leg->setAirplaneType($this->findPlane($planes, $aleg->airplane_type_id));
$leg->setAirportStart($this->findAirport($airports, $a->lfi_from));
$leg->setAirportEnd($this->findAirport($airports, $a->lfi_to));
$leg->setDurationLeg($aleg->duration);
$leg->setEndHour($aleg->datetime_to);
}
$startdate = $a->datetime;
}
$reservations[] = $leg;
}
return $reservations;
}
and here is the result when I dump($elements) :
FlightController.php on line 55:
array:4 [▼
0 => {#953 ▼
+"3e1f975601f59090decc8f2d5ced72010162e48e": {#954 ▼
+"lfi_from": "FR58957"
+"lfi_to": "FR45300"
+"datetime": "2015-09-10 20:00:00"
+"nb_pax": "4"
+"availabilities": array:1 [▼
0 => {#955 ▶}
]
}
}
1 => {#956 ▼
+"3e1f975601f59090decc8f2d5ced72010162e48e": {#957 ▼
+"lfi_from": "FR45300"
+"lfi_to": "AG00060"
+"datetime": "2015-09-10 23:00:00"
+"nb_pax": "4"
+"availabilities": array:1 [▼
0 => {#958 ▶}
]
}
}
2 => {#959 ▼
+"3e1f975601f59090decc8f2d5ced72010162e48e": {#960 ▼
+"lfi_from": "FR45300"
+"lfi_to": "AG00060"
+"datetime": "2015-11-30 23:00:00"
+"nb_pax": "4"
+"availabilities": array:1 [▼
0 => {#961 ▶}
]
}
}
3 => {#962 ▼
+"3e1f975601f59090decc8f2d5ced72010162e48e": {#963 ▼
+"lfi_from": "FR45300"
+"lfi_to": "OLOLOL"
+"datetime": "2015-09-18 23:00:00"
+"nb_pax": "2"
+"availabilities": array:1 [▼
0 => {#964 ▶}
]
}
}
]
The main problem is that the API returns several thousands results. For obvious reasons, I cannot persist them all.
I guess the easiest way to ask would be "What is the best way to send datas on an entity to another function in my controller, without persisting this entity?". So far, I've always worked with persisted elements, with an id as identifier, but I realize it gets trickier when we deal with non-persisted entities.
If you have a OneToMany relation between your User and these Elements, it means the Elements are persisted somehow. So why can't you use the id of the element ?
In case you persist it, you may need to add a ParamConverter in your controller code somewhere along those lines:
/**
* #Route("/selectLeg/{element}")
* #ParamConverter("element", class="YourBundle:Element", options={"mapping": {"name": "element.whatever_param"}})
* #Template()
*/
public function selectLegAction(Element $element)
The fact is, if Symfony2 doesn't know about your Element entity, you won't be able to do addElement() to your User.
What I guess is that you get the elements' list in the frontend and then try to update your User object. In this case I would json_encode your element in Twig (it's a simple array after all if I understand) :
Select this leg
and create a new Element in your controller :
/**
* #Route("/selectLeg/{legAsJSONString}")
*/
public function selectLegAction($legAsJSONString) {
$e = json_decode($legAsJSONString);
$leg = new Leg();
$leg->setWhateverParameter($e->parameter_in_the_array);
// more parameters here
$em->persist($leg)->flush();
/// Now here you have $leg->getId(); if ever you need it
}
EDIT : Adapted to your comment. If you don't need to persist the element (leg) before the user has chosen a particular one, then send the element in string form in a GET parameter, in the route parameters, or in the data of a POST request (cleaner solution). You don't need the id since you can pass the full object in the request, as a JSON string.
Remove your useless $leg->getId(); from getAvailabilities() as well, it doesn't actually do anything, and the id doesn't exist anyway.
If I miss the point and that the $leg object is too complicated and therefore not serializable in JSON, then you will need to persist it since two consequent requests will need to have access to it.