Seeking null value with get in the Doctrine Entity

Asked

Viewed 37 times

0

Opa all right? I have a problem with values NULL with Doctrine. What happens: I have some fields in a database table where I keep some values for future validations, these fields are nullable = true. Let me give you an example: When I token, i automatically Gero a date for it, and when this token is used, I pass values NULL to the country validateToken, that keeps the date that was generated, as for the token. To define this NULL in the setter It’s quiet, the problem is when I pick up the getter the getValidateToken() or the getTonken() to do some checking and he’s NULL in this case he tells me I need to return a DateTime in the getValidateToken() and string in the getToken() and is returning NULL. Follow my code and error below.

/**
 * @var string
 * @ORM\Column(type="string", length=255, nullable=true)
 */
private $token;

/**
 * @var \DateTime
 * @ORM\Column(type="datetime", nullable=true)
 */
private $validateToken;

/**
 * @return string
 */
public function getToken(): string
{
    return $this->token;
}

/**
 * @param string $token
 *
 * @return User
 */
public function setToken(string $token = null): User
{
    $this->token = $token;
    return $this;
}

/**
 * @return \DateTime
 */
public function getValidateToken(): \DateTime
{
    return $this->validateToken;
}

/**
 * @param \DateTime $validateToken
 *
 * @return User
 */
public function setValidateToken(\DateTime $validateToken = null): User
{
    $this->validateToken = $validateToken;
    return $this;
}

I know I can remove : string and : Datetime but there must be a way to do it without removing.

Thanks

1 answer

0

Eai.. look at this.. I just figured out how to solve the problem.. Just use the type hinting of php7 and put a ? before setting the return. So:

/**
 * @return string
 */
public function getToken(): ?string
{
    return $this->token;
}

/**
 * @return \DateTime
 */
public function getValidateToken(): ?\DateTime
{
    return $this->validateToken;
}

And it’s ready.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.