Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Traits (PHP 5.4) #102

Closed
ruimarinho opened this issue Mar 31, 2012 · 26 comments
Closed

Add support for Traits (PHP 5.4) #102

ruimarinho opened this issue Mar 31, 2012 · 26 comments

Comments

@ruimarinho
Copy link
Contributor

PHP 5.4.0 includes support for Traits and with them, new patterns for "drop in" functionality will likely gain attraction in the following months.

For example, a User could use Traits for mapping social networks profiles, such as Facebook or Twitter. Another interesting use case is to easily include Doctrine behaviors without having to rely on much more complex code. KnpLabs recently blogged about this new way of dealing with behaviors on a blog post.

This issue is a request to include support for mapping metadata when using Traits.

@AlmogBaku
Copy link

I have the same problem

@garak
Copy link

garak commented Sep 29, 2014

👍

@nass600
Copy link

nass600 commented Feb 16, 2015

Same here

@kmonmousseau
Copy link

+1

5 similar comments
@drewclauson
Copy link

+1

@pleerock
Copy link

+1

@floriansemm
Copy link

+1

@giovkanata
Copy link

👍

@cfoehrdes
Copy link

+1

@jamesisaac
Copy link

+1, exposing Doctrine behaviour traits (https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/timestampable.md#traits) involves a load of redundant copy/pasting without trait support in the serializer.

@barduck007
Copy link

+1

1 similar comment
@hhsissi
Copy link

hhsissi commented Jul 20, 2015

+1

@TNAJanssen
Copy link

What is the current status?

@EmmanuelVella
Copy link

+1

1 similar comment
@JulianStier
Copy link

+1

@mikelohmann
Copy link

+1

As a workaround I created a field in the to be serialized class, annotated it wit "@expose" and just before serializing I copied the values from the field in the trait to that one. That works for me. Ugly, but working.

// source is the field in the trait
// serializableSourceCopy it the field in the main class which is only used for this purpose.
$doc->setSerilizableSourceCopy($doc->getSource());
$this->serializer->serialize($doc, 'json');

@sdecandelario
Copy link

+1

Anyone find a solution for this?

@drewclauson
Copy link

I'm not having any problems with fields on traits being serialized, just using the Doctrine\ORM\Mapping and JMS\Serializer\Annotation annotations and it works for me.

@liverbool
Copy link
Contributor

+1

@choco-julie
Copy link

Traits methods aren't available after deserialization

@IndyIndyIndy
Copy link

+1

1 similar comment
@vinaocruz
Copy link

+1

@stepansib
Copy link

+1
Still cant serialize properties defined in traits...

@atournayre
Copy link

I found a solution (for me)

Before, my code was this :

<?php

namespace App\Doctrine\Traits;

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\Groups;

trait IdTrait
{
    /**
     * Id.
     *
     * @Groups({"elastica"})
     * @var int
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }
}

Working code :

<?php

namespace App\Doctrine\Traits;

use Doctrine\ORM\Mapping as ORM;

/**
 * Class IdTrait.
 */
trait IdTrait
{
    /**
     * Id.
     *
     * @JMS\Serializer\Annotation\Groups({"elastica"})
     * @var int
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }
}

@spackmat
Copy link

I have the exact opposite problem. I am using the Knp\DoctrineBehaviors\Model\Blameable\Blameable Trait and this serializes the whole user objects with all roles, passwords and really everything and in general, I cannot control the MaxDepth() of properties implemented in traits with Annotations.

When I set @ExclusionPolicy("all") on my User entity extending the BaseUser of FOSUserBundle, the problem from #100 kicks in. So for now, I cannot get rid of the User internals like passworthashes in all Entities that use the Bameable Trait. That means nearly all my Entities.

How do others handle this? Combining knplabs/doctrine-behaviors and friendsofsymfony/user-bundle should not be the rarest case.

@goetas
Copy link
Collaborator

goetas commented Apr 28, 2017

Not planning to "add" some special "trait" support since the traits are already working as they are.

When using traits there are some scenarios:

  • using annotations and traits are "yours": you can do everything you need/want since annotations from traits are already parsed.
  • using annotations and traits are third-party: use YAML or XML for your metadata to expose/exclude/customize the serialization of the class that uses the trait
  • using YAML or XML: congratulations, everything should work as expected.

When using YAML or XML mapping info for a trait should be placed on the class-metadata file that "uses" the trait.

Example:

trait PhysicalAddress
{
    public $city;
}

class Address
{
    use PhysicalAddress;
    public $firstName;
    public $lastName;
}

Metadata for Address class have to be inside Address.yml

Address:
  properties:
    firstName:
      expose: true
    lastName:
      exclude: true
    city: # this property is coming from the trait
      exclude: true

@goetas goetas closed this as completed Apr 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests