简体   繁体   中英

Symfony2 Does Doctrine support “private/hidden” entity fields?

I'm looking for a function, to remove fields from a Document in auto-magically .

Let's say, I have a User Document that can be queried anonymously with a RESTful api. Of course, I want to remove dangerous fields , such as password or secret etc.

Document:

// src/Acme/StoreBundle/Document/User.php
namespace Acme\StoreBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 * @MongoDB\Document
 */
class Product
{
    /**
     * @MongoDB\Id
     */
    protected $id;

    /**
     * @MongoDB\String
     */
    protected $name;

    /**
     * @MongoDB\Float
     * @Hidden            // This field is "private"
     */
    protected $password;
}

Controller:

// src/Acme/StoreBundle/Controller/UserController.php
namespace Acme\StoreBundle\Controller;

class UserController extends RestController
{
    public function putUserAction(Request $request)
    {
        ...
        // Get the user by the username
        $user = $userManager->findUserByUsername('joe_schmoe');

        $user->removeHiddenFields(); // Just an example implementation

        ...
        // Returns the user object as JSON (I know how to do that, JFYI)
    }
}

Look at jms serializer and its exclusion strategies

/**
 * The following annotations tells the serializer to skip all properties which
 * have not marked with @Expose.
 *
 * @ExclusionPolicy("all")
 */
class MyObject
{
    private $foo;
    private $bar;

    /**
     * @Expose
     */
    private $name;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM