简体   繁体   中英

Laravel eloquent wrapper for maria-db dynamic columns

As far as i know which MariaDb has a Dynamic columns and data of this columns in database should be blob .

The dynamic columns are stored inside a real blob column

in MariaDb sql commands we can use CREATE_COLUMN to create some column and GET_COLUMN to get some column from blob

for example:

inserting data:

INSERT INTO bird_sightings 
(human_id, time_seen, observations) 
VALUES
  (27, NOW(),
   COLUMN_CREATE('wing-shape','pointed', 
  'bill-shape','all-purpose', 'main-color','gray') );

get query from database:

SELECT name_first AS 'Birder', 
DATE_FORMAT(time_seen, '%b %d') AS 'Date',
COLUMN_GET(observations, 'wing-shape' AS CHAR) AS 'Wings',
COLUMN_GET(observations, 'wingspan' AS INT) AS 'Span (cm)',
COLUMN_GET(observations, 'bill-shape' AS CHAR) AS 'Beak'
FROM bird_sightings
JOIN humans USING(human_id);

and now what i want to know? is any Eloquent wrapper for this structure, using like ORM with them? or how can i use with this structure in Laravel

Without having tested this myself I think you can try adding the following in your model:

public function newEloquentBuilder($query) {
      $query->addSelect(DB::raw("COLUMN_GET(observations, 'wing-shape' AS CHAR)"));
      $query->addSelect(DB::raw("COLUMN_GET(observations, 'wingspan' AS CHAR)"));
      $query->addSelect(DB::raw("COLUMN_GET(observations, 'bill-shape' AS CHAR)"));
      return parent::newEloquentBuilder($query);    
}

If you want to ad-hoc get them for a particular query only you can just add the above selects in the query builder eg

$sighting = BirdSighting::selectRaw("name_first AS 'Birder', 
DATE_FORMAT(time_seen, '%b %d') AS 'Date',
COLUMN_GET(observations, 'wing-shape' AS CHAR) AS 'Wings',
COLUMN_GET(observations, 'wingspan' AS INT) AS 'Span (cm)',
COLUMN_GET(observations, 'bill-shape' AS CHAR) AS 'Beak'")->get()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM