Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.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 Doctrine2是否为多个关系获取填充类型?_Php_Doctrine Orm_Zend Framework2 - Fatal编程技术网

Php Doctrine2是否为多个关系获取填充类型?

Php Doctrine2是否为多个关系获取填充类型?,php,doctrine-orm,zend-framework2,Php,Doctrine Orm,Zend Framework2,我不确定如何让所有类型的酒店“至少有一家”酒店?因为这种关系有很多种 因此,使用queryBuilder,我假设我可以执行以下操作: $qb = $this->em->createQueryBuilder(); $qb->select('t') ->from(HotelType::class, 't', 't.id') ->join('t.hotels', 'm'); 但这似乎不起作用?我不确定我必须做什么。以下是我的两个实体: HotelT

我不确定如何让所有类型的酒店“至少有一家”酒店?因为这种关系有很多种

因此,使用queryBuilder,我假设我可以执行以下操作:

$qb = $this->em->createQueryBuilder();

$qb->select('t')
     ->from(HotelType::class, 't', 't.id')
     ->join('t.hotels', 'm');
但这似乎不起作用?我不确定我必须做什么。以下是我的两个实体:

HotelType实体:

use Doctrine\ORM\Mapping as ORM;

/**
 * HotelType
 *
 * @ORM\Table(name="hotel_type")
 * @ORM\Entity
 */
class HotelType
{
    /**
     * @var string
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\ManyToMany(targetEntity="Hotel", cascade={"persist"}, mappedBy="mType")
     */
    private $hotels;
}
酒店实体:

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Hotel
 *
 * @ORM\Table(name="hotel")
 * @ORM\Entity(repositoryClass="Application\Repository\HotelRepository")
 */
class Hotel
{
    /**
     * @var string
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToMany(targetEntity="HotelType", inversedBy="hotels", cascade={"persist"})
     * @ORM\JoinTable(name="hotel_hoteltype",
     * joinColumns={@ORM\JoinColumn(name="hotel_id", referencedColumnName="id")},
     * inverseJoinColumns={@ORM\JoinColumn(name="hoteltype_id", referencedColumnName="id")}
     * )
     */
    private $mType;

    public function __construct()
    {
        $this->mType = new ArrayCollection();
    }

    public function getMType()
    {
        return $this->mType;
    }

    public function addMType($mtype)
    {
        if (!$this->mType->contains($mtype) && is_object($mtype)) {
            $this->mType->add($mtype);
        }

        return $this;
    }

    public function removeMType($mtype)
    {

        if ($this->mType->contains($mtype) && is_object($mtype)) {
            $this->mType->removeElement($mtype);
        }

        return $this;
    }

    public function clearMType()
    {
        $this->mType->clear();

        return $this;
    }
}

我觉得不错。有什么错误吗?您确定没有忘记
->getQuery()->getResult()
?您的实体是否有命名空间?
。似乎不起作用
是否引发错误?什么也不退?对其他实体的查询有效吗?嗯,在其他地方似乎是个问题,现在都有效了(它本应该给出更多结果的时候却给出了0个结果…现在有效了)