Symfony2 Doctrine是否支持"私有/隐藏"实体字段?

mat*_*e64 2 php doctrine mongodb symfony doctrine-orm

我在寻找一个功能,从文档删除字段自动神奇.

让我们说,我有一个User Document可以用RESTful api匿名查询的.当然,我想删除危险的领域,如passwordsecret

文献:

// 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;
}
Run Code Online (Sandbox Code Playgroud)

控制器:

// 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)
    }
}
Run Code Online (Sandbox Code Playgroud)

Ale*_* B. 5

看看jms序列化器及其排除策略

/**
 * 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;
}
Run Code Online (Sandbox Code Playgroud)