How to store historical data with symfony and doctrine? - symfony

I want to store store historical data with symfony2 and doctrine2. For example i am having 2 entities:
class Shop
{
private $id;
private $url;
private $version;
}
and the second entity:
class Version
{
private $id;
private $software;
private $version;
}
The Version entity stores specific shop-versions, for example Magento 1.2 or OXID eShop 4.7 - so a entry for a version-entity should be reusable.
Every time the version for a Shop is changed, i want to store this change to have a historical view for the version-changes.
How can i do that with symfony2 and doctrine2? I have tried many-to-many mappings, but i cant figure out the right way using the correct mapping.
Thanks for your help!

There's a few things you have to set properly in order for this to work.
First, you need to tell Doctrine that $versions is related to Version:
class Shop
{
private $id;
private $url;
/**
* #ORM\ManyToMany(targetEntity="Version", cascade={"persist"})
* #ORM\JoinTable(name="shop_version",
* joinColumns={#ORM\JoinColumn(name="shop_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="version_id", referencedColumnName="id")}
* )
*/
private $versions;
}
Since it's a ManyToMany relationship (documentation), $versions will be treated like an ArrayCollection by Symfony. Thus, you need to create methods to handle it accordingly.
Constructor
public function __construct()
{
$this->versions = new ArrayCollection();
}
Getter
public function getVersions()
{
return $this->versions;
}
Adder
public function addVersion(Version $version)
{
$this->versions[] = $version;
}
Remover
public function removeVersion(Version $version)
{
$this->versions->removeElement($version);
}
That's it. Don't forget to add the use statment for ArrayCollection!
use Doctrine\Common\Collections\ArrayCollection;

In your case instead of reinventing the wheel i would recommend Doctrine2 extension: EntityAudit that allows full versioning of entities and their associations. Usage:
$auditReader = $this->container->get("simplethings_entityaudit.reader");
// find entity state at a particular revision
$articleAudit = $auditReader->find('SimpleThings\EntityAudit\Tests\ArticleAudit', $id = 1, $rev = 10);
// find Revision History of an audited entity
$revisions = $auditReader->findRevisions('SimpleThings\EntityAudit\Tests\ArticleAudit', $id = 1);
// find Changed Entities at a specific revision
$changedEntities = $auditReader->findEntitiesChangedAtRevision( 10 );
and more on: https://github.com/simplethings/EntityAudit

Another available package for entity versioning is https://github.com/madmis/ActivityLogBundle. This package includes a revision control system that saves each state of your desired entities and properties.
To enable logging, add the following annotation to your entity class
#Gedmo\Loggable(logEntryClass="ActivityLogBundle\Entity\LogEntry")
Make sure to import the annotation
use Gedmo\Mapping\Annotation as Gedmo;
Add the following annotations to the properties where you want to log changes of
#Gedmo\Versioned
The package offers methods to easily retrieve logentries for an entity
public function getLogEntriesQuery($entity)
This will return log entries with the following methods
$logEntry->getVersion() //returns entities revision version
$logEntry->getOldData() //returns data state before updating
$logEntry->getData() //returns data state after updating
$logEntry->getLoggedAt() //returns when de log was created
In order to retrieve logEntries for a given timeframe you can extend the querybuilder that's returned from the following method, which is also available in the LogEntryRepository:
public function getLogEntriesQueryBuilder($entity)

Related

symfony: where should I store and how to retrieve a tax rate?

Im wondering where should I store and how to retrieve a tax rate that Im going to use in multiple classes. I would like to set this value as a parameter just one time. Is parameters.yml a good place to set it?
/**
* #ORM\Column(type="string")
*/
protected $taxRate;
I will have the code above in multiple classes as I said.
Bear in mind that I want to use also the value on templates.
Well if you just want to set the value at a single point then the easiest solution would be using a trait.
The benefit is that you can use multiple traits which feels like multiple inheritance instead of a big basic class that is unflexible and does not care about the single responsibility principle.
The trait might look like this:
namespace Your\Namespace;
use Doctrine\ORM;
trait TaxRate {
/**
* #ORM\Column(type="string")
*/
protected $taxRate = 'your_default_value';
// set get methods here
}
You can then use it multiple times:
namespace Your\Namespace;
use Your\Namespace\TaxRate;
class Entity {
use TaxRate;
// more methods here
}
Adding parameters from parameters.yml to an entity is quite a mess in Symfony. An idea is to use the entity as a service which is not working when using Doctrine's find(). So I don't recommend this.
This code looks like it belongs to an Entity. You can share this parameter by creating an abstract class and make all the entities that need and have this property to extend form it:
abstract class TaxableEntity {
/**
* #ORM\Column(type="string")
*/
protected $taxRate;
}
class Entity extends TaxableEntity {
//here you have access to taxRate
}
This approach has its drawbacks though, which are the usual drawbacks of inheritance. Once you get many consumers of this API (protected) you'll have a very hard time re-factoring, plus you're enforcing all your Taxable entities to have that field mapped into the DB.
Another approach, would be to wrap the concept of a TaxRate into a ValueObject. This way you'd be able to use it more as a regular attribute but with a stronger type:
class TaxRate {
private function __construct($rate) {
$this->rate = $rate;
}
public static function fromFloat($value) {
if(!is_float($value)) {
throw new \InvalidArgumentException();
}
return self($value);
}
public static function fromInteger($value) {
if(!is_int($value)) {
throw new \InvalidArgumentException();
}
return self((float)$value);
}
}
Anyway, the "parameters.yml" has nothing to do with your object's properties, but more about your project's property. For instance, the necessary parameters to connect to your DB would be well located in the parameters.yml:
database_host: localhost
database_port: 3306
database_user: user
database_password: password
database_name: db_name
This way they can be injected/used in your services by means of the DIC.
I think I should consider to define it as a constant: http://symfony.com/doc/current/best_practices/configuration.html#constants-vs-configuration-options
An option where you could handle varying tax rates for when they are changed would be to store your tax rates in a separate table and reference that from your entities, possibly using a trait for ease of use and DRY code.
I will be using annotations as it seems that's what you are using.
AppBundle\Entity\TaxRate
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="app_tax_rate")
* #ORM\Entity(repositoryClass="AppBundle\Entity\TaxRateRepository")
*/
class TaxRate
{
/**
* #var int
*
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var float
*
* #ORM\Column(type="decimal", scale=2, column="tax_rate")
*/
private $rate;
/**
* #var \DateTime
*
* #ORM\Column(type="datetime", column="active_from")
*/
private $activeFrom;
.. getter & setters
}
In this you would store the tax rate and the date the rate became/becomes active. If, for example, you knew that in 3 months the rate was going to change you could add that to your database in preparation but it wouldn't take effect until that date. You would also be able to set an entities tax rates from the past or the future, should the need arise.
AppBundle\Entity\TaxRateRepository
use Doctrine\ORM\EntityRepository;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class TaxRateRepository extends EntityRepository
{
/**
* Get current tax rate if available
*
* #return TaxRate
* #throws NotFoundHttpException
*/
public function getCurrentTaxRateForDate(\DateTime $date)
{
return $this->getTaxRateForDate(new \DateTime());
}
/**
* Get tax rate for a specified date if available
*
* #param \DateTime $date
* #return TaxRate
* #throws NotFoundHttpException
*/
public function getTaxRateForDate(\DateTime $date)
{
$queryBuilder = $this->createQueryBuilder('r');
$rate = $queryBuilder
->where($queryBuilder->expr()->lte('r.activeFrom', ':date'))
->setParameter('date', $date)
->orderBy('r.activeFrom', 'DESC')
->setMaxResults(1)
->getQuery()
->getResult();
if (null === $rate) {
throw new NotFoundHttpException(sprintf(
'No tax rate available for "%s"',
$date->format('Y-m-d')
));
}
return $rate;
}
}
This "getTaxRateForDate" method will find all tax rates that became active on or before today and order them by the date they became active, in reverse, then return the first in the list.. so the last tax rate to become active before the given date. The "getCurrentTaxRate" will do the above but automatically set the date to "today".
You can then create a trait that you can drop into any entity that uses a tax rate, with the trait containing the necessary annotations. The association will be unidirectional meaning you would be able to get the tax rate from the order/invoice/etc but not the other way around.
AppBundle\Entity\Traits\TaxableTrait
use Doctrine\ORM\Mapping as ORM;
trait TaxableTrait
{
/**
* #ORM\ManyToOne(targetEntity="TaxRate")
* #ORM\JoinColumn(name="tax_rate_id", referencedColumnName="id")
*/
private $taxRate;
.. getters & setters
}
Then in each of your entities you can just add the trait which will add the getter, setter and annotation for the field.
use AppBundle\Entity\Traits\TaxableTrait;
class Order
{
use TaxableTrait;
// the rest of your entity
}
To use this you would then do something like..
$order = new Order();
// This, as stated in your custom repository, would cause an exception
// to be thrown if no tax rate was available for "now"
$order->setTaxRate($taxRateRepository->getCurrentTaxRate());
.. set the rest of the order details
$order->getTaxRate(); // would get the tax rate object
$order->getTaxRate()->getRate(); // would get the actual rate
{{ order.taxRate.rate }} // would get the rate in a twig template
Create service that returns taxrate from database .
For Performance use Doctrine Second Level Cache ( there will be no difference if you access tax one or hundred times - after first time value would be store in SLC - till end of request )

Make another entity from entity by Doctrine

I have two tables, user and userAttr.
'user' and 'userAttr' are tied as onebyone.
I would like to insert a row in userAttr when row is inserted user.
So this is my idea.
Make new data row of userAttr In prePersist() method in user entity.
in Acme/UserBundle/Entity/User.php
class User extends BaseUser implements ParticipantInterface
{
public function prePersist()
{
$userAttr = new userAttr();
$userAttr->setUser($this);
$userAttr->setEnabled(true);
$this->setUserAttr($userAttr);
$em = $this->getDoctrine()->getManager();
$em->persist($userAttr);
$em->flush();
but it shows error like this.
Fatal error: Call to undefined method Acme\UserBundle\Entity\User::getDoctrine() in
There are two quesions.
1.Is my basic idea correct?
2.How can I get the instance of doctrine in entity class?
Generally you got a little fallacy in there. Once the entity manager persists a entry it will also persist the related one-to-one connection. So if you persist $userAttr - which is one-to-one connected to your instance of User - it will persist User before it should get persisted. Causing double writings in the database. You can avoid this by adjusting your prePersist() to
public function prePersist()
{
$userAttr = new userAttr();
$userAttr->setUser($this);
$userAttr->setEnabled(true);
$this->setUserAttr($userAttr);
}
This avoids finding a way to get a instance of the entity manager too.
I'll answer your second question first.
How can I get the instance of doctrine in entity class?
You can't and you shouldn't. Your entity class is just a model, it has no knowledge of Doctrine, Symfony or the Entity Manager. Persistence will be handled at a higher level.
Is my basic idea correct?
No. As I said in the previous point, persistence shouldn't be a worry at this level. Here, you're just defining the properties and relations of your model.
I imagine your entity looks somewhat like this:
class User extends BaseUser implements ParticipantInterface
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
// ...
/**
* #ORM\OneToOne(targetEntity="UserAttr", cascade={"persist"})
*/
private $userAttr;
public function setUserAttr(UserAttr $userAttr = null)
{
$this->userAttr = $userAttr;
return $this;
}
public function getUserAttr()
{
return $this->technician;
}
}
Note the cascade={"persist"} option in the relation with UserAttr. This is what tells Doctrine that it should insert that into the database, too.
Further reading
Doctrine: One-To-One, Unidirectional
Doctrine: Transitive persistence / Cascade Operations
Symfony: Persisting Objects to the Database

How to call doctrine in an entity class using symfony

Am using symfony framework for my application. And to save records in database I want call the $this->getDoctrine()->getManager(); method in my entity class. But when I did that it gave me the error:
Call to undefined method getDoctrine(),
Can some one tell me what is the right way to do this.
My entity class is like:
namespace Acme\SuperbAppBundle\Entity;
use Symfony\Component\DependencyInjection\Container;
use Doctrine\ORM\Mapping as ORM;
class Users
{
/**
* #var integer
*/
private $id;
/**
* #var string
*/
private $firstName;
/**
* #var string
*/
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set firstName
*
* #param string $firstName
* #return Users
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
return $this;
}
/**
* Get firstName
*
* #return string
*/
public function getFirstName()
{
return $this->firstName;
}
function __construct($firstName){
$this->setFirstName($firstName);
}
function save(){
$em = $this->getDoctrine()->getManager();
$em->persist($create);
$em->flush();
}
}
And my controller method is like:
public function test(){
$create = new Users('Rajat');
$create->save();
}
Your save method is attempting to call
$this->getDoctrine();
Whereby $this is the current Class, and any other Class it inherits. As it stands, your current Class, User, is standalone, and does not have a getDoctrine() method. If your Class were to extend the Controller Class, it would have access to that method:
class User extends Controller
I believe this simple fix will work, although it probably doesn't make real sense for it to extend Controller, as it is a User Entity, and unrelated to a Controller. A preferred, more advanced method, would be to inject the Doctrine service into the User class.
Ok, first of all Doctrine Entities :
Handle the entity generation and configuration
Declare the operations on the setters and getters.
If you wana save an object into your entity there it's your User, you have two way to store this user:
One:
You can use entity manager to store a user and the entity will help you to create the right object using the seters and getters:
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use PATH\TO\Users;
class ExampleController extends Controller
{
public function examplefunction()
{
$em = $this->getDoctrine()->getManager();
$entity = new Users();
$entity->setFirstName('Rajat');
$em->persist($entity);
$em->flush();
}
}
The other way is to create this entry using QueryBuilder but it's a bad way in your case.
Oh, i forgot please delete the save method in your entity Doctrine manager allready implement it.
Your controller probably doesnt extends Symfony\Bundle\FrameworkBundle\Controller\Controller ...
You should have controller defined like this example:
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class DefaultController extends Controller
{
}
Entity class does not extends ContainerAware / Controller, so you can't call $this->getDoctrine()->getManager(). I don't think your Entity class should extend to a Controller. Because your entity class will become a controller instance just because you want to access the doctrine manager. That's a not good practice. What you can do is inject doctrine manager to your Entity class through services.
I wrote a blog few weeks ago regarding injecting services container and accessing through constructor. You can inject doctrine entity manager in the same way you inject services container. You can take a look at that if you like :- http://anjanasilva.com/blog/injecting-services-in-symfony-2/
Here's a nice question regarding injecting doctrine manager. Make sure you read the answer as well. :- Symfony 2 EntityManager injection in service
And another nice tutorial on injecting custom repository manager instead of injecting the whole entity manager. Which I believe even a good solution. :- http://php-and-symfony.matthiasnoback.nl/2014/05/inject-a-repository-instead-of-an-entity-manager/
Hope this helps to increase your understanding about Symfony 2.
Cheers!

