public function testWhereClausesCanBeRemoved()
 {
     // For this test it doesn't matter what type of relationship we have, so we'll just use HasOne
     $builder = new EloquentRelationResetStub();
     $parent = m::mock('Illuminate\\Database\\Eloquent\\Model');
     $parent->shouldReceive('getKey')->andReturn(1);
     $relation = new HasOne($builder, $parent, 'foreign_key');
     $relation->where('foo', '=', 'bar');
     list($wheres, $bindings) = $relation->getAndResetWheres();
     $this->assertEquals('bar', $bindings[0]);
     $this->assertEquals('Basic', $wheres[0]['type']);
     $this->assertEquals('foo', $wheres[0]['column']);
     $this->assertEquals('bar', $wheres[0]['value']);
 }
示例#2
0
 /**
  * Attach a model instance to the parent model.
  *
  * This adds the field_id value
  *
  * @param  \Illuminate\Database\Eloquent\Model  $model
  * @return \Illuminate\Database\Eloquent\Model
  */
 public function save(Model $model)
 {
     if ($this->fieldId) {
         $model->setAttribute($this->fieldKey, $this->fieldId);
     }
     return parent::save($model);
 }
示例#3
0
 /**
  * Get the results of the relationship.
  * @return mixed
  */
 public function getResults()
 {
     // New models have no possibility of having a relationship here
     // so prevent the first orphaned relation from being used.
     if (!$this->parent->exists) {
         return null;
     }
     return parent::getResults();
 }
示例#4
0
 /**
  * Add an object through a HasOne relation
  *
  * @param \Illuminate\Database\Eloquent\Relations\HasOne $relation
  * @param \Illuminate\Database\Eloquent\Model $model
  * @return \Illuminate\Database\Eloquent\Model
  */
 protected function addHasOneAssociatedObject(HasOne $relation, \Illuminate\Database\Eloquent\Model $model)
 {
     return $relation->save($model);
 }
示例#5
0
文件: HasOne.php 项目: guratr/cruddy
 /**
  * @param Model $model
  * @param Model $parent
  */
 public function attach(Model $model, Model $parent)
 {
     parent::attach($model, $parent);
     $model->setAttribute($this->relation->getPlainForeignKey(), $parent->getKey());
 }
示例#6
0
 /**
  * Add select with alias to query from "has one" relation
  *
  * @param Builder $query
  * @param HasOne  $relatedModel
  */
 private function addHasOneSelect(Builder $query, HasOne $relatedModel)
 {
     $query->getQuery()->join($relatedModel->getRelated()->getTable(), $relatedModel->getRelated()->getTable() . '.' . $relatedModel->getRelated()->getKeyName(), '=', $relatedModel->getQualifiedParentKeyName());
 }
示例#7
0
 /**
  * Create a new has many relationship instance.
  * @return void
  */
 public function __construct(Builder $query, Model $parent, $foreignKey, $localKey, $relationName = null)
 {
     $this->relationName = $relationName;
     parent::__construct($query, $parent, $foreignKey, $localKey);
 }
示例#8
0
 protected function applyHasOne(Query $query, Model $model, HasOne $hasOne, $name)
 {
     if (isset($this->joinClasses[$name])) {
         return;
     }
     $modelTable = $model->getTable();
     $related = $hasOne->getRelated();
     $relatedTable = $related->getTable();
     $foreignKey = $hasOne->getPlainForeignKey();
     $qualifiedLocalKey = $hasOne->getQualifiedParentKeyName();
     list($parentTable, $localKey) = explode('.', $qualifiedLocalKey);
     if ($this->parser->isRelatedKey($name)) {
         list($parentPath, $key) = $this->parser->toJoinAndKey($name);
         $this->addJoinOnce($query, $this->model, $parentPath);
         $qualifiedLocalKey = $this->joinAliases[$parentPath] . ".{$localKey}";
     } else {
         $qualifiedLocalKey = $hasOne->getQualifiedParentKeyName();
     }
     $alias = $this->joinNameToAlias($name);
     $joinMethod = $this->getJoinMethod($name);
     $query->{$joinMethod}("{$relatedTable} AS {$alias}", "{$qualifiedLocalKey}", '=', "{$alias}.{$foreignKey}");
     $query->distinct();
     $this->joinClasses[$name] = $hasOne->getRelated();
     $this->joinTable[$name] = $relatedTable;
     $this->joinAliases[$name] = $alias;
 }