Doctrine 1
  1. Doctrine 1
  2. DC-90

Proposal: add Doctrine_Record::setColumnOption() method (diff included)

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.2.0-ALPHA1
    • Fix Version/s: 1.2.0-ALPHA3
    • Component/s: Record
    • Labels:
      None

      Description

      We are generating our entity classes from the database. By doing so, we get a nicely separated set of base record classes and derived record classes where we can implement our logic. The case is, that we want to add validators to about every field there is. The only way that we can do that right now, is by overriding the setUp() method, calling parent::setUp() and providing new column definitions using addColumn(). By doing so, we do have to include the options that were so nicely automatically determined from the database. If we now update our schema in the db and regenerate the base record classes, we might have to manually update addColumn() calls in derived records.

      To make it possible to fully separate the automatic and manual code, we were looking for a method to tweak an already existing table, but we couldn't find such method. Therefore, we implemented one ourselves.

      A practial usage example. This is what we had to write in the derived class (not a validator example, but the idea is the same of course: tweak or add a column option):

          // Override the "info" column definition. We want this field to be
          // an object field, so we can store a serialized object in it.
          $this->hasColumn('info', 'object', null, array(
            'type' => 'object',
            'fixed' => false,
            'unsigned' => false,
            'notnull' => true,
            'primary' => false,
          ));
      

      This is what we write now:

          $this->setColumnOption('info', 'type', 'object');
      

      Please, consider adding the proposed change from the attached diff file. Of course, also let me know if there is a better way to handle this.

        Issue Links

          Activity

          Hide
          Maurice Makaay added a comment -

          Diff for adding a setColumnOption() method to Doctrine_Record.

          Show
          Maurice Makaay added a comment - Diff for adding a setColumnOption() method to Doctrine_Record.
          Hide
          Maurice Makaay added a comment -

          A collegue added another method for batch-setting options in an easy way. It would be nice if this one could also be added.

              /**
               * setColumnOptions
               * sets options for existing column definitions
               *
               * @param array $options          array of arrays with
               *                                ($name, $option, $value) combinations
               */
              public function setColumnOptions($options) {
                foreach($options as $optionset)
                {
                  $this->_table->setColumnOption($optionset[0], $optionset[1], $optionset[2]);
                }
              }
          

          Example end-product code:

          class Address extends BaseAddress
          {
            public function setTableDefinition()
            {
              parent::setTableDefinition();
              $this->setColumnOptions(array(
                array('kind_of_address', 'Yoda_Doctrine_Validator_StringLength', array(3, 10)),
                array('street', 'Yoda_Doctrine_Validator_StringLength', array(3, 45)),
                array('street_short', 'Yoda_Doctrine_Validator_StringLength', array(3, 24)),
                array('housenumber', 'Yoda_Doctrine_Validator_Housenumber', true),
                array('housenumber_extension', 'Yoda_Doctrine_Validator_StringLength', array(0, 25)),
                array('postcode', 'Yoda_Doctrine_Validator_StringLength', array(3, 6)),
                array('postcode', 'Yoda_Doctrine_Validator_Postcode', true),
                array('city', 'Yoda_Doctrine_Validator_StringLength', array(3, 24)),
                array('province', 'Yoda_Doctrine_Validator_StringLength', array(3, 25)),
                array('country', 'Yoda_Doctrine_Validator_StringLength', array(3, 25)),
                array('status',  'Yoda_Doctrine_Validator_StringLength', array(3, 30)),
              ));
            }
          }
          
          Show
          Maurice Makaay added a comment - A collegue added another method for batch-setting options in an easy way. It would be nice if this one could also be added. /** * setColumnOptions * sets options for existing column definitions * * @param array $options array of arrays with * ($name, $option, $value) combinations */ public function setColumnOptions($options) { foreach($options as $optionset) { $ this ->_table->setColumnOption($optionset[0], $optionset[1], $optionset[2]); } } Example end-product code: class Address extends BaseAddress { public function setTableDefinition() { parent::setTableDefinition(); $ this ->setColumnOptions(array( array('kind_of_address', 'Yoda_Doctrine_Validator_StringLength', array(3, 10)), array('street', 'Yoda_Doctrine_Validator_StringLength', array(3, 45)), array('street_short', 'Yoda_Doctrine_Validator_StringLength', array(3, 24)), array('housenumber', 'Yoda_Doctrine_Validator_Housenumber', true ), array('housenumber_extension', 'Yoda_Doctrine_Validator_StringLength', array(0, 25)), array('postcode', 'Yoda_Doctrine_Validator_StringLength', array(3, 6)), array('postcode', 'Yoda_Doctrine_Validator_Postcode', true ), array('city', 'Yoda_Doctrine_Validator_StringLength', array(3, 24)), array('province', 'Yoda_Doctrine_Validator_StringLength', array(3, 25)), array('country', 'Yoda_Doctrine_Validator_StringLength', array(3, 25)), array('status', 'Yoda_Doctrine_Validator_StringLength', array(3, 30)), )); } }

            People

            • Assignee:
              Jonathan H. Wage
              Reporter:
              Maurice Makaay
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: