在生成扩展自定义记录类的Doctrine模型/类时,可以遵循以下步骤:
以下是一个简单的示例:
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\BaseEntity;
/**
* @ORM\Entity(repositoryClass=CustomRepository::class)
*/
class CustomEntity extends BaseEntity
{
/**
* @ORM\Column(type="string", length=255)
*/
private $customField;
public function getCustomField(): ?string
{
return $this->customField;
}
public function setCustomField(string $customField): self
{
$this->customField = $customField;
return $this;
}
}
namespace App\Repository;
use App\Entity\CustomEntity;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
class CustomRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, CustomEntity::class);
}
public function findByCustomField(string $customField)
{
return $this->createQueryBuilder('c')
->andWhere('c.customField = :customField')
->setParameter('customField', $customField)
->getQuery()
->getResult();
}
}
$customEntity = new CustomEntity();
$customEntity->setCustomField('custom value');
$entityManager->persist($customEntity);
$entityManager->flush();
$customRepository = $entityManager->getRepository(CustomEntity::class);
$customEntities = $customRepository->findByCustomField('custom value');
这样,就可以生成扩展自定义记录类的Doctrine模型/类了。
领取专属 10元无门槛券
手把手带您无忧上云