Code Monkey home page Code Monkey logo

kapitanluffy / propel-model-parser-bundle Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 164 KB

The PropelModelParserBundle enables Propel BaseObjects to be parsed into array, which in turn can be converted into JSON for API responses. Unlike native BaseObject methods like toJSON(), the PropelModelParserBundle can automatically convert BaseObjects as well as their "child objects" to array, as well as add custom properties.

PHP 99.68% HTML 0.32%
propel-models php propel orm

propel-model-parser-bundle's Introduction

Propel Model Parser Bundle

The PropelModelParserBundle enables Propel BaseObjects to be parsed into array, which in turn can be converted into JSON for API responses.

Unlike native BaseObject methods like toJSON(), the PropelModelParserBundle can automatically convert BaseObjects as well as their "child objects" to array, as well as add custom properties.

Installation:

  1. Install PropelModelParserBundle using Composer
  2. Register bundle in AppKernel
  3. In your Propel schema.xml, Use kapitanluffy\PropelModelParserBundle\BaseModel as your baseClass
  4. Rebuild Propel models.

Usage:

In your controller..

<?php
namespace AppBundle\Controller;

use kapitanluffy\PropelModelParserBundle\PropertyCollection;
use Symfony\Component\HttpFoundation\;

class DefaultController extends Controller
{
    public function indexAction($user_id)
    {
        $user = Model\UserQuery::create()
        ->joinWith('Post')
        ->findOneById($user_id);
        
        $post_count = $user->getPosts()->count();
        
        $properties = new PropertyCollection;
        $properties->addProperty('posts', 'getPosts')
            // parse child object's (post) children (user)
            ->useProperty('posts')
                ->addProperty('poster', 'getUser')
            ->endUse()
            // add custom property
            ->addProperty('post_count', $post_count);
        
        $data = $user->parseObject($properties);
        
        $response = new JsonResponse;
        $response->setData($data);
        return $response;
    }
}

Will result into

{
    "id":1,
    "username":"user1",
    "password":"pass",
    "email":"[email protected]",
    "posts":[
        {
            "id":1,
            "string":"this is user1\u0027s post",
            "user_id":1,
            "created_at":{
                "date":"2015-06-24 11:28:52.000000",
                "timezone_type":3,
                "timezone":"UTC"
            },
            "updated_at":{
                "date":"2015-06-24 11:28:52.000000",
                "timezone_type":3,
                "timezone":"UTC"
            },
            "poster":{
                "id":1,
                "username":"user1",
                "password":"pass",
                "email":"[email protected]"
            }
        },
        {
            "id":4,
            "string":"another user1 post",
            "user_id":1,
            "created_at":{
                "date":"2015-06-24 11:28:52.000000",
                "timezone_type":3,
                "timezone":"UTC"
            },
            "updated_at":{
                "date":"2015-06-24 11:28:52.000000",
                "timezone_type":3,
                "timezone":"UTC"
            },
            "poster":{
                "id":1,
                "username":"user1",
                "password":"pass",
                "email":"[email protected]"
            }
        }
    ],
    "post_count":2
}

propel-model-parser-bundle's People

Contributors

kapitanluffy avatar

Watchers

 avatar  avatar  avatar

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.