2 namespace Auction\Entity;
4 use Doctrine\ORM\Mapping as ORM;
10 * @ORM\HasLifecycleCallbacks
11 * @ORM\Table(name="auction_open_day")
17 * Can archive records.
19 const ARCHIVE = false;
21 * Pull Synchronization Strategy for this table.
23 const PULL_SYNCH_STRATEGY = false;
25 * Push Synchronization Strategy for this table.
27 const PUSH_SYNCH_STRATEGY = false;
31 * @ORM\Column(type="integer");
32 * @ORM\GeneratedValue(strategy="AUTO")
37 * @ORM\Column(type="date", name="open_date");
42 * @ORM\OneToMany(targetEntity="OpenDayGroup", mappedBy="openDay", cascade={"all"}, fetch="EXTRA_LAZY")
47 * @ORM\Column(type="datetime");
52 * @ORM\Column(type="datetime", nullable=true);
59 * Initialize collections.
61 public function __construct()
63 $this->groups = new \Doctrine\Common\Collections\ArrayCollection();
67 * Magic getter to expose protected properties.
68 * @param string $property
71 public function __get($property)
73 return $this->$property;
77 * Magic setter to save protected properties.
78 * @param string $property
81 public function __set($property, $value)
83 $this->$property = $value;
89 public function setCreateTime()
91 $this->created = new \DateTime("now");
97 public function setUpdateTime()
99 $this->updated = new \DateTime("now");
103 * Convert the object to an array.
104 * @param array $expand
105 * @param array $intersect
108 public function toArray(
109 array $expand = array('attachment'), array $intersect = array(),
110 $showIdentifiers = false, $expandAll = 0
113 $dateTimeFormat = \Utility\Registry::getConfigParam('DateTimeFormat');
114 $dateFormat = \Utility\Registry::getConfigParam('DateFormat');
115 $includeAll = empty($intersect);
117 ($includeAll || isset($intersect['id']))
118 && $data['id'] = $this->id;
119 ($includeAll || isset($intersect['openDate']))
120 && $data['openDate'] = !is_null($this->openDate)
121 ? $this->openDate->format($dateFormat)
123 ($includeAll || isset($intersect['groups']))
124 && $data['groups'] = (in_array('groups', $expand) || $expandAll || $showIdentifiers)
125 && !is_null($this->groups)
126 ? $this->groupsToArray($showIdentifiers)
128 ($includeAll || isset($intersect['created']))
129 && $data['created'] = !is_null($this->created)
130 ? $this->created->format($dateTimeFormat)
132 ($includeAll || isset($intersect['updated']))
133 && $data['updated'] = !is_null($this->updated)
134 ? $this->updated->format($dateTimeFormat)
140 * Internal utility to change auction groups collection into array.
141 * @param boolean $showIdentifiers
144 protected function groupsToArray($showIdentifiers = false)
147 $iterator = $this->groups->getIterator();
148 foreach ($iterator as $group)
150 $data[] = array('id' => $group->companyGroup->id);
156 * Populate from an array.
159 public function fromArray($data = array())
162 && $this->id = $data['id'];
163 isset($data['openDate'])
164 && $this->openDate = is_object($data['openDate'])
166 : new \DateTime($data['openDate']);
167 if (isset($data['groups']))
169 $em = \Utility\Registry::getEntityManager();
171 foreach ($data['groups'] as $grp)
173 $newGrps[$grp['id']] = $grp['id'];
175 $currentGrps = array();
176 $iterator = $this->groups->getIterator();
177 foreach ($iterator as $grp)
179 $currentGrps[$grp->companyGroup->id] = $grp->companyGroup->id;
180 !isset($newGrps[$grp->companyGroup->id])
181 && $em->remove($grp);
183 foreach ($data['groups'] as $grp)
185 if (!isset($currentGrps[$grp['id']]))
187 $openDayGrp = new \Auction\Entity\OpenDayGroup();
188 $openDayGrp->companyGroup = $em->getReference('\Company\Entity\Group', $grp['id']);
189 $openDayGrp->openDay = $this;
190 $this->groups->add($openDayGrp);