Beispiel #1
0
 public function __construct($type, array $settings)
 {
     $this->fill($settings);
     $this->type = $type;
     $table = $this->refTable;
     if ($this->isForeignService && $this->refServiceId) {
         $table = Service::getCachedNameById($this->refServiceId) . '.' . $table;
     }
     switch ($this->type) {
         case static::BELONGS_TO:
             $this->name = $table . '_by_' . $this->field;
             break;
         case static::HAS_MANY:
             $this->name = $table . '_by_' . $this->refFields;
             break;
         case static::MANY_MANY:
             $junction = $this->junctionTable;
             if ($this->isForeignJunctionService && $this->junctionServiceId) {
                 $junction = Service::getCachedNameById($this->junctionServiceId) . '.' . $junction;
             }
             $this->name = $table . '_by_' . $junction;
             break;
         default:
             break;
     }
 }
Beispiel #2
0
 /**
  * @param string         $table_name
  * @param string | array $field_names
  * @param string | array $select
  *
  * @throws \InvalidArgumentException
  * @return array
  */
 public function getSchemaExtrasForFieldsReferenced($table_name, $field_names = '*', $select = '*')
 {
     if (empty($field_names)) {
         return [];
     }
     if ('*' === $field_names) {
         $result = DbFieldExtras::whereRefServiceId($this->getServiceId())->whereRefTable($table_name)->get()->toArray();
     } else {
         if (false === ($values = DbUtilities::validateAsArray($field_names, ',', true))) {
             throw new \InvalidArgumentException('Invalid field list. ' . $field_names);
         }
         $result = DbFieldExtras::whereRefServiceId($this->getServiceId())->whereRefTable($table_name)->whereIn('ref_fields', $values)->get()->toArray();
     }
     foreach ($result as &$extra) {
         if (!empty($extra['ref_service_id']) && $extra['ref_service_id'] != $extra['service_id']) {
             $extra['service'] = Service::getCachedNameById($extra['service_id']);
         }
     }
     return $result;
 }