Giter Site home page Giter Site logo

vanilla's Introduction

vanilla

The vanilla database model and persistence library.

AbstractModel

With vanilla-db you can create an activerecord model that can be stored and retrieved by utilizing the persistence handler built into the vanilla-db package.

class MyModel extends \Vanilla\Models\AbstractModel
{
    /**
     * @var String
     */
    private $someField;
    
    public function getSomeField()
    {
        return $this->someField;
    }
    
    public function setSomeField($someField)
    {
        $this->someField = $someField;
        return $this;
    }
}

You may provide a mapping object if your fields are NOT identical to the table. You may also establish a table name by defining the tableName function:

class MyModel extends \Vanilla\Models\AbstractModel
{
    protected $modelMap = [
        "someField" => "field_test"
    ];
    
    public function tableName()
    {
        return "tbl_temporary";
    }
}

Foreign Keys

One of the advantages to using our simple vanilla-db library is that we traverse the relationships of the models and persist those. For that we need a few tricks in the AbstractModel and PersistSql classes.

class MyModel extends \Vanilla\Models\AbstractModel
{
    private $myRelatedModel = [];
    
    public function getMyRelatedModel()
    {
        return $this->myRelatedModel;
    }
    
    public function setMyRelatedModel($myRelatedModel)
    {
        $this->myRelatedModel = $myRelatedModel;
        return $this;
    }
       
    public function listModels()
    {
        return [
            "MyRelatedModel" => "modelId"
        ];
    }  
}

class MyRelatedModel extends \Vanilla\Models\AbstractModel
{
    public function foreignKeys()
    {
        return [
            "MyModel" => "modelId"
        ];
    }
}

During persistence the foreignKeys method is checked, and if empty, ignored. But if it is not empty it will attempt to record the id of the attached parent model into the foreign key field of the related model. On load, it will attempt to find all related models and apply them to the main model, thus building a traversable relationship of model-linked data.

$myModel = new MyModel();

$myRelatedModel = new MyRelatedModel();

$myModel->setMyRelatedModel([$myRelatedModel]);

$persistenceHandler = \Vanilla\Persist\PersistFactory::getInstance();
$persistenceHandler->save($myModel);

vanilla's People

Contributors

potentbanana avatar sarukie avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

vanilla's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.