ghaziksibi
ghaziksibi

Reputation: 471

Value setted to null on flush doctrine

I've got a problem with my code. I am just trying to insert a simple set of data to my db, but doctrine insert my attribute (telVerifCode) as NULL.

I've dumped my data and figured out, that attribute (telVerifCode) has some value in it, but after I flush it is set to NULL.

This is my controller:

$user = $this->getUser();

if ($user->getTel() != $tel || $user->getTelCode() != $telCode) {

    try {
        $code = $this->sendTelehopneCode($user);
    } catch (\Exception $e) {
        //.......
    }

    // update user phone verifcation fields //
    $user->setTelVerifCode($code);
    $user->setLastTelVerificationCodeDate(new \DateTime());

    $em->persist($user);
    $em->flush();
}

My ORM Mapping:

/**
 * @var string
 *
 * @ORM\Column(name="tel_verification_code", type="string", length=255, nullable=true)
 */
protected $telVerifCode;
/**
 * @var \DateTime
 *
 * @ORM\Column(name="last_tel_verification_code_date", type="date", nullable=true)
 */
protected $lastTelVerificationCodeDate;

sendTelehopneCode function :

private function sendTelehopneCode($user)
{
    $code = strval(rand(100000, 999999));
    $tel = $user->getTelCode() . $user->getTel();
    $msg = 'code:' . $code;

    $twilio = $this->get('twilio.api');
    try {
        $message = $twilio->account->messages->sendMessage(
            "+14*******", // Verified Outgoing Caller ID or Twilio number
            $tel, // The phone number you wish to send a message to
            $msg
        );
    } catch (\Services_Twilio_RestException $e) {
        throw $e;
    }

    return $code;
}

Upvotes: 3

Views: 1127

Answers (3)

ghaziksibi
ghaziksibi

Reputation: 471

I solved the problem, I made a listener On preUpdate one that puts the value null, I completely forgotten it :(

Upvotes: 1

Samir Patel
Samir Patel

Reputation: 895

Try clearing your doctrine caches, the code looks fine and cannot be the issue.

./bin/console doctrine:cache:clear-metadata
./bin/console doctrine:cache:clear-query
./bin/console doctrine:cache:clear-result

Upvotes: 1

Paul Andrieux
Paul Andrieux

Reputation: 1847

Maybe your problem is due to a typo in your setter. Are you sure your setter setTelVerifCode looks exactly like this?

public function setTelVerifCode($code)
{
    $this->telVerifCode = $code;
}

Upvotes: 0

Related Questions