Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 多对多关系的条令约束_Php_Symfony_Orm_Doctrine Orm_Entity - Fatal编程技术网

Php 多对多关系的条令约束

Php 多对多关系的条令约束,php,symfony,orm,doctrine-orm,entity,Php,Symfony,Orm,Doctrine Orm,Entity,我们有一个实体“用户”,它与不同的“类型”有多对多的关系。MySQL关系通过交叉表处理 类用户{ /** *@ORM\ManyToMany(targetEntity=“App\Entity\Type”) *@ORM\JoinTable(name=“user\u type”, *joinColumns={@ORM\JoinColumn(name=“user\u id”,referencedColumnName=“id”)}, *inverseJoinColumns={@ORM\JoinColumn

我们有一个实体“用户”,它与不同的“类型”有多对多的关系。MySQL关系通过交叉表处理

类用户{
/**
*@ORM\ManyToMany(targetEntity=“App\Entity\Type”)
*@ORM\JoinTable(name=“user\u type”,
*joinColumns={@ORM\JoinColumn(name=“user\u id”,referencedColumnName=“id”)},
*inverseJoinColumns={@ORM\JoinColumn(name=“type\u id”,referencedColumnName=“id”)}
* )
*/
受保护的类型;
}
类类型{
/**
*@ORM\ManyToMany(targetEntity=“App\Entity\User”)
*@ORM\JoinTable(name=“user\u type”,
*joinColumns={@ORM\JoinColumn(name=“type\u id”,referencedColumnName=“id”)},
*inverseJoinColumns={@ORM\JoinColumn(name=“user\u id”,referencedColumnName=“id”)}
* )
*/
受保护的用户;
/**
*@ORM\Column(type=“datetime”)
*/
受保护的$DAT;
}
如何将这种多对多关系中的响应限制为仅显示已发布的类型?

也就是说,SQL应该包括一个
,其中
,用于过滤尚未发布的相应项。我可以用注释之类的东西来做吗


我知道我可以通过过滤返回的集合来做到这一点。但是还有比这更有效的吗?

这个问题有点复杂。 答案如下:

其中一条评论指出,这会导致多对多关系出现错误。从第2.5条开始,情况不再如此

因此,在原则中,当您请求关系的实体时,您可以移交查询条件:

因此,您不会更改注释,而是更改实体的getter:


类用户{
/**
*@ORM\ManyToMany(targetEntity=“App\Entity\Type”)
*@ORM\JoinTable(name=“user\u type”,
*joinColumns={@ORM\JoinColumn(name=“user\u id”,referencedColumnName=“id”)},
*inverseJoinColumns={@ORM\JoinColumn(name=“type\u id”,referencedColumnName=“id”)}
* )
*/
受保护的类型;
公共函数getTypes()
{
$criteria=criteria::create()->其中(criteria::expr()->lte('publishedAt',date('Y-m-d'));
返回$this->types->matching($criteria);
}
}

这将导致延迟获取(取决于您的设置)所需的项目。所有的魔法仍然有效,比如缓存之类的。因此,只有在尚未获取集合的情况下,才会获取集合…

这个问题有点复杂。 答案如下:

其中一条评论指出,这会导致多对多关系出现错误。从第2.5条开始,情况不再如此

因此,在原则中,当您请求关系的实体时,您可以移交查询条件:

因此,您不会更改注释,而是更改实体的getter:


类用户{
/**
*@ORM\ManyToMany(targetEntity=“App\Entity\Type”)
*@ORM\JoinTable(name=“user\u type”,
*joinColumns={@ORM\JoinColumn(name=“user\u id”,referencedColumnName=“id”)},
*inverseJoinColumns={@ORM\JoinColumn(name=“type\u id”,referencedColumnName=“id”)}
* )
*/
受保护的类型;
公共函数getTypes()
{
$criteria=criteria::create()->其中(criteria::expr()->lte('publishedAt',date('Y-m-d'));
返回$this->types->matching($criteria);
}
}

这将导致延迟获取(取决于您的设置)所需的项目。所有的魔法仍然有效,比如缓存之类的。因此,只有在尚未获取集合的情况下才会获取集合…

您可以使用
标准。
向名为eg
getPublishedTypes
User
类添加一个函数

public function getPublishedTypes()
{
    return $this->getTypes()->matching(TypeRepository::createPublishedCriteria());
}
并在
TypeRepository
中添加函数
createPublishedCriteria

static public function createPublishedCriteria()
{
    return Criteria::create()
        ->andWhere(Criteria::expr()->lte('publishedAt', date('Y-m-d')); //your logic
}
注意:功能必须是静态的

您也可以稍后在查询生成器中使用该条件,并使用以下行
$qb->addCriteria(self::createPublishedCriteria())


另一个实践不佳的解决方案可能是集合筛选。在用户类中添加:

public function getPublishedTypes()
{
    return $this->getTypes()->filter(function(Type $type) {
        return $type->getPublishedAt < date('Y-m-d');
}
getPublishedTypes()公共函数
{
返回$this->getTypes()->filter(函数类型$Type){
返回$type->getPublishedAt

这个版本不是很好,因为它会产生更多的查询(数据库中的大数据很糟糕)。

您可以使用
标准。
向名为eg
getPublishedTypes
User
类添加一个函数

public function getPublishedTypes()
{
    return $this->getTypes()->matching(TypeRepository::createPublishedCriteria());
}
并在
TypeRepository
中添加函数
createPublishedCriteria

static public function createPublishedCriteria()
{
    return Criteria::create()
        ->andWhere(Criteria::expr()->lte('publishedAt', date('Y-m-d')); //your logic
}
注意:功能必须是静态的

您也可以稍后在查询生成器中使用该条件,并使用以下行
$qb->addCriteria(self::createPublishedCriteria())


另一个实践不佳的解决方案可能是集合筛选。添加到用户类中:

public function getPublishedTypes()
{
    return $this->getTypes()->filter(function(Type $type) {
        return $type->getPublishedAt < date('Y-m-d');
}
getPublishedTypes()公共函数
{
返回$this->getTypes()->filter(函数类型$Type){
返回$type->getPublishedAt
这个版本不是很好,因为它会产生更多的查询(对于数据库中的大数据来说很糟糕)