我正在寻找一个函数,从文档中删除字段 doctrine 神奇地自动。
假设我有一个可以使用 RESTful API 匿名查询的用户文档
。当然,我想删除危险字段,例如密码
或 secret
等。
文档:
// 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)
}
}
最佳答案
看看jms serializer及其 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;
}
关于php - Symfony2 Doctrine 支持 "private/hidden"实体字段吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20405510/