Problems to get the child data from entity class (via SESSION)

I just starting with doctrine / symfony and I am facing the following issue:

Background

I have 3 Entities:

  1. Course
  2. Pricing
  3. Discounts

Each Course is connectect to a pricing. A pricing can be used for multiple courses.
Each pricing has different discounts (pending on the date of booking).

What I did:

I added a function into the Course-Entity to get the current price

class Course {
   // Remark: I will skip the doctine annontation in this example
   
   // properties corresponding to database
   private $name
   private $basicprice
   [...]

   // getters and setters for the properties
   [...]

   // getter to related entities
   public function getPricing() {...}

   // additional functions
   public function getCurrentPrice() {
      dump($this->getPricing());
      dump($this->getPricing()->getName());
      dump($this->getPricing());

      $discounts = $this->getPricing()->getDiscounts(); // <-- BIG ERROR

      // +++ do some magic logic +++
      $currentDiscount = magic();
      $currentPrice    = $this->getBasicPrice() - $currentDiscount; 
     
      return $currentPrice;
   }
}

What I want:

The Course-Object is stored in SESSION and shall now be treated

class BookingController extends AbstractController {
   [...]
   
   public function index() {
      $session = new Session();
      $course  = $session->get('course')
      $price   = $course->getCurrentPrice();
      [...]
   }
}

Issue

While I can get the PRICING and the DISCOUNTS with other controllers, I assume that the problem is in the usage of SESSION.

I don’t get the DISCOUNTS (error), but the problem is I guess already on PRICING-Level. See the DUMPs:

Course.php on line 68:
Proxies__CG__AppEntityPricing {#948 ▼
  +__isInitialized__: false
  -id: 456
  -name: null
  -createdAt: null
  -updatedAt: null
  -courses: null
  -discounts: null
   …2
}

Course.php on line 69:
null

Course.php on line 70:
Proxies__CG__AppEntityPricing {#948 ▼
  +__isInitialized__: false
  -id: 456
  -name: null
  -createdAt: null
  -updatedAt: null
  -courses: null
  -discounts: null
   …2
}

Questions

  1. Is that additional function in the Course-Class correctly placed? Or does it need to go into a subclass of Course-Class?
  2. Why do I not get the wanted data already on PRICING-Level?

Many thanks for your ideas!!!

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *