In my Syfmony 4.1 project, I'm using Custom Annotations to apply some meta data to some Value Objects. I'm following the Doctrine Annotation Documentation, which (I think) is assuming that you're using Doctrine as a stand alone package.
However, it seems to me that Symfony is handing some of the Annotation Setup for you. As an example, the documentation talks about using its own autoloading mechanism rather than the one provided by PHP / Composer. Symfony seems to handle this automatically.
The documentation talks about Annotation Readers and recommends caching your Annotations with either a FileCacheReader or Doctrine AppCache. I'm assuming that Symfony is using one of these mechanisms (or it's own caching mechanism) to cache Symfony's built in Annotations.
My question is this: Does Symofny automatically handle the caching of Custom Annotations, or do I need to cache them manually?
Symfony takes care the caching of doctrine annotations through the doctrine bundle configuration:
doctrine:
orm:
metadata_cache_driver: array #this is the option to configure
query_cache_driver: array
result_cache_driver: array
The metadata_cache_driver options define the cache driver that symfony uses to cache the annotations.
All you have to do is to configure this option properly to have your annotations cached.
More on how to properly configure the cache driver options in documentation.
Related
I recently got to know the Sylius project and was trying to reuse its components in a separate project (study only).
My goal was to test if I can use the sylius components in a separate project. Only a few components.
Following the documentation (http://sylius-older.readthedocs.io/en/latest/components/Order/basic_usage.html), I was able to install the components and use their classes, but how do I do the database tables?
I installed the doctrine and tried to map the classes, but I could not.
I was thinking of creating the migration (doctrine or eloquent) for each table and doing the actions (CRUD).
Thank you so much guys.
Assuming you have installed the OrderBundle using Composer you will probably have to tell Doctrine where to read the entity mapping. In case of Sylius' OrderBundle they are stored as xml files in Resources/config/doctrine/models, e.g. Order.orm.xml. If you look at the sample configuration in the DoctrineBundle-recipe you can find a reference for a manual mapping. In your case it probably should look something like this:
# app/config/config.yml (in Symfony 3.4)
# config/packages/doctrine.yaml (in Symfony 4)
doctrine:
dbal:
...
orm:
mappings:
SyliusOrderBundle:
is_bundle: false
type: xml
dir: '%kernel.project_dir%/../vendor/sylius/order-bundle/Resources/config/doctrine/models'
prefix: 'Sylius\Component\Order\Model'
alias: SyliusOrder
You might have to tweak this, e.g. if you have a Symfony 4 app, but with this you should be able to create the appropriate schema using the default Doctrine commands. You might also have to adjust auto_mapping under doctrine.orm and possibly manually map your own entities if you do this.
I am using MongoDB as a DB storage, thus I should use DoctrineMongoDBBundle to use Doctrine in my Symfony application. DoctrineBundle is installed by default with new Symfony installation, but I can safety remove it and it seems not to affect my work with Doctrine.
Should I really remove this bundle? Is it completely useless in case of using MongoDB, or I miss something, and I will need this bundle for some reason in the future?
Doctrine can mean a few things. Usually it's by default understood as Doctrine ORM. But in your case you want to use Doctrine MongoDB ODM.
DoctrineBundle is a Symfony intergration for Doctrine ORM and DBAL
DoctrineMongoDBBundle is a Symfony integration for Doctrine MongoDB ODM
Doctrine ORM and Doctrine MongoDB ODM are two separate, independent projects.
Therefore you can remove DoctrineBundle if you don't use relational databases.
I am developing a web application using Zend Framework 2 and Doctrine 2. I'm new to Doctrine 2 in general and Migrations in particular. I was wondering if there are any recommended best practices in using this. Some specific things I'm looking for:
A recommended workflow from development to deployment?
Do you include pre-populating data in migrations?
How to handle reverting to a previous version if migration fails.
Many thanks!
Doctrine has own library for migrations, that includes also Symfony bundle.
For Zend there probably is some bundle as well (maybe seek on Github a bit more)
As for your specific questions:
Nothing special. Basic workflow is nicely described in Symfony bundle documentation. We use it in pretty same way even in a different framework.
Yes, so every developer has fully operational system. For tests we use data-fixtures with minimal required data only.
It's managed by this package itself.
The Doctrine ORM module for ZF2 (DoctrineORMModule) has built-in support for Doctrine ORM migrations. There's a very brief blurb in the documentation about how to configure it. You can then access the migration commands (generate, migrate, etc) through the CLI interface that module provides (vendor/bin/doctrine-module)
As for my personal workflow I generally put initialization or pre-population data - the stuff you initially seed a new installation with - into database fixtures (which Doctrine ORM also supports and there is a ZF2 module for).
I need to cache some application specific data using Symfony 2's caching system so that I can run cache:clear to clear it. All the cache relies under app/cache but how do I actually go about caching data?
http://symfony.com/doc/current/cookbook/index.html
The only topic I see is about HTML caching with Varnish.
If you are using Doctrine already just use those cache classes.
Add a service to config.yml:
services:
cache:
class: Doctrine\Common\Cache\ApcCache
And use it in your controller:
if ($fooString = $this->get('cache')->fetch('foo')) {
$foo = unserialize($fooString);
} else {
// do the work
$this->get('cache')->save('foo', serialize($foo));
}
Simple way use Doctrine cache providers.
At first, register service(sample in config.yml):
services:
memcached:
class: Memcached
calls:
- [ addServer, ['localhost', 11211] ]
memcached_cache:
class: Doctrine\Common\Cache\MemcachedCache
calls:
- [ setMemcached, [#memcached] ]
Then to use get service, for example in controler:
$cache = $this->get('memcached_cache');
to send in another service use calls:
calls:
- [ setCacheProvider, [#memcached_cache] ]
or arguments:
arguments:
- #memcached_cache
In the same way, you can use other interfaces of Doctrine Cache package.
Doctrine Cache provides a very simple interface for which several out of the box implementations are provided:
ApcCache (requires ext/apc)
ArrayCache (in memory, lifetime of the request)
FilesystemCache (not optimal for high concurrency)
MemcacheCache (requires ext/memcache)
MemcachedCache (requires ext/memcached)
PhpFileCache (not optimal for high concurrency)
RedisCache.php (requires ext/phpredis)
WinCacheCache.php (requires ext/wincache)
XcacheCache.php (requires ext/xcache)
ZendDataCache.php (requires Zend Server Platform)
If you do not already use Doctrine, you may require Common Library for Doctrine projects: php composer.phar require doctrine/common or require only Caching library offering an object-oriented API for many cache backends: php composer.phar require doctrine/cache
How to use Doctrine Caching you can read in Doctrine Common documentation on Doctrine Project web site
Symfony 3.1 provide a new Cache component.
Symfony2 does not provide any component for application layer caching.
Like you were already told, you can use the Doctrine Common caching library http://docs.doctrine-project.org/projects/doctrine-common/en/latest/reference/caching.html
If you want something more advanced, you can also use one of the cache bundle provided by the community. For instance, the https://github.com/TheBigBrainsCompany/TbbcCacheBundle#cachebundle which provides tools for a good caching strategy.
There is no partial cache in Symfony2, the build-in cache is full HTTP only.
You have to use a reverse proxy, and if you only want to cache a piece of code, you have to use ESI. It's maybe more work than with symfony 1 but performances worth it.
Anyway, nothing stop you to use a memcached and store some stuff in it, look at this Bundle i.e.
If as your question state it, you only have data to store, that's perfect (and a memcache cache is much faster than a filesystem one).
I have an application written in symfony 1.4, and I'd like to use the Symfony2 Serializer component. Is it possible to begin with ? And what should I do to be able to do this ?
Symfony2 is composed of two main features: component and bundle. You are mixing this two concept in your question. Components are stand-alone and thus independent of each others, but can optionally make use of other components to enhance their possibilities. On the opposite, bundles usually use components to achieve their tasks. They can be dependent of each others and they all depend on the FrameworkBundle. The FrameworkBundle ties together multiple components, it's a kind of glue between components. The FrameworkBundle with other bundles like DoctrineBundle and the components form Symfony2.
Since Symfony2 components are stand-alone they can be used with any projects. There is no reason why it would not be possible to use the Serializer component within symfony 1.4.
There is not much documentation on using components of Symfony2 independently. I know they have a PEAR channel so it is simply a matter of getting the library via PEAR and add entry to the autoloader and use classes defined in the component.
I didn't see any README for the serializer component, you may need to look at the code to check how you can use it. Here some links to documentation related to the subject, not specially on serializer.
Symfony2 components page: here
Symfony2 PEAR channels: here
Serializer component github: here
Fabien Potencier's blog on what is Symfony2: here