Skip to content

Feat: Use mapped attributes as property object #26

Closed

Description

@neznaika0

Additional context
I'm trying to use strict entities in the project and restrict calling mapped attributes:
$u->name is not allowed, $u->username is allowed if $datamap['name'] = 'username' .

The library currently uses the old version of $u->{$attribute}.
I understand it doesn't make sense for a framework. Can you think of something for my case? Given the requirement not to call mapped attributes as object properties.
$u->mapProperty(...) is a method, but it is protected.

Any ideas?

Activity

  1. neznaika0 commented on Mar 9, 2026

    @neznaika0
    ContributorAuthor

    I additionally installed your new "relations" package. It has the same behavior. It seems you are adding dynamic properties in the process, I have forbidden it.

       // custom entity check
    
        /**
         * Is the attribute available for work?
         */
        private function hasAttribute(string $attribute): bool
        {
            return array_key_exists($attribute, $this->attributes);
        }
    
       // PostModel - table `post`  (other  TagModel - table `tag`)
        public function tags(): Relation
        {
            return $this->belongsToMany(TagModel::class, 'post_tag', 'post_id', 'tag_id', 'id', 'id');
        }

    EntityException: Attribute "__pivot_parent_id" of "Tag" not exist.

  2. michalsn commented on Mar 9, 2026

    @michalsn
    Owner

    I have no intentions of changing the default framework behavior.

    If you encounter issues with the new "relations" package, please report them in its repository and include the full context (migrations, relations definitions) so the problem can be reproduced easily.

  3. neznaika0 commented on Mar 9, 2026

    @neznaika0
    ContributorAuthor

    Yes, I mentioned that.

    I understand it doesn't make sense for a framework.

    I found code. It sets the dynamic properties of objects (if the result is not an array). To check, I added these attributes, it seems to work. But that doesn't suit me - extra properties in the object.

    https://github.com/michalsn/codeigniter-relations/blob/df2a28b9174a2ee18611d97d980c7d9c74b839dd/src/Relations/BelongsToMany.php#L268-L277

    To better understand my problem, imagine that all properties (not attributes) are these are the real private properties of the object and you cannot set new ones.

  4. neznaika0 commented on Mar 9, 2026

    @neznaika0
    ContributorAuthor

    @michalsn it doesn't matter where the issue is open, do we have to write in both repositories? If you don't have an answer or time, then let's close it. I'll have to write my own solution.

  5. michalsn commented on Mar 9, 2026

    @michalsn
    Owner

    What would really help here is a minimal reproducible example (migrations, models, entities, and a usage example) that I can copy and run locally. That would save me a lot of time trying to understand the exact scenario you're describing.

    The last time I worked on this library was in early January, and without a runnable example, it's very difficult for me to follow the problem from snippets or links alone.

    Also, it does matter where the issue is opened. The nested-model and the relations package are separate projects and are implemented differently, so mixing the discussion across repositories makes it harder to investigate.

    At the moment, the only thing I clearly understand from your post is this:

    I'm trying to use strict entities in the project and restrict calling mapped attributes:
    $u->name is not allowed, $u->username is allowed if $datamap['name'] = 'username' .

    As I mentioned earlier, I have no intention of changing this behavior, as this is how the framework currently handles it. However, if you would like to introduce a config option for this behavior, feel free to open a PR, and I can review it.

  6. neznaika0 commented on Mar 9, 2026

    @neznaika0
    ContributorAuthor

    Considering how I implemented it by extending the basic Entity, I'm not sure if you'll approve of my idea. When I came across this feature (temporary properties from tables) this will have a big BC on other projects where it is allowed (the default class of the framework).

    I can open the blog development repository. He's just in the process 😄 I will try to create a branch for the test. But I think you can also add the desired test locally.

    At this stage, this discussion will be about StrictEntity (for our conversation), and it applies to both of your repositories if we continue the discussion.

  7. neznaika0 commented on Mar 9, 2026

    @neznaika0
    ContributorAuthor

    @michalsn see https://github.com/neznaika0/codeigniter-blog/tree/feat/relations

    Rename the configs inside ./utils/ to run scripts. Delete it .dist in the names, create an .env,, run the tests.

    for some of them to run successfully, uncomment the necessary attributes in the Post, Tag.

  8. michalsn commented on Mar 10, 2026

    @michalsn
    Owner

    You can check PR #27

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions