public static function __init() { parent::__init(); self::applyFilter('set', function ($self, $params, $chain) { if (isset($params['data']['email_address'])) { $newEmail = strtolower($params['data']['email_address']); $params['data']['email_address'] = $newEmail; #die; } return $chain->next($self, $params, $chain); }); self::applyFilter('validates', function ($self, $params, $chain) { $chainValid = $chain->next($self, $params, $chain); $valid = true; $conditions = array('email_address' => $params['entity']->email_address); if ($params['entity']->exists()) { $conditions['_id'] = array('$ne' => $params['entity']->_id); } $othersWithSameEmail = $self::count($conditions); if ($othersWithSameEmail > 0) { $valid = false; $params['entity']->errors('email_address', 'This address is already taken.'); } return $valid and $chainValid; }); }
public static function __init() { parent::__init(); // Ensure that exactly two teams are set self::applyFilter('validates', function ($self, $params, $chain) { $chainValid = $chain->next($self, $params, $chain); $valid = true; if (!isset($params['entity']->teams) or count($params['entity']->teams) !== 2) { $valid = false; $params['entity']->errors('teams', 'Each game must have exactly two teams.'); } return $valid and $chainValid; }); // Ensure that all teams are in the league self::applyFilter('validates', function ($self, $params, $chain) { $chainValid = $chain->next($self, $params, $chain); $valid = true; if (isset($params['entity']->teams)) { foreach ($params['entity']->teams as $team_id) { $team = Teams::find((string) $team_id); if (!isset($team) or $team->league_id != $params['entity']->league_id) { $valid = false; $params['entity']->errors('teams', 'Both teams must be members of this league.'); break; } } } return $valid and $chainValid; }); // self::applyFilter('save', function($self, $params, $chain) { // var_dump($params['entity']); die; // }); }
public static function __init() { parent::__init(); self::applyFilter('save', function ($self, $params, $chain) { if (isset($params['data']['registration_open'])) { $oldDate = strtotime($params['data']['registration_open']); $params['data']['registration_open'] = mktime(12, 0, 0, date('n', $oldDate), date('j', $oldDate), date('Y', $oldDate)); } if (isset($params['data']['registration_close'])) { $oldDate = strtotime($params['data']['registration_close']); $params['data']['registration_close'] = mktime(23, 59, 59, date('n', $oldDate), date('j', $oldDate), date('Y', $oldDate)); } return $chain->next($self, $params, $chain); }); }
/** * processing supllying custom query made * @param array $options * @return list of object */ public static function processQuery($options = array()) { $className = self::alias(); $qb = Model::getEntity()->createQueryBuilder(); $selected = array(); if (isset($options['where'])) { if (isset($options['where']['and'])) { $and = $qb->expr()->andx(); foreach ($options['where']['and'] as $rule) { $and->add(self::addRule($qb, $rule)); } } if (isset($options['where']['or'])) { $or = $qb->expr()->orx(); foreach ($options['where']['or'] as $rule) { $or->add(self::addRule($qb, $rule)); } } } if (isset($options['leftJoin'])) { foreach ($options['leftJoin'] as $join) { $selected[] = self::alias($join['field']); } } if (isset($options['innerJoin'])) { foreach ($options['innerJoin'] as $join) { $selected[] = self::alias($join['field']); } } $qb->select($className . (!empty($selected) ? ", " : "") . implode(", ", $selected) . (isset($options['groupBy']['select']) ? ", {$options['groupBy']['select']}" : ""))->from(get_called_class(), $className); $and = self::addJoin($qb, $options, $className, isset($and) ? $and : null); if (isset($and)) { $qb->where($and); } if (isset($or)) { $qb->orWhere($or); } if (isset($options['orderBy'])) { $orders = array(); foreach ($options['orderBy']['fields'] as $field) { $orders[] = "{$className}.{$field}"; } $qb->addOrderBy(implode(",", $orders), isset($options['orderBy']['direction']) ? $options['orderBy']['direction'] : null); } if (isset($options['offset'])) { $qb->setFirstResult($options['offset']); } if (isset($options['limit'])) { $qb->setMaxResults($options['limit']); } if (isset($options['groupBy'])) { foreach ($options['groupBy']['fields'] as $by) { $qb->addGroupBy($by); } } return $qb; }