Code Monkey home page Code Monkey logo

yiiattributex's Introduction

EActiveRecordAx

Author: Cristian Salazar H. [email protected]

Repo: https://github.com/christiansalazar/yiiattributex

Allows a CActiveRecord based class to use extended attributes stored in a blob.

Using this specialized class you can deal with extra attributes not declared in the table scheme, instead, stored in a blob, as follows:

[php]

// Color.php, defined as: class Color extends EActiveRecordEx { ... }
$red = new Color;
$red->name = 'red';
$red->value = '#f00';
// extended attributes, will be stored in a single blob
$red->rgb_notation = 'rgb(255,0,0)';
$red->label = 'This is a red color';
$red->insert(); // inserts a new record, the two extra fields are stored in
		// one single database column named: ax_data (see also seetings)

In this example the extra attributes: 'rgb_notation' and 'label' does not exists in the table schema, this both attributes are treated as regular model attributes with no difference as others persisted in table columns.

In order to setup this extension you're required to create a new table column in your schema (typically a BLOB column), give it a name (by default is named: 'ax_data') and declare it via settings in your config file (see also later).

The Storage Column and Model Attributes

As an example, suppose you have this two extra attributes declared in your config file ('rgb_notation' and 'label'), also, a new column (the storage colum) was created in your table schema (name it 'ax_data'), so, future calls to $model->attributes will return an array containing all your regular table columns and class attributes, plus this two extra attributes also excluding the storage attribute (ax_data).

##Dealing with extra attributes

There is no difference at modeling level with the extra declared attributes, you can use them in any CDataProvider based widget (CGridView, CListColumn,etc) also, the regular usage.

// the 'first_name' is not declared in the table schema, but will persist.
$model->first_name = "Christian";
$model->save();

Also, this works too:

$model = Person::model()->findByPk(123);
echo "First Name is: ".$model->first_name;  // will echo "Christian"

The extra attribute is present when retriving a attribute list:

$list = $model->attributes;   // this list will include 'first_name'

##Setup Instructions

  1. git clone (or download) this extension into your protected/extensions dir.

     git clone https://github.com/christiansalazar/yiiattributex.git
    
  2. edit your config/main.php settings file and put the imports path:

     'import'=>array(
    		'application.models.*',
    		'application.components.*',
    		'application.extensions.yiiattributex.*',   // << HERE
    	),
    
  3. Add a LONGBLOB attribute to your table scheme by using a command similar to:

     "alter table `color` add column `ax_data` longblob;
    
  4. Define the extra attributes in your config/main.php settings file

     'params'=>array(
     	'yiiattributex' => array(
     		// the class name
     		'Color'=>array(
     			// this entry must go first.  ax_data is the attribute name
     			// created in your scheme.
     				'settings' => array('ax_data'),
     			// the extra fields:
     				'rgb_notation'=>array('#000',),
     				'label'=>array('no label yet',),
     		),
     	),
     ),
    
  5. Extend your classes from EActiveRecordAx instead of CActiveRecord.

yiiattributex's People

Contributors

christiansalazar avatar

Watchers

James Cloos avatar

yiiattributex's Issues

Empty attributes values when calling update()

Problem:

When loading a object and immediatly call update() it will destroy all values previously defined because it never initialize after constructor is called.

How to reproduce:

// success:
$object = Some::model()->findByPk(123);
$object->first_name = "Christian";
$object->update();
$object = Some::model()->findByPk(123);
echo $object->first_name; // will echo "Christian"

now, make bug show up:

$object = Some::model()->findByPk(123);
echo $object->first_name; // will echo "Christian"
$object = Some::model()->findByPk(123);
$object->update();
echo $object->first_name; // will echo "" (bug, attributes never loads and save empty values destroying the record values)

The attribute list should be defined in both: config (hardwired mode) or in a database (dynamic mode)

Currently yiiattributex define the extended attributes in a config entry, in a form of indexed array, as follows:

    // file: protected/config/main.php
    'params'=>array(   
        'yiiattributex' => array(
            'Color'=>array(
                    'settings' => array('ax_data'),
                    'first_name'=>array('no name',),
                    'last_name'=>array('no name',),
            ),
        ),
    ),

The idea behind this issue is to provide a way to define the same information in a dynamic database.

Recommend Projects

  • React photo React

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

  • Vue.js photo Vue.js

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

  • Typescript photo Typescript

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

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

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

Recommend Topics

  • javascript

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

  • web

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

  • server

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

  • Machine learning

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

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

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

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

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

  • D3 photo D3

    Data-Driven Documents codes.