Reputation: 362
First of all, I've searched through similar topics and I couldn't find an answer to my issue. So I have an event manager with reoccurring events. I want to store the starting date of every instance of a repeating event up to the ongoing one.
Goal:
array (size=5)
0 =>
object(DateTime)[288]
public 'date' => string '2015-08-11 14:30:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
1 =>
object(DateTime)[288]
public 'date' => string '2015-08-18 14:30:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
2 =>
object(DateTime)[288]
public 'date' => string '2015-08-25 14:30:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
3 =>
object(DateTime)[288]
public 'date' => string '2015-09-01 14:30:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
What I get:
array (size=5)
0 =>
object(DateTime)[288]
public 'date' => string '2015-09-01 14:30:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
1 =>
object(DateTime)[288]
public 'date' => string '2015-09-01 14:30:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
2 =>
object(DateTime)[288]
public 'date' => string '2015-09-01 14:30:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
3 =>
object(DateTime)[288]
public 'date' => string '2015-09-01 14:30:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
In short, all previous values in the array get overwritten by the last assigned value as the code runs through the loop.
Code:
class Model_Event {
private $post_id;
private $count;
private $instances;
public function __construct( $post_id ) {
$this->post_id = $post_id;
$this->count = (int) 0;
$this->instances = array();
$this->fetch_instances();
}
private function fetch_instances() {
// Abort if no starting date defined.
if ( !$this->has_date() )
return;
// Set the original instance.
$this->instances[ $this->count ] = $this->datetime( 'both', 'from', false, true );
// Abort if event not repeating.
if ( !$this->is_repeating() )
return;
while ( !$this->is_stop_reached( $this->instances[ $this->count ], $this->count ) && $this->is_instance_started( $this->instances[ $this->count ] ) ) :
$this->instances[ $this->count + 1 ] = $this->next_instance( $this->instances[ $this->count ] );
$this->count++;
endwhile;
var_dump($this->instances);
return;
}
private function next_instance( DateTime $dateObject ) {
if ( $this->repeat_unit() === 'h' )
return $this->add_hours( $this->repeat_value(), $dateObject );
if ( $this->repeat_unit() === 'd' )
return $this->add_days( $this->repeat_value(), $dateObject );
if ( $this->repeat_unit() === 'w' )
return $this->add_weeks( $this->repeat_value(), $dateObject );
if ( $this->repeat_unit() === 'm' )
return $this->add_months( $this->repeat_value(), $dateObject );
if ( $this->repeat_unit() === 'y' )
return $this->add_years( $this->repeat_value(), $dateObject );
}
/**
* Currently only testing on "repeat every X weeks".
*/
private function add_weeks( $weeks, DateTime $dateObject ) {
return $dateObject->modify('+'.$weeks.' week');
}
...
}
I am trying to fix this for over 5 hours and can't seem to wrap my head around the problem. Thank you.
Upvotes: 0
Views: 139
Reputation: 362
I've found the problem to be in the following line:
$this->instances[ $this->count + 1 ] = $this->next_instance( $this->instances[ $this->count ] );
@J Santosh and this answer made me realize that instead of copying the contents of the object assigned, the new instance points to the older one and so on and thus all previous objects get overwritten. So I had to change this line as follows:
$this->instances[ $this->count + 1 ] = $this->next_instance( clone $this->instances[ $this->count ] );
The clone method prevents the objects from being overwritten and therefore I am now getting the desired output.
Upvotes: 1