/**
  * Adds a WHERE according with the relation of other entity.
  *
  * @param RowInterface $row
  *
  * @return self
  */
 public function relatedWith(RowInterface $row)
 {
     $entity = $row->getEntity();
     if ($this->entity->hasOne($entity)) {
         return $this->by($entity->foreignKey, $row->get('id'));
     }
     if ($this->entity->hasMany($entity)) {
         return $this->byId($row->get($this->entity->foreignKey));
     }
     $bridge = $this->entity->getBridge($entity);
     if ($bridge) {
         $this->from($bridge->name);
         $this->from($entity->name);
         $this->fields[] = "`{$bridge->name}`.`{$entity->foreignKey}`";
         $this->where("`{$bridge->name}`.`{$this->entity->foreignKey}` = `{$this->entity->name}`.`id`");
         $this->where("`{$bridge->name}`.`{$entity->foreignKey}` = `{$entity->name}`.`id`");
         $this->where("`{$entity->name}`.`id` IN (:{$bridge->name})", [":{$bridge->name}" => $row->get('id')]);
         return $this;
     }
     throw new SimpleCrudException("The tables {$this->entity->name} and {$entity->name} are no related");
 }
예제 #2
0
 /**
  * Relate 'has-one' elements with this row.
  *
  * @param RowInterface $row The row to relate
  *
  * @return $this
  */
 public function relateWith(RowInterface $row)
 {
     if (!$this->entity->hasOne($row->getEntity())) {
         throw new SimpleCrudException('Not valid relation');
     }
     if (empty($row->id)) {
         throw new SimpleCrudException('Rows without id value cannot be related');
     }
     $this->{$row->getEntity()->foreignKey} = $row->id;
     return $this;
 }