Doctrine 1
  1. Doctrine 1
  2. DC-364

Adding the fieldName to mutator/accessor methods

    Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.2.1
    • Fix Version/s: 1.2.3
    • Component/s: Record
    • Labels:
      None

      Description

      Would it be possible to add the fieldName to the function variables when a mutator or accessor function is called. This would allow for more generalized accessor / mutator functions that can be reused easily.

      example:

      public function mutatorFunction($value, $load, $fieldName)
      {
      $this->_set($fieldName, someValidationFunction($value));
      }

      Thank you

        Activity

        Hide
        Jonathan H. Wage added a comment -

        I don't understand the issue. Can you please provide more valid information, detailed descriptions, sample code, test cases, etc.

        Show
        Jonathan H. Wage added a comment - I don't understand the issue. Can you please provide more valid information, detailed descriptions, sample code, test cases, etc.
        Hide
        Andrew Coulton added a comment -

        I believe the issue is that there may be logic common to mutators across a whole record (or indeed a set of records). A classic example would be date fields. Doctrine uses the ISO 8601 format for dates entirely, but often we want a date field to be accessible directly as a DateTime object or a UNIX timestamp to allow use in date formatting functions without additional strtotime overhead. Or we might want the application to automatically convert a user-provided date from a locale based format to ISO 8601.

        The current pattern requires a custom accessor/mutator to each field - therefore a record with three date fields would have three accessors and mutators. I believe the OP's suggestion is that the fieldName be added as a parameter to the call to the accessor/mutator so that a single accessor/mutator pair could serve multiple fields. Indeed, we could then write a behaviour that includes the accessor/mutator and registers it on all date fields, or whatever other recurring field type/name/etc we have in the application.

        As PHP will allow a function to be called with additional arguments, I believe all that's needed is to add the parameter to the calls in Doctrine_Record::set and Doctrine_Record::get as per the attached patch. Mutators/Accessors that care would then be able to access the field name as a second parameter, but without breaking any existing code.

        Show
        Andrew Coulton added a comment - I believe the issue is that there may be logic common to mutators across a whole record (or indeed a set of records). A classic example would be date fields. Doctrine uses the ISO 8601 format for dates entirely, but often we want a date field to be accessible directly as a DateTime object or a UNIX timestamp to allow use in date formatting functions without additional strtotime overhead. Or we might want the application to automatically convert a user-provided date from a locale based format to ISO 8601. The current pattern requires a custom accessor/mutator to each field - therefore a record with three date fields would have three accessors and mutators. I believe the OP's suggestion is that the fieldName be added as a parameter to the call to the accessor/mutator so that a single accessor/mutator pair could serve multiple fields. Indeed, we could then write a behaviour that includes the accessor/mutator and registers it on all date fields, or whatever other recurring field type/name/etc we have in the application. As PHP will allow a function to be called with additional arguments, I believe all that's needed is to add the parameter to the calls in Doctrine_Record::set and Doctrine_Record::get as per the attached patch. Mutators/Accessors that care would then be able to access the field name as a second parameter, but without breaking any existing code.
        Hide
        Andrew Coulton added a comment -

        Suggested patch to Doctrine_Record to implement this improvement

        Show
        Andrew Coulton added a comment - Suggested patch to Doctrine_Record to implement this improvement
        Hide
        Carl Alexander added a comment -

        Sorry meant to get back to you about this sooner.

        What Andrew has described is exactly the issue I am having and his suggested patch is what I would hope to see implemented.

        Show
        Carl Alexander added a comment - Sorry meant to get back to you about this sooner. What Andrew has described is exactly the issue I am having and his suggested patch is what I would hope to see implemented.

          People

          • Assignee:
            Jonathan H. Wage
            Reporter:
            Carl Alexander
          • Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: