/** * 设置 Model 类型对象 * * @param Model $model */ public function setModelObject(Model $model) { $config = $model->getConnection(); self::$table = $config['tablename']; $this->setConfig($config); self::$model = $model; }
/** * Filters a query object * * @param Query $query * @param Eloquent $model * * @return void */ public function filterQuery(&$query, $model) { //if the field is if ($this->value !== '') { $query->where($model->table() . '.' . $this->field, '=', $this->value); } }
/** * Constrains a query by a given set of constraints * * @param Query $query * @param Eloquent $model * @param array $constraints * * @return void */ public function constrainQuery(&$query, $model, $constraints) { //if the column hasn't been joined yet, join it if (!Column::isJoined($query, $this->table)) { $query->join($this->table, $model->table() . '.' . $model::$key, '=', $this->column2); } $query->where($this->column, '=', $constraints); }
/** * Adds selects to a query * * @param Query $query * @param array $selects * @param Eloquent $model * * @return void */ public function filterQuery(&$query, &$selects, $model) { //add the select statement if ($this->select) { //if this is a related field, we have to set up a fancy select because of issues with grouping if ($this->isRelated) { $where = ''; $fieldTable = $this->relationshipField->table; switch ($this->relationshipField->type) { case 'belongs_to': $fieldTable = $this->field . '_' . $this->relationshipField->table; $where = $model->table() . '.' . $this->relationshipField->foreignKey . ' = ' . $fieldTable . '.' . $this->relationshipField->column; break; case 'has_one': case 'has_many': $where = $model->table() . '.' . $model::$key . ' = ' . $fieldTable . '.' . $this->relationshipField->column; break; case 'has_many_and_belongs_to': $where = $model->table() . '.' . $model::$key . ' = ' . $this->relationshipField->column; break; } $selects[] = DB::raw("(SELECT " . $this->select . "\n\t\t\t\t\t\t\t\t\t\tFROM " . $this->relationshipField->table . " AS " . $fieldTable . "\n\t\t\t\t\t\t\t\t\t\tWHERE " . $where . ") AS " . $this->field); } else { $selects[] = DB::raw($this->select . ' AS ' . $this->field); } } }
/** * Get the table name * * @return string */ public function table() { $table = parent::table(); $table = $this->prefix ? $this->prefix . $table : $table; return $table; }