Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

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.

share|improve this question
    
can you post the difference between goal and What I get: snippets – J Santosh Sep 5 at 8:21
    
The differences lie in the " public 'date' => string '...' (length=19)" rows. In short, all previous values in the array get overwritten with the latest assigned value. – Filip Juncu Sep 5 at 8:32
    
may you missed some logic. You are updating entire array while adding new object to it – J Santosh Sep 5 at 8:39
    
@J Santosh The core of the problem is in the fetch_instances() function. The line responsible for getting the next date is: $this->instances[ $this->count + 1 ] = $this->next_instance( $this->instances[ $this->count ] ); – Filip Juncu Sep 5 at 8:40
1  
i am not good in PHP, but i can say the logic, every time you add a new object to array you have initialize, update and add. i think you are just doing update and add. i experienced this problem in .net. so suggested. – J Santosh Sep 5 at 8:45

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.