Пример #1
0
 /**
  * Removes a relationship between current item and the passed one
  *
  * @param string   $relation Name of the relationship
  * @param \PHPixie\ORM\Model    $model    ORM item to remove relationship with. Can be omitted for 'belongs_to' relationships
  * @return void
  * @throws \Exception If realtionship is not defined
  * @throws \Exception If current item is not in the database yet (isn't considered loaded())
  * @throws \Exception If passed item is not in the database yet (isn't considered loaded())
  */
 public function remove($relation, $model = null)
 {
     if (!$this->loaded()) {
         throw new \Exception("Model must be loaded before you try removing relationships from it.");
     }
     $rels = array_merge($this->has_one, $this->has_many, $this->belongs_to);
     $rel = $this->pixie->arr($rels, $relation, false);
     if (!$rel) {
         throw new \Exception("Model doesn't have a '{$relation}' relation defined");
     }
     if ($rel['type'] != 'belongs_to' && (!$model || !$model->loaded())) {
         throw new \Exception("Model must be loaded before being removed from a has_one or has_many relationship.");
     }
     if ($rel['type'] == 'belongs_to') {
         $key = $rel['key'];
         $this->{$key} = null;
         $this->save();
     } elseif (isset($rel['through'])) {
         $this->conn->query('delete')->table($rel['through'])->where(array(array($rel['key'], $this->_row[$this->id_field]), array($rel['foreign_key'], $model->_row[$model->id_field])))->execute();
     } else {
         $key = $rel['key'];
         $model->{$key} = null;
         $model->save();
     }
     $this->cached = array();
 }
Пример #2
0
 /**
  * @param \App\Model\Newsletter|\PHPixie\ORM\Model $newsletter
  */
 protected function onSuccessfulEdit(Model $newsletter)
 {
     if ($this->request->post('send')) {
         $this->pixie->db->query('delete')->table('tbl_newsletter_instances')->where('newsletter_id', $newsletter->id())->execute();
         $sql = "INSERT INTO tbl_newsletter_instances (newsletter_id, subscriber_id)\n" . "SELECT " . $newsletter->id() . ", ns.id\n" . "FROM tbl_newsletter_signups ns";
         if (!$newsletter->send_to_all) {
             $subscriberIds = preg_split('/\\s*,\\s*/', $newsletter->subscriber_ids, -1, PREG_SPLIT_NO_EMPTY);
             if (!$subscriberIds) {
                 $newsletter->save();
                 return;
             }
             $sql .= "\nWHERE ns.id IN (" . implode(', ', $subscriberIds) . ")";
         }
         /** @var \PDO $conn */
         $conn = $this->pixie->db->get()->conn;
         $conn->exec($sql);
         $newsletter->recipient_count = $newsletter->instances->count_all();
         $newsletter->status = \App\Model\Newsletter::STATUS_SENDING;
         $newsletter->save();
     }
 }
Пример #3
0
 /**
  * Regenerates users login token
  *
  * @param \PHPixie\ORM\Model $user User model to update
  * @return string Generated token
  */
 public function regenerate_login_token($user)
 {
     $token_field = $this->login_token_field;
     $token = $this->random_string() . ':' . time();
     $user->{$token_field} = $token;
     $user->save();
     return $token;
 }
Пример #4
0
 public function save()
 {
     if ($this->loaded()) {
         $this->preUpdate();
     } else {
         $this->preInsert();
     }
     return parent::save();
 }