Nestor  Beier

Nestor Beier

1622211475

Elementor PODs Tutorial - How to work with Custom Post Types and Custom Fields

Elementor PODs Tutorial - How to work with Custom Post Types and Custom Fields

#elementor #wordpress #web-development

What is GEEK

Buddha Community

Elementor PODs Tutorial - How to work with Custom Post Types and Custom Fields
Rupert  Beatty

Rupert Beatty

1666454701

Fully Customizable & Extensible Action Sheet Controller

XLActionController

By XMARTLABS.

XLActionController is an extensible library to quickly create any custom action sheet controller.

Examples

demo_spotify.gifdemo_twitter.gifdemo_tweetbot.gif
demo_periscope.gifdemo_youtube.gifdemo_skype.gif

The action sheet controllers shown above were entirely created using XLActionController and are included in the Examples. To run the Example project: clone XLActionController repository, open XLActionController workspace and run the Example project.

The code snippet below shows how to present the Tweetbot action sheet controller:

let actionController = TweetbotActionController()

actionController.addAction(Action("View Details", style: .default, handler: { action in
  // do something useful
}))
actionController.addAction(Action("View Retweets", style: .default, handler: { action in
  // do something useful
}))
actionController.addAction(Action("View in Favstar", style: .default, handler: { action in
  // do something useful
}))
actionController.addAction(Action("Translate", style: .default, executeImmediatelyOnTouch: true, handler: { action in
  // do something useful
}))

actionController.addSection(Section())
actionController.addAction(Action("Cancel", style: .cancel, handler:nil))

present(actionController, animated: true, completion: nil)

As you may have noticed, the library usage looks pretty similar to UIAlertController.

Actions' handlers are executed after the alert controller is dismissed from screen. If you want, you can change this passing true to the action's constructor to the argument executeImmediatelyOnTouch.

Behind the scenes XLActionController uses a UICollectionView to display the action sheet.

Usage

First create a custom action sheet view controller by extending from the ActionController generic class. For details on how to create a custom action sheet controller look at the Extensibility section.

For instance, let's suppose we've already created TwitterActionController.

// Instantiate custom action sheet controller
let actionSheet = TwitterActionController()
// set up a header title
actionSheet.headerData = "Accounts"
// Add some actions, note that the first parameter of `Action` initializer is `ActionData`.
actionSheet.addAction(Action(ActionData(title: "Xmartlabs", subtitle: "@xmartlabs", image: UIImage(named: "tw-xmartlabs")!), style: .default, handler: { action in
   // do something useful
}))
actionSheet.addAction(Action(ActionData(title: "Miguel", subtitle: "@remer88", image: UIImage(named: "tw-remer")!), style: .default, handler: { action in
   // do something useful
}))
// present actionSheet like any other view controller
present(actionSheet, animated: true, completion: nil)

As the code above illustrates, there are no relevant differences compared to the UIAlertController API.

The main difference is that XLActionController works with any header data type and not only the standard UIAlertController title and message properties. Similarly XLActionController's Action works with any data Type and not only the title string.

// XLActionController:
xlActionController.headerData = SpotifyHeaderData(title: "The Fast And The Furious Soundtrack Collection", subtitle: "Various Artists", image: UIImage(named: "sp-header-icon")!)

// vs UIAlertController:
uiActionController.title = "The Fast And The Furious Soundtrack Collection" // no way to pass an image
uiActionController.message = "Various Artists"
// XLActionController:
let xlAction = Action(ActionData(title: "Save Full Album", image: UIImage(named: "sp-add-icon")!), style: .default, handler: { action in })
// notice that we are able to pass an image in addition to the title
xlActionController.addAction(xlAction)

// vs UIAlertController:
let uiAction = UIAlertAction(title: "Xmartlabs", style: .default, handler: { action in }))
uiActionController.addAction(uiAction)

This can be accomplished because XLActionController is a generic type.

Another important difference is that XLActionController provides a way to add action sections as illustrated in the code below:

  actionController.addSection(Section())

and also each section has a data property. This property is generic, so that it can hold any type. This data will be used to create this section's header view.

let section = actionController.addSection(Section())
section.data = "String" // assuming section data Type is String

Each section contains a set of actions. We typically use sections to show a header view above a set of actions.

Extensibility

ActionController uses a UICollectionView to show actions and headers on screen. Actions will be rendered as instances of UICollectionViewCell. You can use your own subclass of UICollectionViewCell by specifying it in the action controller declaration. Additionally, ActionController allows you to specify a global header and a section header. Headers are shown as collection view's supplementary views.

The ActionController class is a generic type that works with any cell, header, section header type and its associated data types.

Create your custom action sheet controller

XLActionController provides extension points to specify a whole new look and feel to our custom sheet controller and to tweak present and dismiss animations. Let's see an example:

// As first step we should extend the ActionController generic type
public class PeriscopeActionController: ActionController<PeriscopeCell, String, PeriscopeHeader, String, UICollectionReusableView, Void> {

    // override init in order to customize behavior and animations
    public override init(nibName nibNameOrNil: String? = nil, bundle nibBundleOrNil: Bundle? = nil) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        // customizing behavior and present/dismiss animations
        settings.behavior.hideOnScrollDown = false
        settings.animation.scale = nil
        settings.animation.present.duration = 0.6
        settings.animation.dismiss.duration = 0.5
        settings.animation.dismiss.options = .curveEaseIn
        settings.animation.dismiss.offset = 30

        // providing a specific collection view cell which will be used to display each action, height parameter expects a block that returns the cell height for a particular action.
        cellSpec = .nibFile(nibName: "PeriscopeCell", bundle: Bundle(for: PeriscopeCell.self), height: { _ in 60})
        // providing a specific view that will render each section header.
        sectionHeaderSpec = .cellClass(height: { _ in 5 })
        // providing a specific view that will render the action sheet header. We calculate its height according the text that should be displayed.
        headerSpec = .cellClass(height: { [weak self] (headerData: String) in
            guard let me = self else { return 0 }
            let label = UILabel(frame: CGRect(x: 0, y: 0, width: me.view.frame.width - 40, height: CGFloat.greatestFiniteMagnitude))
            label.numberOfLines = 0
            label.font = .systemFontOfSize(17.0)
            label.text = headerData
            label.sizeToFit()
            return label.frame.size.height + 20
        })

        // once we specify the views, we have to provide three blocks that will be used to set up these views.
        // block used to setup the header. Header view and the header are passed as block parameters
        onConfigureHeader = { [weak self] header, headerData in
            guard let me = self else { return }
            header.label.frame = CGRect(x: 0, y: 0, width: me.view.frame.size.width - 40, height: CGFloat.greatestFiniteMagnitude)
            header.label.text = headerData
            header.label.sizeToFit()
            header.label.center = CGPoint(x: header.frame.size.width  / 2, y: header.frame.size.height / 2)
        }
        // block used to setup the section header
        onConfigureSectionHeader = { sectionHeader, sectionHeaderData in
            sectionHeader.backgroundColor = UIColor(white: 0.95, alpha: 1.0)
        }
        // block used to setup the collection view cell
        onConfigureCellForAction = { [weak self] cell, action, indexPath in
            cell.setup(action.data, detail: nil, image: nil)
            cell.separatorView?.isHidden = indexPath.item == self!.collectionView.numberOfItems(inSection: indexPath.section) - 1
            cell.alpha = action.enabled ? 1.0 : 0.5
            cell.actionTitleLabel?.textColor = action.style == .destructive ? UIColor(red: 210/255.0, green: 77/255.0, blue: 56/255.0, alpha: 1.0) : UIColor(red: 0.28, green: 0.64, blue: 0.76, alpha: 1.0)
        }
    }
}

ActionController type declaration:

public class ActionController<ActionViewType: UICollectionViewCell, ActionDataType, HeaderViewType: UICollectionReusableView, HeaderDataType, SectionHeaderViewType: UICollectionReusableView, SectionHeaderDataType>

When extending ActionController we must specify the following view types ActionViewType, HeaderViewType, SectionHeaderViewType. These types are the cell type used to render an action, the view used to render the action sheet header and the view used to render the section header.

Each view type has its associated data: ActionDataType, HeaderDataType, SectionHeaderDataType respectively.

If your custom action sheet doesn't have a header view we can use UICollectionReusableView as HeaderViewType and Void as HeaderDataType. If it doesn't have a section header view you can use UICollectionReusableView as SectionHeaderViewType and Void as SectionHeaderDataType.

The code below shows how we specify these types for the action controllers provided in the example project:

class PeriscopeActionController: ActionController<PeriscopeCell, String, PeriscopeHeader, String, UICollectionReusableView, Void> { ... } // doesn't need to show a section header
class SpotifyActionController: ActionController<SpotifyCell, ActionData, SpotifyHeaderView, SpotifyHeaderData, UICollectionReusableView, Void> { ... } // doesn't need to show a section header
class TwitterActionController: ActionController<TwitterCell, ActionData, TwitterActionControllerHeader, String, UICollectionReusableView, Void> { ... } // doesn't need to show a section header
class YoutubeActionController: ActionController<YoutubeCell, ActionData, UICollectionReusableView, Void, UICollectionReusableView, Void>

Tweaking default style and animation parameters

By following the previous section steps you should already be able to play with your custom action controller. It happens quite often that we need some other customization such as zooming out the presenting view, changing the status bar color or customizing the default present and dismiss animation.

ActionController class defines the settings property of type ActionSheetControllerSettings to tweak all these.

UICollectionView's behavior

// Indicates if the action controller must be dismissed when the user taps the background view. `true` by default.
settings.behavior.hideOnTap: Bool
// Indicates if the action controller must be dismissed when the user scrolls down the collection view. `true` by default.
settings.behavior.hideOnScrollDown: Bool
// Indicates if the collectionView's scroll is enabled. `false` by default.
settings.behavior.scrollEnabled: Bool
// Controls whether the collection view scroll bounces past the edge of content and back again. `false` by default.
settings.behavior.bounces: Bool
// Indicates if the collection view layout will use UIDynamics to animate its items. `false` by default.
settings.behavior.useDynamics: Bool
// Determines whether the navigation bar is hidden when action controller is being presented. `true` by default
settings.hideCollectionViewBehindCancelView: Bool

UICollectionView Style

// Margins between the collection view and the container view's margins. `0` by default
settings.collectionView.lateralMargin: CGFloat
// Cells height when UIDynamics is used to animate items. `50` by default.
settings.collectionView.cellHeightWhenDynamicsIsUsed: CGFloat

Animation settings

Struct that contains all properties related to presentation & dismissal animations

// Used to scale the presenting view controller when the action controller is being presented. If `nil` is set, then the presenting view controller won't be scaled. `(0.9, 0.9)` by default.
settings.animation.scale: CGSize? = CGSize(width: 0.9, height: 0.9)

Present animation settings

// damping value for the animation block. `1.0` by default.
settings.animation.present.damping: CGFloat
// delay for the animation block. `0.0` by default.
settings.animation.present.delay: TimeInterval
// Indicates the animation duration. `0.7` by default.
settings.animation.present.duration: TimeInterval
// Used as `springVelocity` for the animation block. `0.0` by default.
settings.animation.present.springVelocity: CGFloat
// Present animation options. `UIViewAnimationOptions.curveEaseOut` by default.
settings.animation.present.options: UIViewAnimationOptions

Dismiss animation settings

// damping value for the animation block. `1.0` by default.
settings.animation.dismiss.damping: CGFloat
// Used as delay for the animation block. `0.0` by default.
settings.animation.dismiss.delay: TimeInterval
// animation duration. `0.7` by default.
settings.animation.dismiss.duration: TimeInterval
// springVelocity for the animation block. `0.0` by default
settings.animation.dismiss.springVelocity: CGFloat
// dismiss animation options. `UIViewAnimationOptions.curveEaseIn` by default
settings.animation.dismiss.options: UIViewAnimationOptions

StatusBar Style

// Indicates if the status bar should be visible or hidden when the action controller is visible. Its default value is `true`
settings.statusBar.showStatusBar: Bool
// Determines the style of the device’s status bar when the action controller is visible. `UIStatusBarStyle.LightContent` by default.
settings.statusBar.style: UIStatusBarStyle
// Determines whether the action controller takes over control of status bar appearance from the presenting view controller. `true` by default.
settings.statusBar.modalPresentationCapturesStatusBarAppearance: Bool

Cancel view style

Sometimes we need to show a cancel view below the collection view. This is the case of the SpotifyActionController. These properties have nothing to do with the actions added to an action Controller nor with the actions with .Cancel as style value.

 // Indicates if the cancel view is shown. `false` by default.
settings.cancelView.showCancel: Bool
 // Cancel view's title. "Cancel" by default.
settings.cancelView.title: String?
 // Cancel view's height. `60` by default.
settings.cancelView.height: CGFloat
 // Cancel view's background color. `UIColor.black.withAlphaComponent(0.8)` by default.
settings.cancelView.backgroundColor: UIColor
// Indicates if the collection view is partially hidden by the cancelView when it is pulled down.
settings.cancelView.hideCollectionViewBehindCancelView: Bool

Advanced animations

If tweaking previous settings is not enough to make the animations work like you want, XLActionController allows you to change the present/dismiss animation by overriding some functions.

Presentation

open func presentView(_ presentedView: UIView, presentingView: UIView, animationDuration: Double, completion: ((_ completed: Bool) -> Void)?)

The function above is responsible for making the present animation. It encapsulates how the presentation is performed and invokes onWillPresentView, performCustomPresentationAnimation and onDidPresentView to allow you to change a specific point of the animation.

Typically we don't need to override presentView function because overriding either onWillPresentView, performCustomPresentationAnimation or onDidPresentView is enough.

open func onWillPresentView()

onWillPresentView is called before the animation block starts. Any change here won't be animated. It's intended to set the initial animated properties values.

open func performCustomPresentationAnimation(_ presentedView: UIView, presentingView: UIView)

performCustomPresentationAnimation is called from within the main animation block.

open func onDidPresentView()

After the present animation is completed, presentView calls onDidPresentView from within completion callback.

onWillPresentView, performCustomPresentationAnimation, onDidPresentView won't be invoked if you override presentView implementation.

Dismissal

Dismissal animation can be customized in the same way as presentation animation.

open func dismissView(_ presentedView: UIView, presentingView: UIView, animationDuration: Double, completion: ((_ completed: Bool) -> Void)?)

The function above is responsible for making the dismissal animation. It encapsulates how the dismissal animation is performed and invokes onWillDismissView, performCustomDismissingAnimation and onDidDismissView to allow you to change an specific point of the animation.

Typically we don't need to override dismissView method because overriding either onWillDismissView, performCustomDismissingAnimationoronDidDismissView` is enough.

open func onWillDismissView()

Overrides onWillDismissView to perform any set up before the animation begins.

open func performCustomDismissingAnimation(_ presentedView: UIView, presentingView: UIView)

performCustomDismissingAnimation function is invoked from within the main animation block.

open func onDidDismissView()

After the dismissal animation completes, dismissView calls onDidDismissView from within completion callback.

onWillDismissView, performCustomDismissingAnimation, onDidDismissView won't be invoked if you override dismissView implementation.

To show how simple and powerful XLActionController is and give several examples of how to extend ActionController we have mimicked the Skype, Tweetbot, Twitter, Youtube, Periscope and Spotify action controllers.

Requirements

  • iOS 9.3+
  • Xcode 10.2+
  • Swift 5.0+

Getting involved

  • If you want to contribute please feel free to submit pull requests.
  • If you have a feature request please open an issue.
  • If you found a bug or need help please check older issues before submitting an issue.

If you use XLActionController in your app we would love to hear about it! Drop us a line on twitter.

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects.

Specify XLActionController into your project's Podfile:

source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!

target '<Your App Target>' do
  # This will install just the library's core, won't include any examples
  pod 'XLActionController'

  # Uncomment depending on the examples that you want to install
  #pod 'XLActionController/Periscope'
  #pod 'XLActionController/Skype'
  #pod 'XLActionController/Spotify'
  #pod 'XLActionController/Tweetbot'
  #pod 'XLActionController/Twitter'
  #pod 'XLActionController/Youtube'
end

Then run the following command:

$ pod install

Carthage

Carthage is a simple, decentralized dependency manager for Cocoa.

Specify XLActionController into your project's Carthage:

github "xmartlabs/XLActionController" ~> 5.1.0

Manually as Embedded Framework

Clone XLActionController as a git submodule by running the following command from your project root git folder.

$ git submodule add https://github.com/xmartlabs/XLActionController.git

Open XLActionController folder that was created by the previous git submodule command and drag the XLActionController.xcodeproj into the Project Navigator of your application's Xcode project.

Select the XLActionController.xcodeproj in the Project Navigator and verify the deployment target matches with your application deployment target.

Select your project in the Xcode Navigation and then select your application target from the sidebar. Next select the "General" tab and click on the + button under the "Embedded Binaries" section.

Select XLActionController.framework and we are done!

Download Details:

Author: xmartlabs
Source Code: https://github.com/xmartlabs/XLActionController 
License: MIT license

#swift #ios 

Nestor  Beier

Nestor Beier

1622211475

Elementor PODs Tutorial - How to work with Custom Post Types and Custom Fields

Elementor PODs Tutorial - How to work with Custom Post Types and Custom Fields

#elementor #wordpress #web-development

Controller Extra Bundle for Symfony2

ControllerExtra for Symfony2

This bundle provides a collection of annotations for Symfony2 Controllers, designed to streamline the creation of certain objects and enable smaller and more concise actions.

Reference

By default, all annotations are loaded, but any individual annotation can be completely disabled by setting to false active parameter.

Default values are:

controller_extra:
    resolver_priority: -8
    request: current
    paginator:
        active: true
        default_name: paginator
        default_page: 1
        default_limit_per_page: 10
    entity:
        active: true
        default_name: entity
        default_persist: true
        default_mapping_fallback: false
        default_factory_method: create
        default_factory_mapping: true
    form:
        active: true
        default_name: form
    object_manager:
        active: true
        default_name: form
    flush:
        active: true
        default_manager: default
    json_response:
        active: true
        default_status: 200
        default_headers: []
    log:
        active: true
        default_level: info
        default_execute: pre

ResolverEventListener is subscribed to kernel.controller event with priority -8. This element can be configured and customized with resolver_priority config value. If you need to get ParamConverter entities, make sure that this value is lower than 0. The reason is that this listener must be executed always after ParamConverter one.

Entity provider

In some annotations, you can define an entity by several ways. This chapter is about how you can define them.

By namespace

You can define an entity using its namespace. A simple new new() be performed.

/**
 * Simple controller method
 *
 * @SomeAnnotation(
 *      class = "Mmoreram\CustomBundle\Entity\MyEntity",
 * )
 */
