2 namespace PriceGuide\Entity;
4 use Doctrine\ORM\Mapping as ORM;
12 * @ORM\HasLifecycleCallbacks
13 * @ORM\Table(name="price_guide")
19 * Can archive records.
23 * Pull Synchronization Strategy for this table.
25 const PULL_SYNCH_STRATEGY = false;
27 * Push Synchronization Strategy for this table.
29 const PUSH_SYNCH_STRATEGY = false;
33 /* ------------------------------------ Identification ------------------------------------ */
36 * @ORM\Column(type="integer");
37 * @ORM\GeneratedValue(strategy="AUTO")
42 /* ------------------------------------ Ownership ------------------------------------ */
44 * @ORM\ManyToOne(targetEntity="\User\Entity\Profile")
45 * @ORM\JoinColumn(nullable=false, name="created_by_profile_id")
50 * @ORM\ManyToOne(targetEntity="\Company\Entity\Company")
51 * @ORM\JoinColumn(nullable=false, name="company_id")
56 * @ORM\ManyToOne(targetEntity="\Valuation\Entity\Valuation")
57 * @ORM\JoinColumn(nullable=true, name="stock_valuation_id")
62 * @ORM\ManyToOne(targetEntity="\Stock\Entity\Stock")
63 * @ORM\JoinColumn(nullable=false, name="stock_id")
68 /* ------------------------------------ Linkage ------------------------------------ */
70 * @ORM\OneToMany(targetEntity="ClubStock", mappedBy="priceGuideStock", cascade={"all"}, fetch="EXTRA_LAZY")
75 * @ORM\OneToMany(targetEntity="MemberStock", mappedBy="priceGuideStock", cascade={"all"}, fetch="EXTRA_LAZY")
80 * @ORM\OneToMany(targetEntity="Offer", mappedBy="priceGuideStock", cascade={"all"}, fetch="EXTRA_LAZY")
85 /* ------------------------------------ Tracking ------------------------------------ */
87 * @ORM\Column(type="string", length=25, nullable=true, name="previous_status");
89 protected $previousState;
92 * @ORM\Column(nullable=true, type="string", length=25, name="status");
94 protected $jobState = 'Open4Offers';
97 * @ORM\Column(type="datetime");
102 * @ORM\Column(type="datetime", nullable=true);
107 * @ORM\Column(type="boolean");
109 protected $archived = false;
114 * Initialize collections.
116 public function __construct()
118 $this->clubs = new \Doctrine\Common\Collections\ArrayCollection();
119 $this->offers = new \Doctrine\Common\Collections\ArrayCollection();
123 * Add a new ClubStock to this Stock Item.
124 * @param Damage $damage
127 public function addClubStock(ClubStock $clubStock)
129 $this->clubs[] = $clubStock;
136 * Magic getter to expose protected properties.
138 * @param string $property
141 public function __get($property)
143 return $this->$property;
147 * Magic setter to save protected properties.
149 * @param string $property
150 * @param mixed $value
152 public function __set($property, $value)
154 $this->$property = $value;
160 public function setCreateTime()
162 $this->company = \Utility\Registry::resolveCompanyContext($this->company);
163 $this->createdBy = \Utility\Registry::resolveProfileContext($this->createdBy);
164 $this->created = new \DateTime("now");
170 public function setUpdateTime()
172 $this->updated = new \DateTime("now");
176 * Convert the object to an array.
177 * @param array $expand
178 * @param array $intersect
179 * @param boolean $showIdentifiers
180 * @param integer $expandAll
183 public function toArray(
184 array $expand = array(), array $intersect = array(),
185 $showIdentifiers = false, $expandAll = 0
188 $intersect = array_flip($intersect);
189 $dateTimeFormat = \Utility\Registry::getConfigParam('DateTimeFormat');
190 $includeAll = empty($intersect);
192 ($includeAll || isset($intersect['id']))
193 && $data['id'] = $this->id;
194 ($includeAll || isset($intersect['company']))
195 && $data['company'] = (in_array('company', $expand) || $expandAll || $showIdentifiers)
196 && !is_null($this->company)
197 ? (!$showIdentifiers ? $this->company->toArray(
198 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
199 ) : $this->company->id)
201 ($includeAll || isset($intersect['stock']))
202 && $data['stock'] = (in_array('stock', $expand) || $expandAll || $showIdentifiers)
203 && !is_null($this->stock)
204 ? (!$showIdentifiers ? $this->stock->toArray(
205 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
206 ) : $this->stock->id)
208 ($includeAll || isset($intersect['clubs']))
209 && $data['clubs'] = (in_array('clubs', $expand) || $expandAll || $showIdentifiers)
210 && !is_null($this->clubs)
211 ? $this->clubsToArray($showIdentifiers)
213 (($includeAll || isset($intersect['clubs'])) && in_array('club-ids', $expand))
214 && $data['clubs'] = (in_array('club-ids', $expand) || $expandAll || $showIdentifiers)
215 && !is_null($this->clubs)
216 ? $this->clubsToArray(true)
218 ($includeAll || isset($intersect['offers']))
219 && $data['offers'] = (in_array('offers', $expand) || $expandAll || $showIdentifiers)
220 && !is_null($this->offers)
221 ? $this->offersToArray($showIdentifiers)
223 ($includeAll || isset($intersect['jobState']))
224 && $data['jobState'] = $this->jobState;
225 ($includeAll || isset($intersect['created']))
226 && $data['created'] = !is_null($this->created)
227 ? $this->created->format($dateTimeFormat)
229 ($includeAll || isset($intersect['updated']))
230 && $data['updated'] = !is_null($this->updated)
231 ? $this->updated->format($dateTimeFormat)
237 * Internal utility to change clubs collection into array.
238 * @param boolean $showIdentifiers
241 protected function clubsToArray($showIdentifiers = false)
244 $iterator = $this->clubs->getIterator();
245 foreach ($iterator as $club)
247 $data[] = $showIdentifiers
248 ? array('id' => $club->id)
255 * Internal utility to change stock offers collection into array.
256 * @param boolean $showIdentifiers
259 protected function offersToArray($showIdentifiers = false)
262 $iterator = $this->offers->getIterator();
263 foreach ($iterator as $offer)
265 $data[] = $showIdentifiers
266 ? array('id' => $offer->id)
273 * Populate from an array.
276 public function fromArray($data = array())
279 && $this->id = $data['id'];
280 isset($data['company'])
281 && $this->company = $data['company'];
282 isset($data['valuation'])
283 && $this->valuation = $data['valuation'];
284 isset($data['stock'])
285 && $this->stock = $data['stock'];
286 if (isset($data['clubs']))
288 $em = \Utility\Registry::getEntityManager();
289 foreach ($data['clubs'] as $cl)
291 if (!is_numeric($cl['id']))
295 $clubStock = new \PriceGuide\Entity\ClubStock();
296 $clubStock->club = $em->getReference('\PriceGuide\Entity\Club', $cl['id']);
297 if (is_null($clubStock->club))
301 $clubStock->priceGuideStock = $this;
302 $em->persist($clubStock);
303 $this->clubs->add($clubStock);