symfony2 create doctrine collection from query

Not sure if this is possible or not but im looking to create a doctrine collection from a query. The idea is to populate the collection with some pre-set values so i can update the database think of it like an import/generate users from an old system into a new one. Im struggling with the repository bit.
Entity
// Portal\UserBundle\Entity\User.php
namespace Portal\UserBundle\Entity;
use Doctrine\ORM\Mapping AS ORM;
/**
* #ORM\Entity
*/
class User
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255, nullable=false)
*/
private $fistname;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
// etc...
}
Repository
namespace Portal\UserBundle\Entity\Repository;
use Doctrine\ORM\EntityRepository;
class UserRepository extends EntityRepository
{
public function getGenerateNewUsers()
{
// acts as an import from an old user table
$sql = " SELECT firstname, surname, other FROM old_user_table ";
$userCollection = .... not sure how I link query?
return $userCollection;
}
}
Calling it inside the controller
With the above I intend to be able to fetch the newly generated users loop over them and have access to my entity methods objects etc.
class SetupController extends Controller
{
public function indexAction(){
$repository = this->getDoctrine()->getRepository('UserBundle:User');
$newUsers = $repository->getGenerateUsers();
// I can now have access to the users with something like
foreach($newUsers as $user){
$user->setFirstName('testing');
$user->save();
}
}
}
It's usually the case with imports like this that your legacy table doesn't directly map to your new one (in terms of field names, constraints, etc), and may not even be in the same DBMS, so really the best option is a slightly manual approach. Execute your SQL query against your legacy database in your favourite old-fashioned way to get your users as simple arrays, then loop through them and create entities:
//Set up an empty collection
$collection = new ArrayCollection();
/*Assuming PDO where you have set up and executed a PDO statement $pdoStatement,
but mysql_fetch_assoc or whatever is appropriate for your DBMS would work equally
well. $oldUser should just be a plain associative array*/
while($oldUser = $pdoStatement->fetch(PDO::FETCH_ASSOC)){
//Initialise an empty User entity
$newUser = new User();
//Set the properties on the entity using the values from your array
$newUser->setFirstName($oldUser['firstname']);
//etc
//Add your user to the collection
$collection->add($newUser);
}
return $collection
I notice you're thinking of calling save() on your User objects in your controller, but it doesn't generally work that way in Doctrine as your entities will be plain objects which don't inherit from anything and don't have any special methods. The way to save the entity to your new database is to grab the entity manager and call its persist method.
In your controller:
$entityManager = $this->get('Doctrine')->getManager();
foreach($users as $user){
//Manipulate the user here if you need to
//Then persist it
$entityManager->persist($user);
}
As an aside - if you wanted to get a collection of entities by executing a query against your new database that's a slightly different problem to which there's a much more elegant solution. Doctrine Query Language allows you to query your database in a SQL-like way while using the language of your objects. With DQL, the results of your queries will by default be hydrated into Doctrine entites.
Hogan mentions DQL. Here is what that would look like, but you'd have to make sure your old database was wired up. The result is a collection of entities, off which you could use method calls to store part or all of the data as you see fit.
namespace Portal\UserBundle\Entity\Repository;
use Doctrine\ORM\EntityRepository;
class UserRepository extends EntityRepository
{
public function getGenerateNewUsers()
{
$qb = $this->getEntityManager()
->getRepository('Bundle:Old_User')->createQueryBuilder('o');
$query = $qb->getQuery();
$results = $query->getResult();
return $results;
}
}

