Code Monkey home page Code Monkey logo

Comments (7)

pwhelan avatar pwhelan commented on May 23, 2024

Do you mean scaffolding as in generating a Model, View and Controller?

from datachore.

emilorol avatar emilorol commented on May 23, 2024

Yes. I was thinking of some how using the kind definition to do so, but I can't visualize it. Maybe something like a "super class" or "object" as a helper, I am brainstorming here.

The idea came after trying Ferris 2 (Python).

from datachore.

pwhelan avatar pwhelan commented on May 23, 2024

I have an application that uses Datachore, here: https://github.com/pwhelan/djshouts/tree/php. The models themselves are here: https://github.com/pwhelan/djshouts/tree/php/models.

This is the most complex example I could find (https://github.com/pwhelan/djshouts/blob/php/models/OAuth2/Connection.php):

<?php

namespace Djshouts\OAuth2;
use Datachore\Type;
use Datachore\Model;


class Connection extends Model
{
    protected $properties = [
        /** User reference **/
        'user'      => Type::Key,
        /** Service Reference **/
        'service'   => Type::Key,
        /** External ID **/
        'xid'       => Type::String,
        /** Name **/
        'name'      => Type::String,
        /** Type (as a string) **/
        'type'      => Type::String,
        /** Is Hidden ? **/
        'is_hidden' => Type::Boolean,
        /** Icon URL **/
        'icon_url'  => Type::String,
        /** Precedence: Magic value for  **/
        'precedence'    => Type::Integer,
        /** Page Token **/
        'token'     => Type::String
    ];
}

Connection::user points to the User model, while service points to the OAuth\Service model. At the moment they are not explicit (which at best would just check you set the right kind of relation).

This one uses the define method to set the kind in a set (https://github.com/pwhelan/djshouts/blob/php/models/Show.php):

<?php
namespace Djshouts;
use Datachore\Type;
use Datachore\Model;
class Show extends Model
{
    protected $properties = [
        /** User reference **/
        'user'      => Type::Key,
        /** DJ **/
        //'dj'      => Type::Key,
        /** Recording **/
        //'recording'   => Type::Key,
        /** Image **/
        'image'     => Type::Key,
        /** Name **/
        'title'     => Type::String,
        /** Type (as a string) **/
        'description'   => Type::String,
        /** URL **/
        'url'       => Type::String,
        /** Show is Live */
        'is_live'   => Type::Boolean,
        /** Show Source IP. Used to track offline/switch */
        'source_ip' => Type::String,
        /** List of connections */
        'connections'   => Type::Set
    ];

    protected function define()
    {
        $this->properties['connections']->type(Type::Key);
    }
}

from datachore.

pwhelan avatar pwhelan commented on May 23, 2024

There is a bit of documentation to that respect here: https://github.com/pwhelan/datachore#creating-model-files.

from datachore.

emilorol avatar emilorol commented on May 23, 2024

Yes, I was able to define my models and I was able to write and read from the datastore.

The idea I had was to use the model properties to create the CRUD form.

Example:

use Datachore\Type;
use Datachore\Model;

class Connection extends Model
{
    protected $properties = [
        /** Name **/
        'name'      => Type::String,
        /** Is Hidden ? **/
        'is_hidden' => Type::Boolean,
    ];
}

I will use it to generate:

<form>
<input type="text" name="name" value="">
<select name="is_hidden">
<option>Yes</option>
<option>No</option>
</select>
</form>

How do I access the properties of the model to check their type and based on that generate the form?

Could it also be possible to define the type of field on the model definition?

Something like:

use Datachore\Type;
use Datachore\Model;
use Datachore\Form;

class Connection extends Model
{
    protected $properties = [
        /** Name **/
        'name'      => array(Type::String, Form:textfield),
        /** Is Hidden ? **/
        'is_hidden' => array(Type::Boolean, Form:select),
    ];
}

from datachore.

pwhelan avatar pwhelan commented on May 23, 2024

I hadn't thought about actual introspection. I'll gladly add an API for doing that. I'm thinking something along the lines of making the actual Model class iterable (a la foreach which then returns the property name as the key and the value as the value). The value itself would be the Type class.

ie:

<?php

use Datachore\Type;
use Datachore\Model;

class Connection extends Model
{
    protected $properties = [
        /** Name **/
        'name'      => Type::String,
        /** Is Hidden ? **/
        'is_hidden' => Type::Boolean,
    ];
}

$connection = new Connection;
$connection->name = "Here I am";
$connection->is_hidden = false;

foreach($connection as $key => $value)
{
    print "KEY {$key} = \n";
    print_r($value);
}

Which would output something like:

KEY connection =
object(Datachore\Type\String) ...
KEY is_hidden =
object(Datachore\Type\Boolean) ...

For the moment there is no Form library or API, but I wouldn't be averse to adding it. I wouldn't want to tightly couple it so much that it is included directly in the Model itself. Django uses external *Form classes for that.

from datachore.

emilorol avatar emilorol commented on May 23, 2024

I have been thinking more and more about this and I think I have been approaching this issue the wrong way. So, what if instead to reading from the model to generate the form, I read from the form definition to generate the model?

I think that by doing this way, I will have all the flexibility I need as will be able to define the data type right on the field definition.

Example:

$form['title'] = array(
      '#type' => 'textfield', 
      '#title' => t('Subject'), 
      '#default_value' => 'Page title goes here', 
      '#size' => 60, 
      '#maxlength' => 128, 
      '#required' => TRUE,
      '#data_type' => 'String' // <=== Data type definition
    );

Your thoughts?

from datachore.

Related Issues (6)

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.