Wie ich mit der Lehre mit der Anzahl der Zählungen beitreten kannPhp

PHP-Programmierer chatten hier
Anonymous
 Wie ich mit der Lehre mit der Anzahl der Zählungen beitreten kann

Post by Anonymous »

In meinem Symfony -Projekt habe ich folgende Lehre Modelle: < /p>
Ein für meinen Benutzer < /p>

Code: Select all

declare(strict_types=1);

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use App\Entity\Business;

#[ORM\Entity()]
class User
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;

#[ORM\OneToMany(targetEntity: Business::class, mappedBy:"user")]
private Business $business;
#[ORM\Column(type: 'string', length: 180, unique: true)]
private $email;

public function getId(): ?int
{
return $this->id;
}

public function getEmail(): ?string
{
return $this->email;
}

public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}

public function getBusiness(): ?Business
{
return $this->business;
}

public function setBusiness(?Business $business): self
{
$this->business = $business;
return $this;
}
}

< /code>
und Business: < /p>
declare(strict_types=1);

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class Business
{
#[ORM\Id]
#[ORM\Column(type: 'integer')]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private $id;

#[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'business')]
#[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')]
private User $user;

#[ORM\Column(type: 'string')]
private string $name = "";

public function getId(): int
{
return $this->id;
}

public function getName(): string
{
return $this->name;
}

public function setName(string $name):void
{
$this->name = trim($name);
}

public function getUser(): User
{
return $this->user;
}

public function setUser(User $user):void
{
$this->user = $user;
}

}

< /code>
Jetzt versuche ich, die Doktrin und seinen Abfragebauer zu verwenden, um das Äquivalent dieser SQL -Abfrage auszuführen: < /p>
select
user.*,
business_count.count
from
user
join (
select
count(*) as count,
user_id
from
business
group by
user_id) as business_count on business_count.user_id = user.id;
< /code>
Um dies zu tun, habe ich diesen Konsolenbefehl erstellt: < /p>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post