Symfony2 - FOSUserBundle - FindUserBy<field_in_custom_UserBundle>

I´ve created my own user Bundle, extending FOSUserBundle.
In my UserBundle, I have a memberID field, with its setters and getters.
I can already find users by memberID using EntityManager, and then I could find the user through UserManager matching the username/email/... obtained with that EntityManager query, but...
Is there a way to findUserByMemberID using UserManager?
Thank you.
Thank you for your replies. It seems it´s easier than all this stuff.
OwnUserBundle:
/**
* #ORM\Column(type="string")
*
*/
protected $memberID;
public function getMemberID()
{
return $this->memberID;
}
public function setMemberID($memberID)
{
$this->memberID = $memberID;
}
You can query FOSUserBundle:
$userManager = $this->get('fos_user.user_manager');
$user = $userManager->findUserBy(array('memberID' => '123'));
Then, using method findUserBy(array(*OwnUserBundle_field* => *search_parameter_value*)) you´ll get the user/s instance.
Every query that is "not standard" one has to be written into a repository class
You have also to relate this class with one that represent you data model.
Suppose that your entity is called User, you have to do something like this
/**
* VendorName\UserBundle\Entity\User
*
* #ORM\Table(name="users")
* #ORM\Entity(repositoryClass="VendorName\UserBundle\Repository\UserRepository")
*/
class User implements AdvancedUserInterface
{
[...]
This says that every "custom" query for that entity will be fit into that repository class.
Now you have to create the repository
class UserRepository extends EntityRepository implements UserProviderInterface
{
public function findUserByMemberID($id)
{
/* your logic here */
}
[...]
and you can use that in the following way
$userRepo = $this->em->getRepository('VendorUserBundle:User');
$userRepo->findUserByMemberID();
You could extend the UserManager from the FOSUserBundle in your bundle and write your own method. And you could follow these instructions http://symfony.com/doc/current/book/doctrine.html#custom-repository-classes

Resources