public function indexAction()
{
}

By doctrine shortcut

You can define an entity using Doctrine shortcut notations. With this format you should ensure that your Entities follow Symfony Bundle standards and your entities are placed under Entity/ folder.

/**
 * Simple controller method
 *
 * @SomeAnnotation(
 *      class = "MmoreramCustomBundle:MyEntity",
 * )
 */
public function indexAction()
{
}

By parameter

You can define an entity using a simple config parameter. Some projects use parameters to define all entity namespaces (To allow overriding). If you define the entity with a parameter, this bundle will try to instance it with a simple new() accessing directly to the container ParametersBag.

parameters:

    #
    # Entities
    #
    my.bundle.entity.myentity: Mmoreram\CustomBundle\Entity\MyEntity
/**
 * Simple controller method
 *
 * @SomeAnnotation(
 *      class = "my.bundle.entity.myentity",
 * )
 */
public function indexAction()
{
}

Controller annotations

This bundle provide a reduced but useful set of annotations for your controller actions.

@CreatePaginator

Creates a Doctrine Paginator object, given a request and a configuration. This annotation just injects into de controller a new Doctrine\ORM\Tools\Pagination\Pagination instance ready to be iterated.

You can enable/disable this bundle by overriding active flag in configuration file config.yml

controller_extra:
    pagination:
        active: true

By default, if name option is not set, the generated object will be placed in a parameter named $paginator. This behaviour can be configured using default_name in configuration.

This annotation can be configured with these sections

Paginator Entity

To create a new Pagination object you need to refer to an existing Entity. You can check all available formats you can define it just reading the Entity Provider section.

<?php

use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
 * Simple controller method
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 * )
 */
public function indexAction(Paginator $paginator)
{
}

Paginator page

You need to specify Paginator annotation the page to fetch. By default, if none is specified, this bundle will use the default one defined in configuration. You can override in config.yml

controller_extra:
    pagination:
        default_page: 1

You can refer to an existing Request attribute using ~value~ format, to any $_GET element by using format ?field? or to any $_POST by using format #field#

You can choose between Master Request or Current Request accessing to its attributes, by configuring the request value of the configuration.

use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
 * Simple controller method
 *
 * This Controller matches pattern /myroute/paginate/{foo}
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      page = "~foo~"
 * )
 */
public function indexAction(Paginator $paginator)
{
}

or you can hardcode the page to use.

use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
 * Simple controller method
 *
 * This Controller matches pattern /myroute/paginate/
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      page = 1
 * )
 */
public function indexAction(Paginator $paginator)
{
}

Paginator limit

You need to specify Paginator annotation the limit to fetch. By default, if none is specified, this bundle will use the default one defined in configuration. You can override in config.yml

controller_extra:
    pagination:
        default_limit_per_page: 10

You can refer to an existing Request attribute using ~value~ format, to any $_GET element by using format ?field? or to any $_POST by using format #field#

use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
 * Simple controller method
 *
 * This Controller matches pattern /myroute/paginate/{foo}/{limit}
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      page = "~foo~",
 *      limit = "~limit~"
 * )
 */
public function indexAction(Paginator $paginator)
{
}

or you can hardcode the page to use.

use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
 * Simple controller method
 *
 * This Controller matches pattern /myroute/paginate/
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      page = 1,
 *      limit = 10
 * )
 */
public function indexAction(Paginator $paginator)
{
}

Paginator OrderBy

You can order your Pagination just defining the fields you want to orderBy and the desired direction. The orderBy section must be defined as an array of arrays, and each array should contain these positions:

  • First position: Entity alias (Principal object is set as x)
  • Second position: Entity field
  • Third position: Direction
  • Fourth position: Custom direction map (optional)
use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
 * Simple controller method
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      orderBy = {
 *          {"x", "createdAt", "ASC"},
 *          {"x", "updatedAt", "DESC"},
 *          {"x", "id", 1, {
 *              0 => "ASC",
 *              1 => "DESC",
 *          }},
 *      }
 * )
 */
public function indexAction(Paginator $paginator)
{
}

With the third and fourth value you can define a map where to match your own direction nomenclature with DQL one. DQL nomenclature just accept ASC for Ascendant and DESC for Descendant.

This is very useful when you need to match a url format with the DQL one. You can refer to an existing Request attribute using ~value~ format, to any $_GET element by using format ?field? or to any $_POST by using format #field#

use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
 * Simple controller method
 *
 * This Controller matches pattern /myroute/paginate/order/{field}/{direction}
 *
 * For example, some matchings...
 *
 * /myroute/paginate/order/id/1 -> ORDER BY id DESC
 * /myroute/paginate/order/enabled/0 - ORDER BY enabled ASC
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      orderBy = {
 *          {"x", "createdAt", "ASC"},
 *          {"x", "updatedAt", "DESC"},
 *          {"x", "~field~", ~direction~, {
 *              0 => "ASC",
 *              1 => "DESC",
 *          }},
 *      }
 * )
 */
public function indexAction(Paginator $paginator)
{
}

The order of the definitions will alter the order of the DQL query.

Paginator Wheres

You can define some where statements in your Paginator. The wheres section must be defined as an array of arrays, and each array should contain these positions:

  • First position: Entity alias (Principal object is set as x)
  • Second position: Entity field
  • Third position: Operator =, <=, >, LIKE...
  • Fourth position: Value to compare with
  • Fifth position: Is a filter. By default, false
use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
 * Simple controller method
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      wheres = {
 *          {"x", "enabled", "=", true},
 *          {"x", "age", ">", 18},
 *          {"x", "name", "LIKE", "Eferv%"},
 *      }
 * )
 */
public function indexAction(Paginator $paginator)
{
}

You can refer to an existing Request attribute using ~value~ format, to any $_GET element by using format ?field? or to any $_POST by using format #field#

use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
 * Simple controller method
 *
 * This Controller matches pattern /myroute/{field}
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      wheres = {
 *          {"x", "name", "LIKE", "~field~"},
 *      }
 * )
 */
public function indexAction(Paginator $paginator)
{
}

You can use as well this feature for optional filtering by setting the last position to true. In that case, if the filter value is not found, such line will be ignored.

use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
 * Simple controller method
 *
 * This Controller matches pattern /myroute?query=name%
 * This Controller matches pattern /myroute as well
 *
 * In both cases this will work. In the first case we will apply the where line
 * in the paginator. In the second case, we wont.
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      wheres = {
 *          {"x", "name", "LIKE", "?query?", true},
 *      }
 * )
 */
public function indexAction(Paginator $paginator)
{
}

Paginator Not Nulls

You can also define some fields to not null. Is same as wheres section, but specific for NULL assignments. The notNulls section must be defined as an array of arrays, and each array should contain these positions:

  • First position: Object (Principal object is set as x)
  • Second position: Field
use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
 * Simple controller method
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      notNulls = {
 *          {"x", "enabled"},
 *          {"x", "deleted"},
 *      }
 * )
 */
public function indexAction(Paginator $paginator)
{
}

Paginator Left Join

You can do some left joins in this section. The leftJoins section must be defined as an array of array, where each array can have these fields:

  • First position: Entity alias (Principal object is set as x)
  • Second position: Entity relation (Address)
  • Third position: Relation identifier (a)
  • Fourth position: If true, this relation is added in select group. Otherwise, wont be loaded until its request (optional)
use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
 * Simple controller method
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      leftJoins = {
 *          {"x", "User", "u", true},
 *          {"x", "Address", "a", true},
 *          {"x", "Cart", "c"},
 *      }
 * )
 */
public function indexAction(Paginator $paginator)
{
}

Paginator Inner Join

You can do some inner joins in this section. The innerJoins section must be defined as an array of array, where each array can have these fields:

  • First position: Entity alias (x)
  • Second position: Entity relation (Address)
  • Third position: Relation identifier (a)
  • Fourth position: If true, this relation is added in select group. Otherwise, wont be loaded until its request (optional)
use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
 * Simple controller method
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      innerJoins = {
 *          {"x", "User", "u", true},
 *          {"x", "Address", "a", true},
 *          {"x", "Cart", "c"},
 *      }
 * )
 */
public function indexAction(Paginator $paginator)
{
}

Paginator Attributes

A nice feature of this annotation is that you can also inject into your controller a Mmoreram\ControllerExtraBundle\ValueObject\PaginatorAttributes instance with some interesting information about your pagination.

  • currentPage : Current page fetched
  • totalElements : Total elements given your criteria. If none criteria is defined in your configuration, this value will show all elements of a certain entity.
  • totalPages : Total pages you can fetch given a criteria.
  • limitPerPage: Maximum number of elements in each page.

To inject this object you need to define the "attributes" annotation field with the method parameter name.

use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;
use Mmoreram\ControllerExtraBundle\ValueObject\PaginatorAttributes;

/**
 * Simple controller method
 *
 * This Controller matches pattern /myroute/paginate/
 *
 * @CreatePaginator(
 *      attributes = "paginatorAttributes",
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      page = 1,
 *      limit = 10
 * )
 */
public function indexAction(
    Paginator $paginator,
    PaginatorAttributes $paginatorAttributes
)
{
    $currentPage = $paginatorAttributes->getCurrentPage();
    $totalElements = $paginatorAttributes->getTotalElements();
    $totalPages = $paginatorAttributes->getTotalPages();
    $limitPerPage = $paginatorAttributes->getLimitPerPage();

}

Paginator Example

This is a completed example and its DQL resolution

