runopencode/dm-related-selects-plugin 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

runopencode/dm-related-selects-plugin

Composer 安装命令:

composer require runopencode/dm-related-selects-plugin

包简介

dmRelatedSelectsPlugin - Related selects form widgets

README 文档

README

Author: TheCelavi
Version: 0.5.0
Stability: Alpha (testing and improving)
Date: December 5th, 2012
Courtesy of Run Open Code
License: Free for all

Description

In the situation when you have one or several entities related as one(zero)-to-many, per example:

Category -> Sub-category -> Product

and entity such as, per example, PurchasedItem where PurchasedItem is in one-to-one relation with Product the user input of the one Product can be quite hard to find the right one among several dozens, hundreds, etc. of it.

There for, these kind of use case scenarios are solved with filtering with related SELECTS.

Use case scenario can be explained as:

  • User choose Category of the product from Category SELECT field
  • The Sub-category SELECT field get filled with Sub-categories of selected Category
  • User choose Sub-category of the product
  • The SELECT field for Products get filled with products of selected Category and Sub-category

NOTE: this is just illustrative example what kind of problem this plugin solves.

How to use it?

The plugin contains the widget form class sfWidgetFormDoctrineRelatedSelect which extends sfWidgetFormDoctrineChoice. It also has additional two javascript files:

  • jquery.relatedselects.js
  • launch.js

These two files are required, so have in mind that if you are using this form widget in your front components to add a following code:

public function executeExample(dmWebRequest $request)
{
    $this->form = new MyFormWithRelatedSelects();
    // IMPORTANT: Adding JSs from FORM to response!
    foreach ($jscripts = $this->form->getJavaScripts() as $js) {
        $this->getResponse()->addJavaScript($js);
    }
	....
}

Note as well that jquery.relatedselects.js is based on jQuery related selects plugin but the code is modified to suit this plugin.

When you have the plugin installed, in your forms it is required just to add instead of plain selects the sfWidgetFormDoctrineRelatedSelect.

Configuration

The widget extends the sfWidgetFormDoctrineChoice so it uses some of the options from it:

  • model: The model from witch SELECTS are populated
  • key_method: The method from which OPTIONS get value attribute. Default is getPrimaryKey
  • method: The method from which OPTIONS get labels. Default is __toString
  • table_method: The SELECTS are populated based on the provided 'model' option. However, you can create specific table method in table class of the model and that method can be used.

sfWidgetFormDoctrineRelatedSelect uses options stated above and extends them with few more options as well.

Required options are:

  • group: Related selects are grouped. The name of the group is not as much important as the fact that the group name is distinct. So for the related selects, the group name must be the same and distinct from the other groups.
  • index: Each field has its index. The top most has index with value 0. The next one has index value 1 and so on. Note that index has to be zero based, increased by one (per example: 0, 1, 2...).

Optional options are:

  • parent: If the related select has parent class (per example: Product is within ProductCategory) the model has to be stated so the filtering can take place.
  • active_only: When fetching objects, if active_only is true the generated query will have a condition WHERE is_active = true

i18N support

Yes! This plugin has i18n support.

Example

Lets say we have a following entities:

Category:
  columns:
	title: {type: string(255)}
	
SubCategory:
  columns:
    title: {type: string(255)}
    category_id: {type: integer, notnull: true}
  relations:
    Category:
      class: Category
	  local: category_id
	  foreignAlias: SubCategories

Product:
  columns:
    title: {type: string(255)}
    sub_category_id: {type: integer, notnull: true}
  relations:
    SubCategory:
      class: SubCategory
	  local: sub_category_id
	  foreignAlias: Products

PurchasedItem:
  columns:
    title: {type: string(255)}
    product_id: {type: integer, notnull: true}
  relations:
    Product:
      class: Product
	  local: product_id
	  foreignAlias: PurchasedItems

So you want to create a form to create/edit a PurchasedItem? Purchased item would use one SELECT control with the list of products. If you have a few hundreds of various products the selection of right product is hard.

So, this can be solved by filtering products using related selects:

class MyPurchasedItemForm extends dmForm
{
    public function configure()
    {
        $this->widgetSchema['category'] = new sfWidgetFormDoctrineRelatedSelect(array(
            'add_empty' => 'Choose category',
            'model' => 'Category',
            'group' => 'my_group',
            'index' => 0,
            'active_only' => true
        ));
        $this->validatorSchema['category'] = new sfValidatorDoctrineChoice(array(
            'required' => false,
            'model' => 'Category'
        ));

		$this->widgetSchema['sub_category'] = new sfWidgetFormDoctrineRelatedSelect(array(
            'add_empty' => 'Choose sub category',
            'model' => 'SubCategory',
            'group' => 'my_group',
            'index' => 1,
            'active_only' => true,
			'parent' => 'Category'
        ));
        $this->validatorSchema['sub_category'] = new sfValidatorDoctrineChoice(array(
            'required' => false,
            'model' => 'SubCategory'
        ));
    
        $this->widgetSchema['product'] = new sfWidgetFormDoctrineRelatedSelect(array(
            'add_empty' => 'Choose product',
            'model' => 'Product',
            'group' => 'my_group',
            'index' => 2,
            'active_only' => true,
            'parent' => 'SubCategory'
        ));
        $this->validatorSchema['product'] = new sfValidatorDoctrineChoice(array(
            'required' => false,
            'model' => 'Product'
        ));
		
		// ... Some more code here ...
    
        parent::configure();               
    }    
}

So the example is self-explanatory. Please note index option and the values, as well as who has parent option configured and who does not.

runopencode/dm-related-selects-plugin 适用场景与选型建议

runopencode/dm-related-selects-plugin 是一款 基于 JavaScript 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 1, 最近一次更新时间为 2012 年 12 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「plugin」 「diem」 「diem extended」 「form widget」 「dmRelatedSelectsPlugin」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 runopencode/dm-related-selects-plugin 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 runopencode/dm-related-selects-plugin 我们能提供哪些服务?
定制开发 / 二次开发

基于 runopencode/dm-related-selects-plugin 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 18
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 3
  • Forks: 0
  • 开发语言: JavaScript

其他信息

  • 授权协议: Free
  • 更新时间: 2012-12-05