Giter Club home page Giter Club logo

enum's Introduction

CakeDC Enum Plugin

Build Status Coverage Total Downloads License

Enumeration list for CakePHP 5.

Versions and branches

CakePHP CakeDC Enum Plugin Tag Notes
^5.0.6 3.next 3.1.0 stable
^4.0 2.next 2.0.4 stable

Install

Using Composer:

composer require cakedc/enum

You then need to load the plugin. You can use the shell command:

bin/cake plugin load CakeDC/Enum

or by manually adding statement shown below to Application::bootstrap() method:

$this->addPlugin('CakeDC/Enum');

Requirements

  • CakePHP 5.0.6+
  • PHP 8.1+

Documentation

For documentation, as well as tutorials, see the Docs directory of this repository.

Support

For bugs and feature requests, please use the issues section of this repository.

Commercial support is also available, contact us for more information.

Contributing

This repository follows the CakeDC Plugin Standard. If you'd like to contribute new features, enhancements or bug fixes to the plugin, please read our Contribution Guidelines for detailed instructions.

License

Copyright 2015 - 2024 Cake Development Corporation (CakeDC). All rights reserved.

Licensed under the MIT License. Redistributions of the source code included in this repository must retain the copyright notice found in each file.

enum's People

Contributors

admad avatar ajibarra avatar arusinowski avatar drmonkeyninja avatar inoas avatar jadb avatar josegonzalez avatar kaihoefler avatar liviakuenzli avatar rafaelqueiroz avatar ravage84 avatar rochamarcelo avatar rogerpro avatar sdevore avatar skie avatar slywalker avatar steinkel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

enum's Issues

Problem with multi worded prefix in ConstStrategy.

If you have constants like NODE_TYPE_PAGE, NODE_TYPE_BLOG etc. and you configure the behavior to use 'const' => 'node_type', the prefix list generated by ConstStratgey::listPrefixes() has NODE instead of NODE_TYPE.

Things get uglier if you have another set of constants like NODE_FOO_BAR and try use list with prefix node_foo and you get MissingEnumStrategyPrefixException.

Error in ConstStrategy while formatig results

I get the following error when I use the const strategy with enum:

Cake\Utility\Hash::get(): Argument #2 ($path) must be of type array|string|int|null, Cake\ORM\Entity given, called in /application/vendor/cakedc/enum/src/Model/Behavior/Strategy/ConstStrategy.php on line 133

The situation i experience is that while iterating through the result set of a query the $row in the beforeFind event already has been formated and is of type Cake\ORM\Entity.

I do not understand how this happens, but it can be fixed by adding the line

if ($constant instanceof Entity) return $row; after line 128 in the class ConstStrategy.

Deprecated Error in Table class at CakePHP3.6

When I tested with PHPUnit during development, Deprecated Error occurred. It seems that Table :: table () has been deprecated since CakePHP 3.6.

Deprecated Error: App\Model\Table\FooTable::table() is deprecated. Use setTable()/getTable() instead. - /app/vendor/k1low/property-enum/src/Controller/Component/AutoSetComponent.php, line: 17
 You can disable deprecation warnings by setting `Error.errorLevel` to `E_ALL & ~E_USER_DEPRECATED` in your config/app.php. in [/app/vendor/cakephp/cakephp/src/Core/functions.php, line 310]

Enums not working with irregular fieldnames

Problem

    const SALUTATION_001 = 'Herr';
    const SALUTATION_002 = 'Frau';
    const SALUTATION_003 = 'Firma';
    const SALUTATION_004 = 'Herr und Frau';
    const SALUTATION_005 = 'Frau und Herr';
    const SALUTATION_006 = 'Frau und Frau';
    const SALUTATION_007 = 'Herr und Herr';
    const SALUTATION_008 = 'Eheleute';
    const SALUTATION_021 = 'Firma mit Ansprechpartner';
    //...
        $this->addBehavior('CakeDC/Enum.Enum', ['lists' => [
            'xml_kundendaten_adresse_anrede' => [
                'strategy' => 'const',
                'prefix' => 'SALUTATION',
                'field' => 'Xml_Kundendaten_Adresse_Anrede',
            ],
        ]]);

Error

Error CakeDC\Enum\Model\Behavior\Exception\MissingEnumConfigurationException