use Doctrine\ORM\Tools\Pagination\Pagination;
use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;

/**
 * Simple controller method
 *
 * This Controller matches pattern /paginate/nb/{limit}/{page}
 *
 * Where:
 *
 * * limit = 10
 * * page = 1
 *
 * @CreatePaginator(
 *      entityNamespace = "ControllerExtraBundle:Fake",
 *      page = "~page~",
 *      limit = "~limit~",
 *      orderBy = {
 *          { "x", "createdAt", "ASC" },
 *          { "x", "updatedAt", "DESC" },
 *          { "x", "id", "0", {
 *              "1" = "ASC",
 *              "2" = "DESC",
 *          }}
 *      },
 *      wheres = {
 *          { "x", "enabled" , "=", true }
 *      },
 *      leftJoins = {
 *          { "x", "relation", "r" },
 *          { "x", "relation2", "r2" },
 *          { "x", "relation5", "r5", true },
 *      },
 *      innerJoins = {
 *          { "x", "relation3", "r3" },
 *          { "x", "relation4", "r4", true },
 *      },
 *      notNulls = {
 *          {"x", "address1"},
 *          {"x", "address2"},
 *      }
 * )
 */
public function indexAction(Paginator $paginator)
{
}

The DQL generated by this annotation is

    SELECT x, r4, r5
    FROM Mmoreram\\ControllerExtraBundle\\Tests\\FakeBundle\\Entity\\Fake x

    INNER JOIN x.relation3 r3
    INNER JOIN x.relation4 r4

    LEFT JOIN x.relation r
    LEFT JOIN x.relation2 r2
    LEFT JOIN x.relation5 r5

    WHERE enabled = ?where0
    AND x.address1 IS NOT NULL
    AND x.address2 IS NOT NULL

    ORDER BY createdAt ASC, id ASC

PagerFanta Add-on

This annotation can create a PagerFanta instance if you need it. You only have to define your parameter as such, and the annotation resolver will wrap your paginator with a Pagerfanta object instance.

use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;
use Pagerfanta\Pagerfanta;

/**
 * Simple controller method
 *
 * This Controller matches pattern /myroute/paginate/
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      page = 1,
 *      limit = 10
 * )
 */
public function indexAction(Pagerfanta $paginator)
{
}

KNPPaginator Add-on

This annotation can create a KNPPaginator instance if you need it. You only have to define your parameter as such, and the annotation resolver will wrap your paginator with a KNPPaginator object instance.

use Mmoreram\ControllerExtraBundle\Annotation\CreatePaginator;
use Knp\Component\Pager\Pagination\PaginationInterface;

/**
 * Simple controller method
 *
 * This Controller matches pattern /myroute/paginate/
 *
 * @CreatePaginator(
 *      entityNamespace = "MmoreramCustomBundle:User",
 *      page = 1,
 *      limit = 10
 * )
 */
public function indexAction(PaginationInterface $paginator)
{
}

@LoadEntity

Loads an entity from your database, or creates a new one.

<?php

use Mmoreram\ControllerExtraBundle\Annotation\Entity;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
 * Simple controller method
 *
 * @Entity(
 *      namespace = "MmoreramCustomBundle:User",
 *      name  = "user"
 * )
 */
public function indexAction(User $user)
{
}

By default, if name option is not set, the generated object will be placed in a parameter named $entity. This behaviour can be configured using default_name in configuration.

You can also use setters in Entity annotation. It means that you can simply call entity setters using Request attributes.

<?php

use Mmoreram\ControllerExtraBundle\Annotation\Entity;
use Mmoreram\ControllerExtraBundle\Entity\Address;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
 * Simple controller method
 *
 * @Entity(
 *      namespace = "MmoreramCustomBundle:Address",
 *      name  = "address"
 * )
 * @Entity(
 *      namespace = "MmoreramCustomBundle:User",
 *      name  = "user",
 *      setters = {
 *          "setAddress": "address"
 *      }
 * )
 */
public function indexAction(Address $address, User $user)
{
}

When User instance is built, method setAddress is called using as parameter the new Address instance.

New entities are just created with a simple new(), so they are not persisted. By default, they will be persisted using configured manager, but you can disable this feature using persist option.

<?php

use Mmoreram\ControllerExtraBundle\Annotation\Entity;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
 * Simple controller method
 *
 * @Entity(
 *      namespace = "MmoreramCustomBundle:User",
 *      name  = "user",
 *      persist = false
 * )
 */
public function indexAction(User $user)
{
}

Entity Mapping

When you define a new Entity annotation, you can also request the mapped entity given a map. It means that if a map is defined, this bundle will try to request the mapped instance satisfying it.

The keys of the map represent the names of the mapped fields and the values represent their desired values. Remember than you can refer to any Request attribute by using format ~field~, to any $_GET element by using format ?field? or to any $_POST by using format #field#

<?php

use Mmoreram\ControllerExtraBundle\Annotation\Entity;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
 * Simple controller method
 *
 * This Controller matches pattern /user/edit/{id}/{username}
 *
 * @Entity(
 *      namespace = "MmoreramCustomBundle:User",
 *      name  = "user",
 *      mapping = {
 *          "id": "~id~",
 *          "username": "~username~"
 *      }
 * )
 */
public function indexAction(User $user)
{
}

