Reputation: 10032
I have made on my Symfony 3.0 project a User Entity:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="users")
* @ORM\Entity(repositoryClass="AppBundle\Entity\UserRepository")
*/
class Users
{
/**
* @ORM\Column(type="string", length=60)
* @ORM\Id
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="AppBundle\Doctrine\AutoIdGenerate")
*/
private $id;
/**
* @ORM\Column(type="string", length=15)
*/
private $username;
/**
* @ORM\Column(type="string", length=30)
*/
private $password;
/**
* @ORM\Column(type="string", length=60)
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="AppBundle\Doctrine\AutoIdGenerate")
*/
private $token;
/**
* @ORM\Column(type="boolean")
*/
private $activated;
/**
* Get id
*
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* Set username
*
* @param string $username
*
* @return Users
*/
public function setUsername($username)
{
$this->username = strip_tags($username);
return $this;
}
/**
* Get username
*
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set password
*
* @param string $password
*
* @return Users
*/
public function setPassword($password)
{
$options = ['cost' => 42];
$this->password = password_hash ($password,CRYPT_BLOWFISH,$options);
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set token
*
* @param string $token
*
* @return Users
*/
public function setToken($token)
{
$this->token = $token;
return $this;
}
/**
* Get token
*
* @return string
*/
public function getToken()
{
return $this->token;
}
/**
* Set activated
*
* @param boolean $activated
*
* @return Users
*/
public function setActivated($activated)
{
$this->activated = $activated;
return $this;
}
/**
* Get activated
*
* @return boolean
*/
public function getActivated()
{
return $this->activated;
}
}
And a Repository that Iplements My logic:
<?php
namespace AppBundle\Entity;
use AppBundle\Entity\Product;
use Doctrine\ORM\EntityRepository;
class UserRepository extends EntityRepository
{
public function register($username,$password,$email)
{
//A Generic return Object
$status=array('status'=>'true','data'=>"");
if(empty($username))
{
$status['status']=-1;
$status['data']='Username not given';
return $status;
}
if(empty($password))
{
$status['status']=-1;
$status['data']='Password not given';
return $status;
}
if(empty($email))
{
$status['status']=-1;
$status['data']='Email not given';
return $status;
}
$user=new User();
$user->setUsername($username)->setPassword($password);
$em=$this->getEntityManager();
$em->persist($user);
$em->flush();
return $status;
}
}
What I wanna know is when an Entry Sucessfully Inserted to send the email to verify the registration. How I will know That the User Has Sucessfully Inserted In the DB; Also How I will take any Sort of AutoGenerated Fields such as id and token in order to verify the mail address.
I Found that In order to Use Email I can Use the Swiftmailer so the Question Is NOT hoiw to send an Email, but how I will know that the user successfully inserted.
Upvotes: 1
Views: 82
Reputation: 13167
If your entry is correctly inserted, you'll not have any error.
Otherwise, you'll have one (unless you have a mistake that is not coming from the insertion).
To catch a possible error, you can do this :
// ...
$em->persist($user);
try {
$em->flush();
// Success
} catch (\Exception $e) {
$result['data'] = $e->getMessage(); // The exact message of the exception thrown during INSERT
// Error
}
Otherwise, you can consider the entry has been stored.
For the auto-generated fields, they are immediately available through getters after calling $em->flush()
.
Upvotes: 1
Reputation: 3812
You can register Event Listener for Doctrine postFlush
event.
Example listener:
<?php
use Doctrine\ORM\Event\PostFlushEventArgs;
class PostFlushExampleListener
{
public function postFlush(PostFlushEventArgs $args)
{
// ...
}
}
Service registration in Symfony:
services:
my.listener:
class: PostFlushExampleListener
tags:
- { name: doctrine.event_listener, event: postFlush }
Symfony documentation: http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html
Doctrine documentation: http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/events.html#listening-and-subscribing-to-lifecycle-events
You should have all autogenerated fields available just after calling flush
action there where you call it. Doctrine events are non mandatory in your case. I would just assume that after flush i will have all needed data and data will be set in database.
Upvotes: 1