Workaround

        // Note that BOTH have to be set, else it throws exceptions once in _call()  and once in enum()
        $this->addBehavior('CakeDC/Enum.Enum', ['lists' => [
            'xml_kundendaten_adresse_anrede' => [
                'strategy' => 'const',
                'prefix' => 'SALUTATION',
                'field' => 'Xml_Kundendaten_Adresse_Anrede',
            ],
            'Xml_Kundendaten_Adresse_Anrede' => [
                'strategy' => 'const',
                'prefix' => 'SALUTATION',
                'field' => 'Xml_Kundendaten_Adresse_Anrede',
            ],
        ]]);

Question

Is there a reason the list keys are being inflected around inside the behavior?

Enum Helper

Would having an emum helper to pull the enum configuration into a view be useful or is there a better way of doing this?

CakePHP 3.6 Update

Hey :)

I noticed you have a branch ready for 3.6 support, what is missing from it so that it can be released? Can we help?

CakePHP 4 support

Currently this project does not support CakePHP 4.x, is this something you are looking to do?

Problem modifying existing entity with enum and saving it

Hi,
I'm sorry but I have another problem after migration my application which uses Enum form Cake 3 to Cake 5:

I have an Entity e.g. Article which contains an enum attribute visibility.
The enum is defined as follows:

$this->addBehavior('CakeDC/Enum.Enum', ['lists' => [ 'visibility' => [ 'strategy' => 'const', 'prefix' => 'VISIBILITY', 'field' => 'visibility', 'translate' => true ] ]]);

When I read an existing entity and modify it in the controller,

$article = $this->Articles->get($id); $article->count_viewed = $article->count_viewed + 1; $this->Articles->save($article));

the save operation is throwing a TypeError

"array_key_exists(): Argument #1 ($key) must be a valid array offset type"
in ROOT/vendor/cakedc/enum/src/Model/Behavior/EnumBehavior.php at line 299

As far as I see the problem originates by the fact that after reading the visibility attribute is an object(Cake\ORM\Entity) but when I save the value a string is expected.
Because after

$article->visibility = $article->visibility->value;

saving ist possible.

Const Enum Type as integer

I wonder if it would be possible to make the Const Type allow for integers as values. So for example:

class SampleTable extends Table {

    const TYPE_PLATFORM = 0;
    const TYPE_USER = 1;
    const TYPE_DEVICE = 2;

    public function initialize(array $config) {
        parent::initialize($config);

        $this->addBehavior('CakeDC/Enum.Enum', ['lists' => [
            'type' => [
                'strategy' => 'const',
                'prefix' => 'TYPE'
            ]
        ]]);
...

And show PLATFORM, USER and DEVICE in lists but put the integer in the db. I can get this working currently but its a bit hacky. I can add applicationRules => false and having array_flip($this->Samples->enum('type'))

Maybe I need to make a custom strategy? ;-)

Error in ConstStrategy while formatting results

It is working fine in CakePHP3 and enum package version '1.4.0'. I upgraded the project from CakePHP3 to CakePHP4. I am using version 2.1.0 for CakePHP4

I get the following error when I use the const strategy with enum:

ERROR: [InvalidArgumentException] Invalid Parameter { "label": null, "prefix": "TYPE", "value": null }, should be dot separated path or array. in /var/www/html/vendor/cakephp/cakephp/src/Utility/Hash.php on line 65

It can be fixed by adding the below after line 128 in the class ConstStrategy.

if ($constant instanceof Entity) { return $row; }

I think this issue this same as #55. Can you please provide support for v2.1.0.

ConstStrategy::enum() should cache generated lists.

Currently ConstStrategy::enum() does the process of extracting enum list on each call.

Given the use of reflection and multiple iterations of array manipulation and callbacks usage, the whole operation is non trivial and it would be better the lists were cached to avoid the overhead on subsequent requests for the same list.

Error in Branch 3.next-cake5 in ConstStrategy

I used CakeDC/Enum in my project and migrated to CakePHP 5.0 and now have problems with the implementation of the ConstStrategy.

In the branch 3.next-cake5 @arusinowski introduced with commit 0447fec5b1b6eb883b79a257920671f630bb4c74
an changed the following lines:

$query->formatResults(function (CollectionInterface $results) { return $results->map(function ($row) { if (is_string($row) || !$row) { return $row; }
to

$query->formatResults(fn(CollectionInterface $results) => $results ->map(function (EntityInterface $row): EntityInterface {

But as the $row could also be an array if you have a non hydrated result or if you call the exists(ID) function on a modell, this does not work as expected.

This change has not been merged with the master branch.
Therefore the fix #55 is actually not relevant for the master branch but only for the 3.next-cake5 branch.

@arusinowski : Is it possible for you to fix this? Thanks.

Enum translation

Is it possible to use different name for saving and displaying for multilingual purposes?

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.