我有个问题:
我在一个服务中得到一个存储库,然后对它进行findall,问题是:我得到一个空数组(而我的数据库没有)。所以我想我做错了,希望你能启发我。

<?php
/**
 * Created by PhpStorm.
 * User: thocou
 * Date: 07/03/19
 * Time: 15:05
 */

namespace App\Service;

use App\Entity\RentRelease;
use App\Repository\PropertyRepository;
use Doctrine\Common\Persistence\ObjectManager;

class RentReleaseInsertion
{
private $propertyRepository;
private $manager;

public function __construct(PropertyRepository $propertyRepository, ObjectManager $manager)
{
    $this->propertyRepository = $propertyRepository;
    $this->manager = $manager;
}


public function settingRentReleaseValues()
{
    $property = $this->propertyRepository->findAll();

    dd($property);

    foreach ($property as $prop) {
        $lessees = $prop->getLessees();

        foreach ($lessees as $lessee) {
            $rentRelease = new RentRelease();
            $amount = $prop->getRentExcludingCharges() + $prop->getCharges();

            $rentRelease->setRentRelease($lessee);
            $rentRelease->setAmount($amount);
            $rentRelease->setStatus('Paiement en attente');

            $this->manager->persist($rentRelease);
            $this->manager->flush();
        }
    }
}
}

存储库在这里:
<?php

namespace App\Repository;

use App\Entity\Property;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;

/**
 * @method Property|null find($id, $lockMode = null, $lockVersion = null)
 * @method Property|null findOneBy(array $criteria, array $orderBy = null)
 * @method Property[]    findAll()
 * @method Property[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class PropertyRepository extends ServiceEntityRepository
{
public function __construct(RegistryInterface $registry)
{
    parent::__construct($registry, Property::class);
}
}

老实说,我不太明白问题从何而来,尽管我怀疑这是一个很小的问题。
编辑:
这是我的实体(我不向您显示getter setter,因为它是+500行,但它们已经生成):
<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping\JoinColumn;

/**
 * @ORM\Entity(repositoryClass="App\Repository\PropertyRepository")
 */
class Property
{
/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 * @Assert\Type("integer")
 * @var int
 */
private $id;

/**
 * @ORM\Column(type="string", length=255)
 * @Assert\NotBlank
 * @Assert\Choice({"Appartement", "Maison", "Garage", "Bureau", "Château", "Commerce"})
 * @var string
 */
private $propertyCategory;

/**
 * @ORM\Column(type="string", length=255)
 * @Assert\NotBlank
 * @Assert\Type("string")
 * @var string
 */
private $uniqueName;

/**
 * @ORM\Column(type="string", length=255)
 * @Assert\NotBlank
 * @Assert\Type("string")
 * @var string
 */
private $address;

/**
 * @ORM\Column(type="string", length=255)
 * @Assert\NotBlank
 * @Assert\Type("string")
 * @var string
 */
private $city;

/**
 * @ORM\Column(type="integer")
 * @Assert\NotBlank
 * @Assert\Type("integer")
 * @Assert\Length(min = 5, minMessage = "Ce champ doit contenir 5 chiffres")
 * @Assert\Length(max = 5, maxMessage = "Ce champ doit contenir 5 chiffres")
 * @var int
 */
private $zipCode;

/**
 * @ORM\Column(type="string", length=255)
 * @Assert\NotBlank
 * @Assert\Country
 * @var string
 */
private $country;

/**
 * @ORM\Column(type="integer")
 * @Assert\NotBlank
 * @Assert\Type("integer")
 * @var string
 */
private $surfaceInSquareMeter;

/**
 * @ORM\Column(type="integer")
 * @Assert\NotBlank
 * @Assert\Type("integer")
 * @var int
 */
private $numberOfPiece;

/**
 * @ORM\Column(type="text", nullable=true)
 * @Assert\Type("string")
 * @var string
 */
private $description;

/**
 * @ORM\Column(type="string", length=255)
 * @Assert\NotBlank
 * @Assert\Choice({"Meublé", "Non meublé"})
 * @var string
 */
private $rentalCategory;

/**
 * @ORM\Column(type="float")
 * @Assert\NotBlank
 * @Assert\Type("float")
 * @var float
 */
private $rentExcludingCharges;

/**
 * @ORM\Column(type="float")
 * @Assert\NotBlank
 * @Assert\Type("float")
 * @var float
 */
private $charges;

/**
 * @ORM\Column(type="float")
 * @Assert\NotBlank
 * @Assert\Type("float")
 * @var float
 */
private $purchasePrice;

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="properties")
 * @JoinColumn(name="user_property_id", referencedColumnName="id", onDelete="CASCADE")
 */
private $userProperty;

/**
 * @ORM\ManyToMany(targetEntity="App\Entity\Equipment", mappedBy="equipment")
 */
private $equipment;

/**
 * @ORM\ManyToMany(targetEntity="App\Entity\Lessee", mappedBy="lessee")
 */
private $lessees;

/**
 * @ORM\Column(type="string", length=255, nullable=true)
 * @Assert\File(
 *     maxSize = "2000k",
 *     mimeTypes = {"application/pdf", "application/x-pdf"},
 *     mimeTypesMessage = "Choisisez un fichier PDF"
 * )
 */
private $pdfFile;

最佳答案

您是否检查了您的实体并验证此头是否存在:


/**
 * @ORM\Entity(repositoryClass="App\Repository\PropertyRepository")
 */
class Property
{
...

关于php - Symfony4 findAll在服务中返回空数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55063913/

10-16 18:17