Saving Eloquent collection with Mongo - php

I have large, single MongoDB collections and I want to add a timestamp to each of its fields. I do it that way:
$automation = Automation::all();
foreach ($automation as $workflowindex => $workflow)
{
foreach ($workflow['workflow'] as $itemindex => $items)
{
foreach ($items['subscribers'] as $index => $item)
{
foreach ($items as $name => $mail)
{
if ($name != 'subscribers') {
if ($mail['delay'] == "0" && $mail['delay_time'] == 'now') {
$automation[$workflowindex]['workflow'][$itemindex]['subscribers'][$index][$name] = round(time()/60)*60;
$automation->save();
}
}
}
}
}
}
However, if I try to save this collection I get an error saying
Indirect modification of overloaded element of App\Automation has no effect
If I use the method
Automation::all()->toArray();
I still cannot save, beause the save method doesn't work on arrays. Is there a better option to access the field in my foreach loops? If no, then how can I save this collection?

Related

Request too heavy

I try to retrieve for each region the number of cities with current events.
So I started doing this:
$regionCities = [];
foreach ($regions as $region) {
$regionCities[$region->getId()] = $cityRepository->getCitiesByRegion($region);
}
dump($regionCities);
$regionCitiesNumber = [];
foreach ($regionCities as $index => $region) {
foreach ($region as $city) {
$regionCitiesNumber[$index] = count($city->getCurrentEvents());
}
}
My dump returns me this:
dump
The problem is, it crashes my script, and I suddenly get a blank page when I dump regionCitiesNumber.
getCurrentEvents is a method of my City entity that will retrieve all current events.
public static function createCurrentEventsCriteria(): Criteria
{
return Criteria::create()
->where(Criteria::expr()->gte('endDate', new DateTime('00:00:00')))
->orderBy(['id' => 'DESC'])
;
}
public function getCurrentEvents(): Collection
{
$criteria = EventRepository::createCurrentEventsCriteria();
return $this->events->matching($criteria);
}
Use error logs or try using "Try Catch" wrapper
$regionCities = [];
foreach ($regions as $region) {
$regionCities[$region->getId()] = $cityRepository->getCitiesByRegion($region);
}
//dump($regionCities);
try {
$regionCitiesNumber = [];
foreach ($regionCities as $index => $region) {
foreach ($region as $city) {
$regionCitiesNumber[$index] = count($city->getCurrentEvents());
}
}
} catch (\Throwable $t) {
dd($t);
}
So I tried:
return $this->events->matching($criteria)->count();
And I dumped on regionCitiesNumber, it gave me a totally wrong result:
For example for the ARA region, I end up with 0 as a result while there are 1895 events distributed over all the departments ...
As a result, most regions result in 0, sometimes 1, sometimes 2 ...
In addition, it takes a long time to load my page, isn't there a softer solution?

how to update an array using a function, php

Hey I have return a function that takes some orders and subtracts them from an array of inventory...however when am running the function it is only updating the inventory within the function and the original inventory doesn't change..I know this is something to do with scope but I don't know how to do it exactly..
How can I do this ? can you help?
function updateInventory ($inv) {
foreach($_SESSION["inventory"] as $bookDetails) {
foreach($_POST['orders'] as $k => $v) {
if($bookDetails['title'] == $k && $v == "hardcover") {
$bookDetails['hc-quantity']=intval($bookDetails['hc-quatinty'])-1;
}
}
}
}
When you assign an array to a variable, it gets a copy of the array, so changes to that variable don't affect the original array. You can use a reference to prevent copying.
function updateInventory ($inv) {
foreach($_SESSION["inventory"] as &$bookDetails) {
foreach($_POST['orders'] as $k => $v) {
if($bookDetails['title'] == $k && $v == "hardcover") {
$bookDetails['hc-quantity']--;
}
}
}
}

php access object within object

I have an object in PHP and I'm using a
foreach ($items as $item)
to iterate through the items. However, $item contains another object called $item, which contains the $type variable whose value I need to access. How can I access the value of $type? What I want to do is:
foreach($items as item){
if($item->item->type == 'this'){
//do this
} elseif ($item->item->type == 'that'){
//do that
}
}
But $item->item->type isn't picking up that value. How can I access it and use it for my function?
have you tired:
foreach($items as item){
if($item->type == 'this'){
//do this
} elseif ($item->type == 'that'){
//do that
}
}
or you can debug your code to find out what is going on:
foreach($items as item){
print_r($item);
}
and then you can see what kind of childs this $item has.

changing object in loop in Datamapper ORM

I'm trying to save a long form in Codeigniter's Datamapper. I'm able to save the form if I pass the value like this
$t->brandName = $this->input->post('brandName');
$t->specialNotes = $this->input->post('specialNotes');
$t->name = $this->input->post('name');
Now if I call save method it works
$t->save();
Since the form is big I tried to add object values in foreach
$a = get_object_vars($t);
foreach ($a['stored'] as $k => $val){
$t->$k = $this->input->post("$k");
}
however if I call the $t->save() it doesn't work.
I'm not sure what $a['stored'] represents, but it's nothing that's default in Datamapper.
Why don't you do it the opposite way, looping through the post keys?
foreach ($_POST as $key => $val)
{
$t->$key = $this->input->post($key);
}
$t->save();
Note: Any columns that don't exist will just be ignored by Datamapper.
I actually wrote a Datamapper extension for this:
class DM_Data {
function assign_postdata($object, $fields = NULL)
{
// You can pass a different field array if you want
if ( ! $fields)
{
$fields = $object->validation;
}
foreach ($fields as $k => $data)
{
$rules = isset($data['rules']) ? $data['rules'] : array();
if ( ! isset($_POST[$k])) continue;
// Cast value to INT, usually for an empty string.
if (in_array('integer', $rules))
{
$object->$k = (integer) $_POST[$k];
}
// Do other manipulation here if desired
else
{
$object->$k = $_POST[$k];
}
}
return $object;
}
}
You can use $t->assign_postdata()->save(), and optionally pass an array of fields to update to the function (in the datamapper validation format). However, I forget why I use that... but I removed some of the custom stuff. This should be useful for you if you are doing this a lot. It definitely saves me time.

PHP: How do i remove an element from a multidemision array?

I have this code to add new elements to a multidimension array:
$this->shopcart[] = array(productID => $productID, items => $items);
So how do i remove an element from this array? I tried this code, but its not working:
public function RemoveItem($item)
{
foreach($this->shopcart as $key)
{
if($key['productID'] == $item)
{
unset($this->shopcart[$key]);
}
}
}
I get this error:
Warning: Illegal offset type in unset in C:\xampplite\htdocs\katrinelund\classes\TillRepository.php on line 50
public function RemoveItem($item)
{
foreach($this->shopcart as $i => $key)
{
if($key['productID'] == $item)
{
unset($this->shopcart[$i]);
break;
}
}
}
That should do the trick.
Update
There is also an alternative way:
if ( false !== $key = array_search($item, $this->shopcart) )
{
unset($this->shopcart[$key];
}
You're not enumerating over indices, but values there, to unset an array index, you have to unset it by index, not by value.
Also, If your array index is actually the productID you can eliminate the loop altogether:
public function RemoveItem($productID)
{
if (isset($this->shopcart[$productID]))
{
unset($this->shopcart[$productID]);
}
}
Your example doesn't show how you are adding items to $this->shopcart, but this may or may not be an option for you depending on the needs of your project. (i.e. not if you need to have seperate instances of the same productid in the cart).

Categories