我有一个问题: 我在服务中获得一个存储库,并对其执行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;
答案 0 :(得分:0)
您是否检查了Entity并验证此标头是否存在:
/**
* @ORM\Entity(repositoryClass="App\Repository\PropertyRepository")
*/
class Property
{
...