dongou5100 2019-03-08 13:10
浏览 92
已采纳

Symfony4 findAll在服务中返回空数组

I have a problem : I get a repository in a service and I do a findAll on it, problem: I get an empty array (while my database is not). So I guess I'm doing something wrong, I hope you can enlighten me.

<?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();
        }
    }
}
}

the repository is here :

<?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);
}
}

To be honnest I don't really understand where is the problem from, even though I suspect it's a very small problem.

EDIT :

here is my entity (i don't show you getter setter because it's +500 lines but they have been generated):

<?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;
  • 写回答

1条回答 默认 最新

  • donglanzhan7151 2019-03-08 13:17
    关注

    Did you check your Entity and verify that this header is present :

    
    /**
     * @ORM\Entity(repositoryClass="App\Repository\PropertyRepository")
     */
    class Property
    {
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java代码写在记事本上后在cmd上运行时无报错但又没生成文件
  • ¥15 关于#python#的问题:在跑ldsc数据整理的时候一直抱这种错误,要么--out识别不了参数,要么--merge-alleles识别不了参数(操作系统-linux)
  • ¥15 PPOCRLabel
  • ¥15 混合键合键合机对准标识
  • ¥100 现在不懂的是如何将当前的相机中的照片,作为纹理贴图,映射到扫描出的模型上
  • ¥15 魔霸ROG7 pro,win11.息屏后会显示黑屏,如图,如何解决?(关键词-重新启动)
  • ¥15 有没有人知道这是哪里出了问题啊?要怎么改呀?
  • ¥200 C++表格文件处理-悬赏
  • ¥15 Windows Server2016本地登录失败
  • ¥15 复合卡卡号轨道写入芯片卡