Example #1
0
 public function has($model, $pivot)
 {
     if (is_object($pivot)) {
         if ($pivot instanceof \Dbredis\Db) {
             $pivot = $pivot->table;
         } else {
             $pivot = $pivot->db()->table;
         }
     }
     if (!is_object($model)) {
         throw new Exception('the first argument must be a model.');
     }
     if (!strlen($model->id)) {
         throw new Exception("attach method requires a valid model 1.");
     }
     $names = [(string) $model->db()->table, (string) $pivot];
     asort($names);
     $from = current($names);
     $to = end($names);
     if ($from == $model->db()->table) {
         $count = Model::Pivot()->where(['from_table', '=', (string) $from])->where(['from_id', '=', (int) $model->id])->where(['from_db', '=', (string) $model->db()->db])->where(['to_table', '=', (string) $pivot])->count();
     } else {
         $count = Model::Pivot()->where(['from_table', '=', (string) $pivot])->where(['to_id', '=', (int) $model->id])->where(['to_db', '=', (string) $model->db()->db])->where(['to_table', '=', (string) $to])->count();
     }
     return $count > 0 ? true : false;
 }