On 03/27/2012 11:10 PM, Thomas Lundquist wrote:
The model in MongoDB would inherit from
the model in the Model directory, but implement code specific to
MongoDB, ORM would also inherit from the model in the Model
directory, but implement ORM specific code.
But here we disagree. Model should inherit the storage/persistence, not
like you describe it.
Then I can always ask for and make new \Model\Foo in all my code and not
having to ponder how it's stored, Entity, Document, REST, whatever which
would be \Entity\Foo and then later \REST\Foo when it's changed.
But this is a digression.
In a situation where you are writing a bundle that can be used across a
number of persistence types, its not really practical to have the model
inherit from the persistence. The way around this is to set up your
configuration so that you can select your persistence type in when your
DI is configured, select the configuration .xml for the selected
persistence.
app/config/config.yml
my_awesome_bundle:
db_driver: mongodb
MyAwesomeBundle\DependencyInjection\MyAwesomeExtension.php
public function load($configs, $container)
{
...
$loader = new XmlFileLoader($container, new FileLocation(__DIR__ .
'/../Resources/config'));
if (!in_array(strtolower($config['db_drive]), array('mongodb',
'orm'))) {
throw new \InvalidArugmentException(sprintf('What the hell kind
of db is %s', $config['db_drive']));
}
$loader->load(sprintf('%s.xml', $config['db_driver']);
}
MyAwesomeBundle\Resources\config\mongodb.xml
...
<parameter
key="my_awesome.model.class">MyAwesomeBundle\MongoDB\Model.php</parameter>
...
<service id="my_awesome.manager" class="MyAwesomeBundle\MongoDB\Manager.php">
<argument>%my_awesome.model.class%</argument>
</service>
...
Then you can use the container to get the class or use the manager with
the injected the class name
$myAwesome = new $this->container->getParameter('my_awesome.model.class');
or
$myAwesome = $this->container->get('my_awesome.manager')->createMyAwesome();
Look at https://github.com/FriendsOfSymfony/FOSUserBundle for a great
example of this. However, if you are not writing for a bundle, I'm not
sure how much it matters if you split the persistence part of the code
from the rest of the logic.
--
If you want to report a vulnerability issue on symfony, please send it to
security at symfony-project.com
You received this message because you are subscribed to the Google
Groups "symfony developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/symfony-devs?hl=en