Skip to content

Commit

Permalink
New NWQuery class to get rid of eloquent.
Browse files Browse the repository at this point in the history
  • Loading branch information
tchalvak committed Aug 28, 2023
1 parent 11435b3 commit 118d504
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 3 deletions.
7 changes: 4 additions & 3 deletions deploy/lib/data/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
// use Illuminate\Database\Eloquent\Model;
use NinjaWars\core\data\Player;

class Message extends \Illuminate\Database\Eloquent\Model
class Message extends NWQuery
{
protected $primaryKey = 'message_id'; // Anything other than id
static protected $primaryKey = 'message_id'; // if anything other than id
static protected $table = 'messages';
public $timestamps = false;
// The non-mass-fillable fields
protected $guarded = ['message_id', 'date'];
/**
Currently:
Messages Currently:
message_id | serial
message | text
date | timestamp
Expand Down
91 changes: 91 additions & 0 deletions deploy/lib/data/NWQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace NinjaWars\core\data;

use NinjaWars\core\data\DatabaseConnection;
// use Illuminate\Database\Eloquent\Model;
use NinjaWars\core\data\Player;
use Carbon\Carbon;

/**
* Acts as a mini model and query builder with an ActiveRecord pattern
* (e.g. Message::find(id) $message->save(), whatever nw needs)
*/
abstract class NWQuery
{

private static $model;
// Inheriting classes need to set primaryKey and table as:
// protected $primaryKey;
// protected $table = 'messages';


public static function freshTimestamp()
{
// use Carbon::now() to get a current timestamp
return Carbon::now();
}

public static function getTable()
{
return static::$table;
}

public static function getPrimaryKey()
{
return static::$primaryKey;
}

public static function creating($model)
{
// initialize the model if it hasn't been already
if (!self::$model) {
// initialize as a stdClass object

self::$model = new static();
self::$model->table = static::$table;
self::$model->primaryKey = static::$primaryKey;
}
self::$model->date = self::freshTimestamp();
return self::$model;
}

public static function create($model)
{
$model_f = self::creating($model);
$model_f->date = self::freshTimestamp();
if (!$model_f->table) {
throw new \Exception('Error: Model created does not have a table set.');
}
return $model_f;
}

/**
* @return array of items
*/
public static function query($builder)
{
// Destructure the builder into query and parameters
list($query, $params) = $builder;
$datas = query_array($query, $params);
// Meld the incoming data array of multiple entries with the current model
$collected = array_map(function ($data) {
return (object) array_merge((array) self::$model, $data);
}, $datas);
return $collected;
}

/**
* @return object A single model object
*/
public static function find($id)
{

$found_data = reset(self::query(['select * from ' . static::$table . ' where ' . static::$primaryKey . ' = :id', [':id' => $id]]));
$model = new static();
foreach ($found_data as $key => $value) {
$model->$key = $value;
}
return $model;
}
}

0 comments on commit 118d504

Please sign in to comment.