Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embeddable not work in simple QueryBuilder #11751

Open
danilovl opened this issue Dec 6, 2024 · 0 comments
Open

Embeddable not work in simple QueryBuilder #11751

danilovl opened this issue Dec 6, 2024 · 0 comments

Comments

@danilovl
Copy link

danilovl commented Dec 6, 2024

Bug Report

Query builder work very weird after change string to Embeddable

Q A
Version ^3.3
Previous Version if the bug is a regression ---

Summary

<?php declare(strict_types=1);

namespace App\Domain\User\Entity;

use App\Application\Exception\InvalidArgumentException;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\{
    Column,
    Embeddable
};
use Egulias\EmailValidator\EmailValidator;
use Egulias\EmailValidator\Validation\NoRFCWarningsValidation;
use Stringable;

#[Embeddable]
class EmailEmbeddable implements Stringable
{
    #[Column(name: 'email', type: Types::STRING, unique: true, nullable: false)]
    private string $email;

    public function __construct(string $email)
    {
        $this->setEmail($email);
    }

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

    private function setEmail(string $email): void
    {
        $email = trim($email);

        $validator = new EmailValidator;

        if (!$validator->isValid($email, new NoRFCWarningsValidation)) {
            throw new InvalidArgumentException;
        }

        $this->email = $email;
    }

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

    public function __toString(): string
    {
        return $this->toString();
    }
}

Change string by EmailEmbeddable

#[ORM\Table(name: 'user')]
#[UniqueEntity(fields: ['email'])]
#[UniqueEntity(fields: ['username'])]
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\HasLifecycleCallbacks]
#[ORM\Cache(usage: 'NONSTRICT_READ_WRITE', region: 'default')]
class User implements UserInterface, PasswordAuthenticatedUserInterface, LegacyPasswordAuthenticatedUserInterface
{
    use CreateUpdateAbleTrait;

    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'AUTO')]
    #[ORM\Column(name: 'id', type: Types::INTEGER)]
    private int $id;

    #[ORM\Column(name: 'username', type: Types::STRING, length: 180, nullable: true)]
    private ?string $username = null;

//    #[ORM\Column(name: 'email', type: Types::STRING, unique: true, nullable: false)]
//    private string $email;

    #[ORM\Embedded(EmailEmbeddable::class, columnPrefix: false)]
    private EmailEmbeddable $email;

Current behavior

SImple repository query not work ->getQuery()->getOneOrNullResult();

    public function oneByUsername(string $username, ?bool $enable = null): QueryBuilder
    {
        $builder = $this->createQueryBuilder('user')
            ->andWhere('user.username = :username')
            ->setParameter('username', $username);
 
        if ($enable !== null) {
            $builder->andWhere('user.enabled = :enable')
                ->setParameter('enable', $enable);
        }

        return $builder;
    }

return error

Doctrine\DBAL\Types\Type::getType(): Argument #1 ($name) must be of type string, null given,
called in /var/www/html/app/vendor/doctrine/orm/src/Internal/Hydration/AbstractHydrator.php on line 426

But if I change the same query in a different way, its okay

    public function oneByUsername(string $username, ?bool $enable = null): QueryBuilder
    {
         $builder = $this->createQueryBuilder('user');
         $builder->where('user.username = ' . $builder->createNamedParameter($username, Types::STRING));

        if ($enable !== null) {
            $builder->andWhere('user.enabled = :enable')
                ->setParameter('enable', $enable);
        }

        return $builder;
    }

Expected behavior

EmailEmbeddable should not change the behavior of the QueryBuilder result

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant