checking existence of secret code before registration - fosuserbundle

I'm trying to create a private application and I'm using Symfony3 and FOSUserBundle. The administrator send an unique secret code by email to each member, and in registration the member should enter this code.
I have an entity SecretCode contains email and secret code.
What's the best way to verify existence of secret code before registration ?
// entity
class SecretCode
{
/**
* #var string
* #ORM\Column(name="eamil", type="string", length=255)
*/
private $email;
/**
* #var string
* #ORM\Column(name="secret_code", type="string", length=255)
*/
private $secretCode;
//............
}

Related

Format date direct from entity

I am using Symfony3 with doctrine and my question is:
Is possible after getQuery in query builder get results with attribute which has formatted date?
I have attr in entity:
/**
* #var \DateTime
*
* #ORM\Column(name="terminFrom", type="date", nullable=true)
*/
private $terminFrom;
My try - set format in getter:
/**
* #return string
*/
public function getTerminFrom()
{
return $this->terminFrom->format('d.m.Y');
}
But still after getQuery i have \DateTime object in this attribute.
I don't understand - attributes in entity are private so getters must be called... or?

Configure doctrine queries generation to lowercase

I have a sql error deploying my symfony project on Unix. Indeed, the query made uses uppercase for table names which was not the case before. In the database, table names are lowercases.
Does anybody know where you configure how you want the queries generation be made (uppercase or lowercase)
Thank you.
Maybe something in Doctrine configuration??
NOTE : I have some new information.
I asked to rebuild my database from the save at a time I know it was working.
I have some errors because, for the new code, the database must be a little different than what it was at this time but I can see that in queries the names of the tables are in lower case.
I pass my new sql (the same as before plus some little changes) in command line.
\. path/to/my/sql
I launch the site and queries are made with table names in uppercase.
Got any idea ?
example of entity :
<?php
namespace MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* MyTable
*
* #ORM\Table(name="mytable")
* #ORM\Entity(repositoryClass="MyBundle\Repository\MyClassRepository")
*/
class MyTable
{
/**
* #var string
*
* #ORM\Column(name="FIELD1, type="string", length=120, nullable=false)
*/
private $field1;
/**
* #var string
*
* #ORM\Column(name="FIELD2", type="string", length=50, nullable=true)
*/
private $field2
/**
* Set field1
*
* #param string $field1
*
* #return Tretb
*/
public function setField1($field1)
{
$this->field1 = $field1;
return $this;
}
/**
* Get field1
*
* #return string
*/
public function getField1()
{
return $this->field1;
}
/**
* Set field2
*
* #param string $field2
*
* #return Tretb
*/
public function setField2($field2)
{
$this->field2 = $field2;
return $this;
}
/**
* Get field2
*
* #return string
*/
public function getField2()
{
return $this->field2;
}
}
Dont know if you can configure it more generally, but you can define table table for each entity with doctrine
/**
* AppBundle\Entity\MyEntity
*
* #ORM\Table()
* #ORM\Entity
* #ORM\Table(name="mytable")
*/
class MyEntity
{
Does your MySQL instance support lowercase tablenames ?
To check the settings use:
mysql> show variables like "lower_case%";
To change the setting you need to change the mysql setting
lower_case_table_names = 1 or lower_case_table_names = 2
Source from Stackoverflow

Doctrine with MSSQL returning same children

I am using an existing MSSQL-database (Dynamics) and to fetch data out of it I've cloned the database layout in a Doctrine entity so I can fetch the data out of the database. This works well for a sole entity, but when I have OneToMany relationships, something goes wrong. It's returning the correct amount of children, but they're all the same.
When I copy paste the runnable query into SQL Server I get the correct results (with different children). So the SQL query is good.
So it seems like Doctrine doesn't hydrate the result correctly. I am using the pdo_sqlsrv driver and Doctrine2 with Symfony 2.8.
Dynamics table layout:
PurchTable
recid = unique integer
purchid = unique string
PurchLine
recid = unique integer
purchid = referring to purchid from PurchTable
itemid = string
etc...
PurchTable Entity:
/**
* #var integer
*
* #ORM\Column(name="recid", type="integer")
*/
private $recid;
/**
* #var string
*
* #ORM\Column(name="purchid", type="string", length=255)
* #ORM\Id
*/
private $orderid;
/**
* #ORM\OneToMany(targetEntity="PurchLine", mappedBy="order")
*/
protected $lines;
public function __construct()
{
$this->lines = new ArrayCollection();
}
PurchLine:
/**
* #var integer
*
* #ORM\Column(name="recid", type="integer")
* #ORM\Id
*/
private $recid;
/**
* #var integer
*
* #ORM\Column(name="linenum", type="integer")
*/
private $linenum;
/**
* #var string
*
* #ORM\Column(name="itemid", type="string", length=255)
*/
private $itemid;
/**
* #ORM\ManyToOne(targetEntity="PurchTable", inversedBy="lines")
* #ORM\JoinColumn(name="purchid", referencedColumnName="purchid")
*/
protected $order;
As you can see I changed the property name to orderid in PurchTable to avoid using the same name, but that didn't help.
So what am I doing wrong? :)
I cannot change the driver at a short time so I am wondering if this is a driver issue or not (to change the priority of changing the driver). After reading this:
http://doctrine-orm.readthedocs.org/projects/doctrine-dbal/en/latest/reference/configuration.html#driver
A Microsoft SQL Server driver that uses pdo_sqlsrv PDO Note that this driver caused problems in our tests. Prefer the sqlsrv driver if possible.
I am wondering if the pdo_sqlsrv driver can cause these issues or not.
edit
I tested the sqlsrv driver (be sure to use an empty string as username instead of null (for pdo_sqlsrv) to use Windows Authentication) and I encounter the same problem. So it must be something in Doctrine or in the relationship.
edit 2
I recreated the entities to avoid any spelling mistakes but to no avail.
The problem lies in the wrong definition of the columns. The RecId column in the MSSQL database was a bigint instead of integer.
So, changing
/**
* #var integer
*
* #ORM\Column(name="recid", type="integer")
* #ORM\Id
*/
private $recid;
to
/**
* #var bigint
*
* #ORM\Column(name="recid", type="bigint")
* #ORM\Id
*/
private $recid;
is the answer.

Symfony2 Entity ManyToMany Relation Data Structure

I have the following setup:
Entity: 'Customers'
Entity: 'Accounts'
Those entities are in a #ManyToMany relation.
So I have the following database tables:
customers
accounts
customers_accounts
Now I need to save data for each customer for each account. Example:
There is one customer 'Tom'. Two accounts 'Ben' and 'Eric' are in charge of customer 'Tom'. Now I need to save whether or not account 'Ben' has already spoken to customer 'Tom'. Also I need to save the same status for account 'Eric'.
What's the best way to organise the database here? Best thing would be that there is an extra column in the table 'customers_accounts'. Is that possible? What other options are there?
Thanks for your help!
Just to show you how the entities are linked to each other:
/**
* #ORM\ManyToMany(targetEntity="AppBundle\Entity\Customer", inversedBy="accounts", cascade={"persist"})
* #ORM\JoinTable(name="customers_accounts")
**/
protected $customers;
In ManyToMany you can't add additional fields in your junction table i.e customers_accounts , to have an additional fields for your junction table you have to tweak your mappings as create a junction entity which will point your customer and accounts entity in ManyToOne way and your customer and accounts entities will point back to your junction entity in a OneToMany way
OneToMany OneToMany
-----------> <------------
Customer CustomerHasAccounts Accounts
<---------- ------------>
ManyToOne ManyToOne
Customer Entity
/**
* Customer
* #ORM\Table(name="customer")
* #ORM\Entity
*/
class Customer
{
/**
* #ORM\OneToMany(targetEntity="NameSpace\YourBundle\Entity\CustomerHasAccounts", mappedBy="customers",cascade={"persist","remove"} )
*/
protected $hasAccounts;
}
Accounts Entity
/**
* Accounts
* #ORM\Table(name="accounts")
* #ORM\Entity
*/
class Accounts
{
/**
* #ORM\OneToMany(targetEntity="NameSpace\YourBundle\Entity\CustomerHasAccounts", mappedBy="acccounts",cascade={"persist","remove"} )
*/
protected $hasCustomers;
}
CustomerHasAccounts Entity
/**
* CustomerHasAccounts
* #ORM\Table(name="customers_accounts")
* #ORM\Entity
*/
class CustomerHasAccounts
{
/**
* #ORM\ManyToOne(targetEntity="NameSpace\YourBundle\Entity\Accounts", cascade={"persist"}, fetch="LAZY")
* #ORM\JoinColumn(name="acccount_id", referencedColumnName="id")
*/
protected $acccounts;
/**
* #ORM\ManyToOne(targetEntity="NameSpace\YourBundle\Entity\Customer", cascade={"persist","remove"} , fetch="LAZY" )
* #ORM\JoinColumn(name="customers_id", referencedColumnName="id",nullable=true)
*/
protected $customers;
/**
*
* #ORM\Column(name="status", type="string")
*/
protected $status;
}
You have to transform your ManyToMany relation between your entities by a OneToMany-ManyToOne entity making the link between your 2 ManyToMany entities, this entity will have all the data related to this relation.
Customer
Class Customer
{
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\CustomerAccount", mappedBy="societe", cascade={"Persist"})
*/
private $customerAccounts;
}
Account
Class Account
{
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\CustomerAccount", mappedBy="account", cascade={"Persist"})
*/
private $customerAccounts;
}
CustomerAccount
Class CustomerAccount
{
/**
* #ORM\Id
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Customer", inversedBy="customerAccounts")
*/
private $societe;
/**
* #ORM\Id
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Account", inversedBy="customerAccounts")
*/
private $contact;
/**
* #var boolean
*
* #ORM\Column(name="alreadySpoken", type="boolean")
*/
private $alreadySpoken;
}

How can I use 'foreign key' on doctrine?

I am making the lesson administration system on symfony2 and doctrine
I am confused to use foreign key in doctrine.
/Entity/User.php
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*#ORM\OneToOne(targetEntity="Acme\UserBundle\Entity\Lesson", inversedBy("teacher"))
*/
protected $id;
.
.
}
/Entity/Lesson.php
class Lesson
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* #ORM\OneToOne(targetEntity="Acme\UserBundle\Entity\User", inversedBy("id"))
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $teacher;
.
.
}
Each 'Lesson' has one teacher registered in User.php.
How can I write annotation for this purpose?
I am also planning that each Lesson has multiple students from /Entity/User.
How can I write annotation for this purpose? (ManyToMany?)
I have researched ,but I couldn't find good documents for doctrine annotation.
Here some cheat sheets for doctrine annotations : link
For your problem, you need to define your variables in each side of your associations.
In Lesson.php :
/**
* #ORM\OneToOne(
* targetEntity="Acme\UserBundle\Entity\User",
* inversedBy="lessons*removethis : name of the variable in user.php*"
* )
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $teacher;
In User.php :
/**
* #ORM\OneToOne(
* targetEntity="Acme\UserBundle\Entity\Lesson",
* mappedBy="teacher*removethis : name of the variable in lesson.php*"
* )
*/
private $lessons;
And yes, ManyToMany is good for the purpose your are looking for :)

Resources