Welcome Guest, Not a member yet? Register   Sign In
"Hidden" Fields for Models
#4

(This post was last modified: 06-30-2022, 08:05 AM by b126.)

(06-14-2022, 08:56 AM)christianitis Wrote: Supposing I have a database model representing a user. If I set up a REST controller for the User model, I don't want the password field to be returned when it is queried. Similarly, I don't want the password to be displayed in a HTML page if I need to use it in a template.
Although I'm pretty sure there's a way to do this using Entity classes (maybe by using smart getters?), I think this should be a built-in feature for Models.

Personally, I am using JMS Serializer which makes it really easy to select the fields you want to expose or to exclude.
I do it thru simple annotations in my Doctrine entities.

In the following example, all the fields will be excluded but the ones with @Serializer\Expose()

PHP Code:
/**
* Product
*
* @ORM\Table(name="PRODUCT")
* @ORM\Entity
* @Serializer\ExclusionPolicy(policy="all")
*/
class Product
{
    /**
    * @var string
    *
    * @ORM\Column(name="CODE", type="string", length=10, nullable=false)
    * @Serializer\Expose()
    */
    private $code;

    /**
    * @var string
    *
    * @ORM\Column(name="DESCRIPTION", type="string", length=50, nullable=false)
    * @Serializer\Expose()
    */
    private $description;

    /**
    * @var string|null
    *
    * @ORM\Column(name="SECRETMARGIN", type="string", length=250, nullable=true)
    */
    private $secretMargin
Reply


Messages In This Thread
"Hidden" Fields for Models - by christianitis - 06-14-2022, 08:56 AM
RE: "Hidden" Fields for Models - by iRedds - 06-15-2022, 12:45 AM
RE: "Hidden" Fields for Models - by MGatner - 06-16-2022, 04:39 AM
RE: "Hidden" Fields for Models - by b126 - 06-30-2022, 08:05 AM
RE: "Hidden" Fields for Models - by kilishan - 06-30-2022, 10:44 AM



Theme © iAndrew 2016 - Forum software by © MyBB