In this case, you will try to get the mapped instance of User with passed id. If some mapping is defined and any entity is found, a new EntityNotFoundException` is thrown.

Entity Mapping Fallback

So what if one ore more than one mapping references are not found? For example, you're trying to map the {id} parameter from your route, but this parameter is not even defined. Whan happens here? Well, you can assume then that you want to pass a new entity instance by using the mappingFallback.

By default, if mapping_fallback option is not set, the used value will be the parameter default_mapping_fallback defined in configuration. By default this value is false

Don't confuse with the scenario where you're looking for an entity in your database, all mapping references have been resolved, and the entity is not found. In that case, a common "EntityNotFound" exception will be thrown by Doctrine.

Lets see an example. Because we have enabled the mappingFallback, and because the mapping definition does not match the assigned route, we will return a new empty User entity.

<?php

use Mmoreram\ControllerExtraBundle\Annotation\Entity;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
 * Simple controller method
 *
 * This Controller matches pattern /user/edit/{id}
 *
 * @LoadEntity(
 *      namespace = "MmoreramCustomBundle:User",
 *      name  = "user",
 *      mapping = {
 *          "id": "~id~",
 *          "username": "~nonexisting~"
 *      },
 *      mappingFallback = true
 * )
 */
public function indexAction(User $user)
{
    // $user->getId() === null
}

Entity Repository

By default, the Doctrine entity manager provides the right repository per each entity (not the default one, but the right specific one). Although, you can define a custom repository to be used in your annotation by using the repository configuration.

/**
 * Simple controller method
 *
 * @CreateEntity(
 *      namespace = "MmoreramCustomBundle:User",
 *      mapping = {
 *          "id": "~id~",
 *          "username": "~username~"
 *      }
 *      repository = {
 *          "class" = "Mmoreram\CustomBundle\Repository\AnotherRepository",
 *      },
 * )
 */
public function indexAction(User $user)
{
}

By default, the method findOneBy will always be used, unless you define another one.

/**
 * Simple controller method
 *
 * @CreateEntity(
 *      namespace = "MmoreramCustomBundle:User",
 *      mapping = {
 *          "id": "~id~",
 *          "username": "~username~"
 *      }
 *      repository = {
 *          "class" = "Mmoreram\CustomBundle\Repository\AnotherRepository",
 *          "method" = "find",
 *      },
 * )
 */
public function indexAction(User $user)
{
}

Entity Factory

When the annotation considers that a new entity must be created, because no mapping information has been provided, or because the mapping fallback has been activated, by default a new instance will be created by using the namespace value.

This configuration block has three positions

  • class - factory class
  • method - Method to use when retrieving the object
  • static - Method is static

You can define the factory with a simple namespace

/**
 * Simple controller method
 *
 * @CreateEntity(
 *      namespace = "MmoreramCustomBundle:User",
 *      factory = {
 *          "class" = "Mmoreram\CustomBundle\Factory\UserFactory",
 *          "method" = "create",
 *          "static" = true,
 *      },
 * )
 */
public function indexAction(User $user)
{
}

If you want to define your Factory as a service, with the possibility of overriding namespace, you can simply define service name. All other options have the same behaviour.

parameters:

    #
    # Factories
    #
    my.bundle.factory.user_factory: Mmoreram\CustomBundle\Factory\UserFactory
/**
 * Simple controller method
 *
 * @CreateEntity(
 *      class = {
 *          "factory" = my.bundle.factory.user_factory,
 *          "method" = "create",
 *          "static" = true,
 *      },
 * )
 */
public function indexAction(User $user)
{
}

If you do not define the method, default one will be used. You can override this default value by defining new one in your config.yml. Same with static value

controller_extra:
    entity:
        default_factory_method: create
        default_factory_static: true

@CreateForm

Provides form injection in your controller actions. This annotation only needs a name to be defined in, where you must define namespace where your form is placed.

<?php

use Mmoreram\ControllerExtraBundle\Annotation\CreateForm;
use Symfony\Component\Form\AbstractType;

/**
 * Simple controller method
 *
 * @CreateForm(
 *      class = "\Mmoreram\CustomBundle\Form\Type\UserType",
 *      name  = "userType"
 * )
 */
public function indexAction(AbstractType $userType)
{
}

By default, if name option is not set, the generated object will be placed in a parameter named $form. This behaviour can be configured using default_name in configuration.

You can not just define your Type location using the namespace, in which case a new AbstractType element will be created. but you can also define it using service alias, in which case this bundle will return an instance using Symfony DI.

<?php

use Mmoreram\ControllerExtraBundle\Annotation\CreateForm;
use Symfony\Component\Form\AbstractType;

/**
 * Simple controller method
 *
 * @CreateForm(
 *      class = "user_type",
 *      name  = "userType"
 * )
 */
public function indexAction(AbstractType $userType)
{
}

This annotation allows you to not only create an instance of FormType, but also allows you to inject a Form object or a FormView object

To inject a Form object you only need to cast method value as such.

<?php

use Mmoreram\ControllerExtraBundle\Annotation\CreateForm;
use Symfony\Component\Form\Form;

/**
 * Simple controller method
 *
 * @CreateForm(
 *      class = "user_type",
 *      name  = "userForm"
 * )
 */
public function indexAction(Form $userForm)
{
}

You can also, using [SensioFrameworkExtraBundle][1]'s [ParamConverter][2], create a Form object with an previously created entity. you can define this entity using entity parameter.

<?php

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\Form\Form;

use Mmoreram\ControllerExtraBundle\Annotation\CreateForm;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
 * Simple controller method
 *
 * @Route(
 *      path = "/user/{id}",
 *      name = "view_user"
 * )
 * @ParamConverter("user", class="MmoreramCustomBundle:User")
 * @CreateForm(
 *      class  = "user_type",
 *      entity = "user"
 *      name   = "userForm",
 * )
 */
public function indexAction(User $user, Form $userForm)
{
}

To handle current request, you can set handleRequest to true. By default this value is set to false

<?php

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\Form\Form;

use Mmoreram\ControllerExtraBundle\Annotation\CreateForm;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
 * Simple controller method
 *
 * @Route(
 *      path = "/user/{id}",
 *      name = "view_user"
 * )
 * @ParamConverter("user", class="MmoreramCustomBundle:User")
 * @CreateForm(
 *      class         = "user_type",
 *      entity        = "user"
 *      handleRequest = true,
 *      name          = "userForm",
 * )
 */
public function indexAction(User $user, Form $userForm)
{
}

You can also add as a method parameter if the form is valid, using validate setting. Annotation will place result of $form->isValid() in specified method argument.

<?php

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\Form\Form;

use Mmoreram\ControllerExtraBundle\Annotation\CreateForm;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
 * Simple controller method
 *
 * @Route(
 *      path = "/user/{id}",
 *      name = "view_user"
 * )
 * @ParamConverter("user", class="MmoreramCustomBundle:User")
 * @CreateForm(
 *      class         = "user_type",
 *      entity        = "user"
 *      handleRequest = true,
 *      name          = "userForm",
 *      validate      = "isValid",
 * )
 */
public function indexAction(User $user, Form $userForm, $isValid)
{
}

To inject a FormView object you only need to cast method variable as such.

<?php

use Symfony\Component\Form\FormView;

use Mmoreram\ControllerExtraBundle\Annotation\CreateForm;

/**
 * Simple controller method
 *
 * @CreateForm(
 *      class = "user_type",
 *      name  = "userFormView"
 * )
 */
public function indexAction(FormView $userFormView)
{
}

@Flush

Flush annotation allows you to flush entityManager at the end of request using kernel.response event

<?php

use Mmoreram\ControllerExtraBundle\Annotation\Flush;

/**
 * Simple controller method
 *
 * @Flush
 */
public function indexAction()
{
}

If not otherwise specified, default Doctrine Manager will be flushed with this annotation. You can overwrite default Manager in your config.yml file.

controller_extra:
    flush:
        default_manager: my_custom_manager

You can also override this value in every single Flush Annotation instance defining manager value

<?php

use Mmoreram\ControllerExtraBundle\Annotation\Flush;

/**
 * Simple controller method
 *
 * @Flush(
 *      manager = "my_own_manager"
 * )
 */
public function indexAction()
{
}

If you want to change default manager in all annotation instances, you should override bundle parameter in your config.yml file.

controller_extra:
    flush:
        default_manager: my_own_manager

If any parameter is set, annotation will flush all. If you only need to flush one or many entities, you can define explicitly which entity must be flushed.

<?php

use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;

use Mmoreram\ControllerExtraBundle\Annotation\Flush;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
 * Simple controller method
 *
 * @ParamConverter("user", class="MmoreramCustomBundle:User")
 * @Flush(
 *      entity = "user"
 * )
 */
public function indexAction(User $user)
{
}

You can also define a set of entities to flush

<?php

use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;

use Mmoreram\ControllerExtraBundle\Annotation\Flush;
use Mmoreram\ControllerExtraBundle\Entity\Address;
use Mmoreram\ControllerExtraBundle\Entity\User;

/**
 * Simple controller method
 *
 * @ParamConverter("user", class="MmoreramCustomBundle:User")
 * @ParamConverter("address", class="MmoreramCustomBundle:Address")
 * @Flush(
 *      entity = {
 *          "user", 
 *          "address"
 *      }
 * )
 */
public function indexAction(User $user, Address $address)
{
}

If multiple @Mmoreram\Flush are defined in same action, last instance will overwrite previous. Anyway just one instance should be defined.

@ToJsonResponse

JsonResponse annotation allows you to create a Symfony\Component\HttpFoundation\JsonResponse object, given a simple controller return value.

<?php

use Mmoreram\ControllerExtraBundle\Annotation\ToJsonResponse;

/**
 * Simple controller method
 *
 * @ToJsonResponse
 */
public function indexAction(User $user, Address $address)
{
    return array(
        'This is my response'
    );
}

By default, JsonResponse is created using default status and headers defined in bundle parameters. You can overwrite them in your config.yml file.

controller_extra:
    json_response:
        default_status: 403
        default_headers:
            "User-Agent": "Googlebot/2.1"

You can also overwrite these values in each @JsonResponse annotation.

<?php

use Mmoreram\ControllerExtraBundle\Annotation\ToJsonResponse;

/**
 * Simple controller method
 *
 * @ToJsonResponse(
 *      status = 403,
 *      headers = {
 *          "User-Agent": "Googlebot/2.1"
 *      }
 * )
 */
public function indexAction(User $user, Address $address)
{
    return array(
        'This is my response'
    );
}

If an Exception is returned the response status is set by default to 500 and the Exception message is returned as response.

STATUS 500 Internal server error

{
    message : 'Exception message'
}

In case we use a HttpExceptionInterface the use the exception status code as status code. In case we launch this exception

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

...

return new NotFoundHttpException('Resource not found');

We'll receive this response

STATUS 404 Not Found

{
    message : 'Resource not found'
}

If the exception is being launched on an annotation (e.g. Entity annotation) remember to add the JsonResponse annotation at the beginning or at least before any annotation that could cause an exception.

If multiple @Mmoreram\JsonResponse are defined in same action, last instance will overwrite previous. Anyway just one instance should be defined.

@Log

Log annotation allows you to log any plain message before or after controller action execution

<?php

use Mmoreram\ControllerExtraBundle\Annotation\Log;

/**
 * Simple controller method
 *
 * @Log("Executing index Action")
 */
public function indexAction()
{
}

You can define the level of the message. You can define default one if none is specified overriding it in your config.yml file.

controller_extra:
    log:
        default_level: warning

Every Annotation instance can overwrite this value using level field.

<?php

use Mmoreram\ControllerExtraBundle\Annotation\Log;

/**
 * Simple controller method
 *
 * @Log(
 *      value   = "Executing index Action",
 *      level   = @Log::LVL_WARNING
 * )
 */
public function indexAction()
{
}

Several levels can be used, as defined in [Psr\Log\LoggerInterface][6] interface

  • @Mmoreram\Log::LVL_EMERG
  • @Mmoreram\Log::LVL_CRIT
  • @Mmoreram\Log::LVL_ERR
  • @Mmoreram\Log::LVL_WARN
  • @Mmoreram\Log::LVL_NOTICE
  • @Mmoreram\Log::LVL_INFO
  • @Mmoreram\Log::LVL_DEBUG
  • @Mmoreram\Log::LVL_LOG

You can also define the execution of the log. You can define default one if none is specified overriding it in your config.yml file.

controller_extra:
    log:
        default_execute: pre

Every Annotation instance can overwrite this value using level field.

<?php

use Mmoreram\ControllerExtraBundle\Annotation\Log;

/**
 * Simple controller method
 *
 * @Log(
 *      value   = "Executing index Action",
 *      execute = @Log::EXEC_POST
 * )
 */
public function indexAction()
{
}

Several executions can be used,

  • @Mmoreram\Log::EXEC_PRE - Logged before controller execution
  • @Mmoreram\Log::EXEC_POST - Logged after controller execution
  • @Mmoreram\Log::EXEC_BOTH - Logged both

@Get

The Get annotation allows you to get any parameter from the request query string.

For a GET request like:

GET /my-page?foo=bar HTTP/1.1

You can can simply get the foo var using the GET annotation

<?php

use Mmoreram\ControllerExtraBundle\Annotation\Get;

/**
 * Simple controller method
 *
 * @Get(
 *     path = "foo"
 * )
 */
public function indexAction($foo)
{
    // Use the foo var
}

You can also customize the var name and the default value in case the var is not sent on the query string.

For a GET request like:

GET /my-page HTTP/1.1

And this annotation

<?php

use Mmoreram\ControllerExtraBundle\Annotation\Get;

/**
 * Simple controller method
 *
 * @Get(
 *     path = "foo",
 *     name = "varName",
 *     default = 'bar',
 * )
 */
public function indexAction($varName)
{
    // This would print 'bar'
    echo $varName;
}

@Post

The Post annotation allows you to get any parameter from the post request body.

For a POST request like:

POST /my-page HTTP/1.1
foo=bar

You can can simply get the foo var using the POST annotation

<?php

use Mmoreram\ControllerExtraBundle\Annotation\Post;

/**
 * Simple controller method
 *
 * @Post(
 *     path = "foo"
 * )
 */
public function indexAction($foo)
{
    // Use the foo var
}

You can also customize the var name and the default value in case the var is not sent on the query string.

For a POST request like:

POST /my-page HTTP/1.1

And this annotation

<?php

use Mmoreram\ControllerExtraBundle\Annotation\Post;

/**
 * Simple controller method
 *
 * @Post(
 *     path = "foo",
 *     name = "varName",
 *     default = 'bar',
 * )
 */
public function indexAction($varName)
{
    // This would print 'bar'
    echo $varName;
}

Custom annotations

Using this bundle you can now create, in a very easy way, your own controller annotation.

Annotation

The annotation object. You need to define the fields your custom annotation will contain. Must extends Mmoreram\ControllerExtraBundle\Annotation\Annotation abstract class.

<?php

namespace My\Bundle\Annotation;

use Mmoreram\ControllerExtraBundle\Annotation\Annotation;

/**
 * Entity annotation driver
 *
 * @Annotation
 * @Target({"METHOD"})
 */
final class MyCustomAnnotation extends Annotation
{
    /**
     * @var string
     *
     * Dummy field
     */
    public $field;
    
    /**
     * Get Dummy field
     *
     * @return string Dummy field
     */
    public function getField()
    {
        return $this->field;
    }
}

Resolver

Once you have defined your own annotation, you have to resolve how this annotation works in a controller. You can manage this using a Resolver. Must extend Mmoreram\ControllerExtraBundle\Resolver\AnnotationResolver; abstract class.

<?php

namespace My\Bundle\Resolver;

use Symfony\Component\HttpFoundation\Request;

use Mmoreram\ControllerExtraBundle\Resolver\AnnotationResolver;
use Mmoreram\ControllerExtraBundle\Annotation\Annotation;

/**
 * MyCustomAnnotation Resolver
 */
class MyCustomAnnotationResolver extends AnnotationResolver
{
    /**
     * Specific annotation evaluation.
     *
     * This method must be implemented in every single EventListener
     * with specific logic
     *
     * All method code will executed only if specific active flag is true
     *
     * @param Request          $request
     * @param Annotation       $annotation
     * @param ReflectionMethod $method
     */
    public function evaluateAnnotation(
        Request $request,
        Annotation $annotation,
        ReflectionMethod $method
    )
    {
        /**
         * You can now manage your annotation.
         * You can access to its fields using public methods.
         * 
         * Annotation fields can be public and can be acceded directly,
         * but is better for testing to use getters; they can be mocked.
         */
        $field = $annotation->getField();
        
        /**
         * You can also access to existing method parameters.
         * 
         * Available parameters are:
         * 
         * # ParamConverter parameters ( See `resolver_priority` config value )
         * # All method defined parameters, included Request object if is set.
         */
        $entity = $request->attributes->get('entity');
        
        /**
         * And you can now place new elements in the controller action.
         * In this example we are creating new method parameter
         * called $myNewField with some value
         */
        $request->attributes->set(
            'myNewField',
            new $field()
        );
        
        return $this;
    }

}

This class will be defined as a service, so this method is computed just before executing current controller. You can also subscribe to some kernel events and do whatever you need to do ( You can check Mmoreram\ControllerExtraBundle\Resolver\LogAnnotationResolver for some examples.

Definition

Once Resolver is done, we need to define our service as an Annotation Resolver. We will use a custom tag.

parameters:
    #
    # Resolvers
    #
    my.bundle.resolver.my_custom_annotation_resolver.class: My\Bundle\Resolver\MyCustomAnnotationResolver

services:
    #
    # Resolvers
    #
    my.bundle.resolver.my_custom_annotation_resolver:
        class: %my.bundle.resolver.my_custom_annotation_resolver.class%
        tags:
            - { name: controller_extra.annotation }

Registration

We need to register our annotation inside our application. We can just do it in the boot() method of bundle.php file.

<?php

namespace My\Bundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use Doctrine\Common\Annotations\AnnotationRegistry;

/**
 * MyBundle
 */
class ControllerExtraBundle extends Bundle
{

    /**
     * Boots the Bundle.
     */
    public function boot()
    {
        $kernel = $this->container->get('kernel');

        AnnotationRegistry::registerFile($kernel
            ->locateResource("@MyBundle/Annotation/MyCustomAnnotation.php")
        );
    }
}

Et voilà! We can now use our custom Annotation in our project controllers.


Download Details:

Author: mmoreram
Source Code: https://github.com/mmoreram/ControllerExtraBundle

License: MIT license

#symfony #php 

Autumn  Blick

Autumn Blick

1596584126

R Tutorial: Better Blog Post Analysis with googleAnalyticsR

In my previous role as a marketing data analyst for a blogging company, one of my most important tasks was to track how blog posts performed.

On the surface, it’s a fairly straightforward goal. With Google Analytics, you can quickly get just about any metric you need for your blog posts, for any date range.

But when it comes to comparing blog post performance, things get a bit trickier.

For example, let’s say we want to compare the performance of the blog posts we published on the Dataquest blog in June (using the month of June as our date range).

But wait… two blog posts with more than 1,000 pageviews were published earlier in the month, And the two with fewer than 500 pageviews were published at the end of the month. That’s hardly a fair comparison!

My first solution to this problem was to look up each post individually, so that I could make an even comparison of how each post performed in their first day, first week, first month, etc.

However, that required a lot of manual copy-and-paste work, which was extremely tedious if I wanted to compare more than a few posts, date ranges, or metrics at a time.

But then, I learned R, and realized that there was a much better way.

In this post, we’ll walk through how it’s done, so you can do my better blog post analysis for yourself!

What we’ll need

To complete this tutorial, you’ll need basic knowledge of R syntax and the tidyverse, and access to a Google Analytics account.

Not yet familiar with the basics of R? We can help with that! Our interactive online courses teach you R from scratch, with no prior programming experience required. Sign up and start today!

You’ll also need the dyplrlubridate, and stringr packages installed — which, as a reminder, you can do with the install.packages() command.

Finally, you will need a CSV of the blog posts you want to analyze. Here’s what’s in my dataset:

post_url: the page path of the blog post

post_date: the date the post was published (formatted m/d/yy)

category: the blog category the post was published in (optional)

title: the title of the blog post (optional)

Depending on your content management system, there may be a way for you to automate gathering this data — but that’s out of the scope of this tutorial!

For this tutorial, we’ll use a manually-gathered dataset of the past ten Dataquest blog posts.

#data science tutorials #promote #r #r tutorial #r tutorials #rstats #tutorial #tutorials

Brook  Legros

Brook Legros

1659408900

TinyTDS: FreeTDS Bindings for Ruby using DB-Library

TinyTDS - Simple and fast FreeTDS bindings for Ruby using DB-Library.

  • TravisCI - TravisCI
  • Build Status - Appveyor
  • Gem Version - Gem Version
  • Gitter chat - Community

About TinyTDS

The TinyTDS gem is meant to serve the extremely common use-case of connecting, querying and iterating over results to Microsoft SQL Server or Sybase databases from Ruby using the FreeTDS's DB-Library API.

TinyTDS offers automatic casting to Ruby primitives along with proper encoding support. It converts all SQL Server datatypes to native Ruby primitives while supporting :utc or :local time zones for time-like types. To date it is the only Ruby client library that allows client encoding options, defaulting to UTF-8, while connecting to SQL Server. It also properly encodes all string and binary data. The motivation for TinyTDS is to become the de-facto low level connection mode for the SQL Server Adapter for ActiveRecord.

The API is simple and consists of these classes:

  • TinyTds::Client - Your connection to the database.
  • TinyTds::Result - Returned from issuing an #execute on the connection. It includes Enumerable.
  • TinyTds::Error - A wrapper for all FreeTDS exceptions.

Install

Installing with rubygems should just work. TinyTDS is currently tested on Ruby version 2.0.0 and upward.

$ gem install tiny_tds

If you use Windows, we pre-compile TinyTDS with static versions of FreeTDS and supporting libraries. If you're using RubyInstaller the binary gem will require that devkit is installed and in your path to operate properly.

On all other platforms, we will find these dependencies. It is recommended that you install the latest FreeTDS via your method of choice. For example, here is how to install FreeTDS on Ubuntu. You might also need the build-essential and possibly the libc6-dev packages.

$ apt-get install wget
$ apt-get install build-essential
$ apt-get install libc6-dev

$ wget http://www.freetds.org/files/stable/freetds-1.1.24.tar.gz
$ tar -xzf freetds-1.1.24.tar.gz
$ cd freetds-1.1.24
$ ./configure --prefix=/usr/local --with-tdsver=7.3
$ make
$ make install

Please read the MiniPortile and/or Windows sections at the end of this file for advanced configuration options past the following:

--with-freetds-dir=DIR
  Use the freetds library placed under DIR.

Getting Started

Optionally, Microsoft has done a great job writing some articles on how to get started with SQL Server and Ruby using TinyTDS. Please checkout one of the following posts that match your platform.

FreeTDS Compatibility & Configuration

TinyTDS is developed against FreeTDS 0.95, 0.99, and 1.0 current. Our default and recommended is 1.0. We also test with SQL Server 2008, 2014, and Azure. However, usage of TinyTDS with SQL Server 2000 or 2005 should be just fine. Below are a few QA style notes about installing FreeTDS.

NOTE: Windows users of our pre-compiled native gems need not worry about installing FreeTDS and its dependencies.

Do I need to install FreeTDS? Yes! Somehow, someway, you are going to need FreeTDS for TinyTDS to compile against.

OK, I am installing FreeTDS, how do I configure it? Contrary to what most people think, you do not need to specially configure FreeTDS in any way for client libraries like TinyTDS to use it. About the only requirement is that you compile it with libiconv for proper encoding support. FreeTDS must also be compiled with OpenSSL (or the like) to use it with Azure. See the "Using TinyTDS with Azure" section below for more info.

Do I need to configure --with-tdsver equal to anything? Most likely! Technically you should not have to. This is only a default for clients/configs that do not specify what TDS version they want to use. We are currently having issues with passing down a TDS version with the login bit. Till we get that fixed, if you are not using a freetds.conf or a TDSVER environment variable, then make sure to use 7.1.

But I want to use TDS version 7.2 for SQL Server 2005 and up! TinyTDS uses TDS version 7.1 (previously named 8.0) and fully supports all the data types supported by FreeTDS, this includes varchar(max) and nvarchar(max). Technically compiling and using TDS version 7.2 with FreeTDS is not supported. But this does not mean those data types will not work. I know, it's confusing If you want to learn more, read this thread. http://lists.ibiblio.org/pipermail/freetds/2011q3/027306.html

I want to configure FreeTDS using --enable-msdblib and/or --enable-sybase-compat so it works for my database. Cool? It's a waste of time and totally moot! Client libraries like TinyTDS define their own C structure names where they diverge from Sybase to SQL Server. Technically we use the MSDBLIB structures which does not mean we only work with that database vs Sybase. These configs are just a low level default for C libraries that do not define what they want. So I repeat, you do not NEED to use any of these, nor will they hurt anything since we control what C structure names we use internally!

Data Types

Our goal is to support every SQL Server data type and covert it to a logical Ruby object. When dates or times are returned, they are instantiated to either :utc or :local time depending on the query options. Only [datetimeoffset] types are excluded. All strings are associated the to the connection's encoding and all binary data types are associated to Ruby's ASCII-8BIT/BINARY encoding.

Below is a list of the data types we support when using the 7.3 TDS protocol version. Using a lower protocol version will result in these types being returned as strings.

  • [date]
  • [datetime2]
  • [datetimeoffset]
  • [time]

TinyTds::Client Usage

Connect to a database.

client = TinyTds::Client.new username: 'sa', password: 'secret', host: 'mydb.host.net'

Creating a new client takes a hash of options. For valid iconv encoding options, see the output of iconv -l. Only a few have been tested and highly recommended to leave blank for the UTF-8 default.

  • :username - The database server user.
  • :password - The user password.
  • :dataserver - Can be the name for your data server as defined in freetds.conf. Raw hostname or hostname:port will work here too. FreeTDS says that named instance like 'localhost\SQLEXPRESS' work too, but I highly suggest that you use the :host and :port options below. Google how to find your host port if you are using named instances or go here.
  • :host - Used if :dataserver blank. Can be an host name or IP.
  • :port - Defaults to 1433. Only used if :host is used.
  • :database - The default database to use.
  • :appname - Short string seen in SQL Servers process/activity window.
  • :tds_version - TDS version. Defaults to "7.3".
  • :login_timeout - Seconds to wait for login. Default to 60 seconds.
  • :timeout - Seconds to wait for a response to a SQL command. Default 5 seconds. Prior to 1.0rc5, FreeTDS was unable to set the timeout on a per-client basis, permitting only a global timeout value. This means that if you're using an older version, the timeout values for all clients will be overwritten each time you instantiate a new TinyTds::Client object. If you are using 1.0rc5 or later, all clients will have an independent timeout setting as you'd expect. Timeouts caused by network failure will raise a timeout error 1 second after the configured timeout limit is hit (see #481 for details).
  • :encoding - Any valid iconv value like CP1251 or ISO-8859-1. Default UTF-8.
  • :azure - Pass true to signal that you are connecting to azure.
  • :contained - Pass true to signal that you are connecting with a contained database user.
  • :use_utf16 - Instead of using UCS-2 for database wide character encoding use UTF-16. Newer Windows versions use this encoding instead of UCS-2. Default true.
  • :message_handler - Pass in a call-able object such as a Proc or a method to receive info messages from the database. It should have a single parameter, which will be a TinyTds::Error object representing the message. For example:
opts = ... # host, username, password, etc
opts[:message_handler] = Proc.new { |m| puts m.message }
client = TinyTds::Client.new opts
# => Changed database context to 'master'.
# => Changed language setting to us_english.
client.execute("print 'hello world!'").do
# => hello world!

Use the #active? method to determine if a connection is good. The implementation of this method may change but it should always guarantee that a connection is good. Current it checks for either a closed or dead connection.

client.dead?    # => false
client.closed?  # => false
client.active?  # => true
client.execute("SQL TO A DEAD SERVER")
client.dead?    # => true
client.closed?  # => false
client.active?  # => false
client.close
client.closed?  # => true
client.active?  # => false

Escape strings.

client.escape("How's It Going'") # => "How''s It Going''"

Send a SQL string to the database and return a TinyTds::Result object.

result = client.execute("SELECT * FROM [datatypes]")

TinyTds::Result Usage

A result object is returned by the client's execute command. It is important that you either return the data from the query, most likely with the #each method, or that you cancel the results before asking the client to execute another SQL batch. Failing to do so will yield an error.

Calling #each on the result will lazily load each row from the database.

result.each do |row|
  # By default each row is a hash.
  # The keys are the fields, as you'd expect.
  # The values are pre-built Ruby primitives mapped from their corresponding types.
end

A result object has a #fields accessor. It can be called before the result rows are iterated over. Even if no rows are returned, #fields will still return the column names you expected. Any SQL that does not return columned data will always return an empty array for #fields. It is important to remember that if you access the #fields before iterating over the results, the columns will always follow the default query option's :symbolize_keys setting at the client's level and will ignore the query options passed to each.

result = client.execute("USE [tinytdstest]")
result.fields # => []
result.do

result = client.execute("SELECT [id] FROM [datatypes]")
result.fields # => ["id"]
result.cancel
result = client.execute("SELECT [id] FROM [datatypes]")
result.each(:symbolize_keys => true)
result.fields # => [:id]

You can cancel a result object's data from being loading by the server.

result = client.execute("SELECT * FROM [super_big_table]")
result.cancel

You can use results cancelation in conjunction with results lazy loading, no problem.

result = client.execute("SELECT * FROM [super_big_table]")
result.each_with_index do |row, i|
  break if row > 10
end
result.cancel

If the SQL executed by the client returns affected rows, you can easily find out how many.

result.each
result.affected_rows # => 24

This pattern is so common for UPDATE and DELETE statements that the #do method cancels any need for loading the result data and returns the #affected_rows.

result = client.execute("DELETE FROM [datatypes]")
result.do # => 72

Likewise for INSERT statements, the #insert method cancels any need for loading the result data and executes a SCOPE_IDENTITY() for the primary key.

result = client.execute("INSERT INTO [datatypes] ([xml]) VALUES ('<html><br/></html>')")
result.insert # => 420

The result object can handle multiple result sets form batched SQL or stored procedures. It is critical to remember that when calling each with a block for the first time will return each "row" of each result set. Calling each a second time with a block will yield each "set".

sql = ["SELECT TOP (1) [id] FROM [datatypes]",
       "SELECT TOP (2) [bigint] FROM [datatypes] WHERE [bigint] IS NOT NULL"].join(' ')

set1, set2 = client.execute(sql).each
set1 # => [{"id"=>11}]
set2 # => [{"bigint"=>-9223372036854775807}, {"bigint"=>9223372036854775806}]

result = client.execute(sql)

result.each do |rowset|
  # First time data loading, yields each row from each set.
  # 1st: {"id"=>11}
  # 2nd: {"bigint"=>-9223372036854775807}
  # 3rd: {"bigint"=>9223372036854775806}
end

result.each do |rowset|
  # Second time over (if columns cached), yields each set.
  # 1st: [{"id"=>11}]
  # 2nd: [{"bigint"=>-9223372036854775807}, {"bigint"=>9223372036854775806}]
end

Use the #sqlsent? and #canceled? query methods on the client to determine if an active SQL batch still needs to be processed and or if data results were canceled from the last result object. These values reset to true and false respectively for the client at the start of each #execute and new result object. Or if all rows are processed normally, #sqlsent? will return false. To demonstrate, lets assume we have 100 rows in the result object.

client.sqlsent?   # = false
client.canceled?  # = false

result = client.execute("SELECT * FROM [super_big_table]")

client.sqlsent?   # = true
client.canceled?  # = false

result.each do |row|
  # Assume we break after 20 rows with 80 still pending.
  break if row["id"] > 20
end

client.sqlsent?   # = true
client.canceled?  # = false

result.cancel

client.sqlsent?   # = false
client.canceled?  # = true

It is possible to get the return code after executing a stored procedure from either the result or client object.

client.return_code  # => nil

result = client.execute("EXEC tinytds_TestReturnCodes")
result.do
result.return_code  # => 420
client.return_code  # => 420

Query Options

Every TinyTds::Result object can pass query options to the #each method. The defaults are defined and configurable by setting options in the TinyTds::Client.default_query_options hash. The default values are:

  • :as => :hash - Object for each row yielded. Can be set to :array.
  • :symbolize_keys => false - Row hash keys. Defaults to shared/frozen string keys.
  • :cache_rows => true - Successive calls to #each returns the cached rows.
  • :timezone => :local - Local to the Ruby client or :utc for UTC.
  • :empty_sets => true - Include empty results set in queries that return multiple result sets.

Each result gets a copy of the default options you specify at the client level and can be overridden by passing an options hash to the #each method. For example

result.each(:as => :array, :cache_rows => false) do |row|
  # Each row is now an array of values ordered by #fields.
  # Rows are yielded and forgotten about, freeing memory.
end

Besides the standard query options, the result object can take one additional option. Using :first => true will only load the first row of data and cancel all remaining results.

result = client.execute("SELECT * FROM [super_big_table]")
result.each(:first => true) # => [{'id' => 24}]

Row Caching

By default row caching is turned on because the SQL Server adapter for ActiveRecord would not work without it. I hope to find some time to create some performance patches for ActiveRecord that would allow it to take advantages of lazily created yielded rows from result objects. Currently only TinyTDS and the Mysql2 gem allow such a performance gain.

Encoding Error Handling

TinyTDS takes an opinionated stance on how we handle encoding errors. First, we treat errors differently on reads vs. writes. Our opinion is that if you are reading bad data due to your client's encoding option, you would rather just find ? marks in your strings vs being blocked with exceptions. This is how things wold work via ODBC or SMS. On the other hand, writes will raise an exception. In this case we raise the SYBEICONVO/2402 error message which has a description of Error converting characters into server's character set. Some character(s) could not be converted.. Even though the severity of this message is only a 4 and TinyTDS will automatically strip/ignore unknown characters, we feel you should know that you are inserting bad encodings. In this way, a transaction can be rolled back, etc. Remember, any database write that has bad characters due to the client encoding will still be written to the database, but it is up to you rollback said write if needed. Most ORMs like ActiveRecord handle this scenario just fine.

Timeout Error Handling

TinyTDS will raise a TinyTDS::Error when a timeout is reached based on the options supplied to the client. Depending on the reason for the timeout, the connection could be dead or alive. When db processing is the cause for the timeout, the connection should still be usable after the error is raised. When network failure is the cause of the timeout, the connection will be dead. If you attempt to execute another command batch on a dead connection you will see a DBPROCESS is dead or not enabled error. Therefore, it is recommended to check for a dead? connection before trying to execute another command batch.

Binstubs

The TinyTDS gem uses binstub wrappers which mirror compiled FreeTDS Utilities binaries. These native executables are usually installed at the system level when installing FreeTDS. However, when using MiniPortile to install TinyTDS as we do with Windows binaries, these binstubs will find and prefer local gem exe directory executables. These are the following binstubs we wrap.

  • tsql - Used to test connections and debug compile time settings.
  • defncopy - Used to dump schema structures.

Using TinyTDS With Rails & The ActiveRecord SQL Server adapter.

TinyTDS is the default connection mode for the SQL Server adapter in versions 3.1 or higher. The SQL Server adapter can be found using the links below.

Using TinyTDS with Azure

TinyTDS is fully tested with the Azure platform. You must set the azure: true connection option when connecting. This is needed to specify the default database name in the login packet since Azure has no notion of USE [database]. FreeTDS must be compiled with OpenSSL too.

IMPORTANT: Do not use username@server.database.windows.net for the username connection option! You must use the shorter username@server instead!

Also, please read the Azure SQL Database General Guidelines and Limitations MSDN article to understand the differences. Specifically, the connection constraints section!

Connection Settings

A DBLIB connection does not have the same default SET options for a standard SMS SQL Server connection. Hence, we recommend the following options post establishing your connection.

SQL Server

SET ANSI_DEFAULTS ON

SET QUOTED_IDENTIFIER ON
SET CURSOR_CLOSE_ON_COMMIT OFF
SET IMPLICIT_TRANSACTIONS OFF
SET TEXTSIZE 2147483647
SET CONCAT_NULL_YIELDS_NULL ON

Azure

SET ANSI_NULLS ON
SET ANSI_NULL_DFLT_ON ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON

SET QUOTED_IDENTIFIER ON
SET CURSOR_CLOSE_ON_COMMIT OFF
SET IMPLICIT_TRANSACTIONS OFF
SET TEXTSIZE 2147483647
SET CONCAT_NULL_YIELDS_NULL ON

Thread Safety

TinyTDS must be used with a connection pool for thread safety. If you use ActiveRecord or the Sequel gem this is done for you. However, if you are using TinyTDS on your own, we recommend using the ConnectionPool gem when using threads:

Please read our thread_test.rb file for details on how we test its usage.

Emoji Support 😍

This is possible using FreeTDS version 0.95 or higher. You must use the use_utf16 login option or add the following config to your freetds.conf in either the global section or a specfic dataserver. If you are on Windows, the default location for your conf file will be in C:\Sites.

[global]
  use utf-16 = true

The default is true and since FreeTDS v1.0 would do this as well.

Compiling Gems for Windows

For the convenience of Windows users, TinyTDS ships pre-compiled gems for supported versions of Ruby on Windows. In order to generate these gems, rake-compiler-dock is used. This project provides several Docker images with rvm, cross-compilers and a number of different target versions of Ruby.

Run the following rake task to compile the gems for Windows. This will check the availability of Docker (and boot2docker on Windows or OS-X) and will give some advice for download and installation. When docker is running, it will download the docker image (once-only) and start the build:

$ rake gem:windows

The compiled gems will exist in ./pkg directory.

Development & Testing

First, clone the repo using the command line or your Git GUI of choice.

$ git clone git@github.com:rails-sqlserver/tiny_tds.git

After that, the quickest way to get setup for development is to use Docker. Assuming you have downloaded docker for your platform, you can use docker-compose to run the necessary containers for testing.

$ docker-compose up -d

This will download our SQL Server for Linux Docker image based from microsoft/mssql-server-linux/. Our image already has the [tinytdstest] DB and tinytds users created. This will also download a toxiproxy Docker image which we can use to simulate network failures for tests. Basically, it does the following.

$ docker network create main-network
$ docker pull metaskills/mssql-server-linux-tinytds
$ docker run -p 1433:1433 -d --name sqlserver --network main-network metaskills/mssql-server-linux-tinytds
$ docker pull shopify/toxiproxy
$ docker run -p 8474:8474 -p 1234:1234 -d --name toxiproxy --network main-network shopify/toxiproxy

If you are using your own database. Make sure to run these SQL commands as SA to get the test database and user installed.

CREATE DATABASE [tinytdstest];
CREATE LOGIN [tinytds] WITH PASSWORD = '', CHECK_POLICY = OFF, DEFAULT_DATABASE = [tinytdstest];
USE [tinytdstest];
CREATE USER [tinytds] FOR LOGIN [tinytds];
EXEC sp_addrolemember N'db_owner', N'tinytds';

From here you can build and run tests against an installed version of FreeTDS.

$ bundle install
$ bundle exec rake

Examples us using enviornment variables to customize the test task.

$ rake TINYTDS_UNIT_DATASERVER=mydbserver
$ rake TINYTDS_UNIT_DATASERVER=mydbserver TINYTDS_SCHEMA=sqlserver_2008
$ rake TINYTDS_UNIT_HOST=mydb.host.net TINYTDS_SCHEMA=sqlserver_azure
$ rake TINYTDS_UNIT_HOST=mydb.host.net TINYTDS_UNIT_PORT=5000 TINYTDS_SCHEMA=sybase_ase

Docker Builds

If you use a multi stage Docker build to assemble your gems in one phase and then copy your app and gems into another, lighter, container without build tools you will need to make sure you tell the OS how to find dependencies for TinyTDS.

After you have built and installed FreeTDS it will normally place library files in /usr/local/lib. When TinyTDS builds native extensions, it already knows to look here but if you copy your app to a new container that link will be broken.

Set the LD_LIBRARY_PATH environment variable export LD_LIBRARY_PATH=/usr/local/lib:${LD_LIBRARY_PATH} and run ldconfig. If you run ldd tiny_tds.so you should not see any broken links. Make sure you also copied in the library dependencies from your build container with a command like COPY --from=builder /usr/local/lib /usr/local/lib.

Help & Support

About Me

My name is Ken Collins and I currently maintain the SQL Server adapter for ActiveRecord and wrote this library as my first cut into learning Ruby C extensions. Hopefully it will help promote the power of Ruby and the Rails framework to those that have not yet discovered it. My blog is metaskills.net and I can be found on twitter as @metaskills. Enjoy!

Special Thanks

License

TinyTDS is Copyright (c) 2010-2015 Ken Collins, ken@metaskills.net and Will Bond (Veracross LLC) wbond@breuer.com. It is distributed under the MIT license. Windows binaries contain pre-compiled versions of FreeTDS http://www.freetds.org/ which is licensed under the GNU LGPL license at http://www.gnu.org/licenses/lgpl-2.0.html


Author: rails-sqlserver
Source code: https://github.com/rails-sqlserver/tiny_tds
License:

#ruby   #ruby-on-rails