I have started to look into Symfony CMF, and I must say it looks good!
However, I can't get my head around the database Schema! I have read up on the basics of PHPCR, and understand it to an extent. The bit that is confusing me are all the extra tables that Symfony CMF installs. I.E
phpcr_binarydata
phpcr_internal_index_types
phpcr_namespaces
phpcr_nodes
phpcr_nodes_references
phpcr_nodes_weakreferences
phpcr_type_childs
phpcr_type_nodes
phpcr_type_props
phpcr_workspaces
What are all these used for? The only table I can make sense of is phpcr_nodes and the XML / property data stored! Some tables have data and are being queried - but I can't see any reference to them in phpcr_nodes!
For example:
Table "phpcr_type_nodes" has data such as
name: phpcr:managed
supertype: nt:base
Table "phpcr_type_props" has data such as
name: copyright
When you see this schema, it means you are using Jackalope Doctrine DBAL, which is one possible implementation of the PHPCR API. This implements PHPCR on top of a relational database. Unfortunately, there is not too much documentation available, but all of those tables are used.
If you want to learn more about PHPCR, i recommend to have a look at this PHPCR Tutorial . If you really want to understand the database schema in detail, you will need to dig through the source code of Hackalope Doctrine DBAL.
Note that you can also use doctrine ORM with the CMF. The RoutingBundle already brings mapping for it, for other bundles it would be doable to implement ORM mappings and we would be glad for contributions.
PS: I never figured out how to get notifications after the initial notification on stackoverflow, so best open an issue on the relevant github repository if you need more information.
Related
I'm trying to teach myself ZF2 in conjunction with Doctrine 2. I've completed both the Album tutorial and Blog Tutorial on Zend's website successfully. Now I'm trying to go back and convert the Blog Tutorial to use Doctrine 2. I believe I've successfully setup my config for doctrine and used DI to get it inside of my controller (WriteController.php) since I am able to dump the contents of it within my action. I don't get any errors so long as I don't do anything with it.
My question is what roll does Doctrine take in the Controller -> Service -> Mapper -> Backend layered structure which was taught in the Blog tutorial? (Reference To what I mean)
Also, I'm assuming Backend is referring to my Models. Is this correct?
Would I just replace any references to /Blog/Model/Post with /Blog/Entity/Blog?
The Doctrine is the Mapper. And maybe we could say also the Service (through EntityRepository). But usually you will create your own Service Layer.
The Backend is not the entities it self. Entities in one way to map the several options of backend. As backend you can understand the several options of Relational Databases (Mysql, SqlServer, Oracle, etc) NoSql Databases (like MongoDB), file system and so on.
I didn't understand your last question. But when I use Doctrine I always create my entities in /MyModule/Entity namespace. While when I use the standart Zend/Db I always create in /MyModule/Model. I do that by standardizing matters.
I have two tables in DB (topic, topic_content):
what kind of entities i should create for symfony2?
I think, i should have something like this in my symfony structure (Entities/Topic.php, Entities/Topic_content.php) help me please..
Yes, you would create Topic and Topic Content. And likely also a User Entity (because user_id looks like a foreign key).
However, the idea in Symfony2 is to approach the application from the Model site instead of the database site. Quoting https://doctrine-orm.readthedocs.org/en/latest/tutorials/getting-started-database.html:
Development Workflows
When you Code First, you start with developing Objects and then map them onto your database. When you Model First, you are modelling your application using tools (for example UML) and generate database schema and PHP code from this model. When you have a Database First, you already have a database schema and generate the corresponding PHP code from it.
For database first, there is a generator that will derive objects based on your schema:
https://github.com/beberlei/DoctrineCodeGenerator
The recommended approach though is to have Doctrine generate the db schema from your Entities.
Quoting Getting Started - Generating the DB Schema
Doctrine has a Command-Line Interface that allows you to access the SchemaTool, a component that generates the required tables to work with the metadata.
It requires some setup, which is explained in the guide. Once you have that, you simply tell Doctrine to generate or update your schema, whenever your object structure changes.
How to take care of fetching related objects of one object?
For example object project has some tags. How and when should I fetch those object? In user initialization in mapper? That would be a big overload. The best way would be to load them dynamicly when system ask user for tags, but how to do that if model does not know anything about the mapper? Or just use Doctrine and forgot all about those problems?
Im asking this in relation to PHP Zend Framework. But any technology would suffice I think for this problem.
It is difficult to answer your question because you are not referring to a specific ORM or framework. If you are looking for suggestions, I would recommend using Doctrine as the model API and Zend Framework as a stand-alone library.
If you need a full featured framework you can take a look at any of these:
Symfony2
CakePHP
Zend Framework (as a framework vs stand-alone lib)
CodeIgniter
If you choose to go with Doctrine as your ORM, you can setup the schema file to ensure objects are relationship aware, then you can make references like:
// Joins tags table by way of intermediary object_tag table providing
// a M:1, 1:M relationship
$tags = $object->getTags();
Doctrine (1.2 not sure about 2.x) does employ lazy loading pattern, where the objects are only queried when requested.
What I'm trying to figure out is how to add new fields to a table, using Symfony2 with Doctrine2.
I used this to initially create the Entity:
php app/console doctrine:generate:entity --entity="MyMainBundle:ImagesTable" --fields="title:string(100) file:string(100)"
And I used this to create/update the tables on the database:
php app/console doctrine:schema:update --force
Now if I wanted to add new fields to the ImagesTable entity, is there an easy way to do it using the console, or do I have to manually edit the entity. I am just using 1 entity as an example right now, but in reality, there are many entities I'd be changing; so, there has to be an easier way to do it.
I've been manually editing them to create relationships, so if there is an easier way to do that as well, that'd be great.
I remember this being a lot easier with Symfony1.4 - all I had to do was create the database/tables using phpMyAdmin, and Symfony was able to generate the models with no issues.
I really hope I'm missing something here, because this won't work if I have to manually edit every entity for every change.
Doctrine generator commands are intended to help the developer to quickly prototype an idea. They generally don't produce production ready code, and the code needs to be checked to see if it contains what you want.
You can still create your model in phpmyadmin and use Doctrine reverse engineering tools, but it also doesn't produce production ready code, only intended to use in prototyping.
Creating database/tables beforehand doesn't really work well with Doctrine2, as the underlying relation between tables may not be the same as the relation between objects of your model. The whole point of ORM is to think in classes and letting Doctrine do the rest of the work for you.
Doctrine is not intended to write your entities for you, it gives you tools to build your data model, which you use to code your model in Php.
If you don't like to code your entities by hand (which is what all developers using doctrine does), you may want to have a look at RedbeanPHP, a zero-config ORM framework for PHP. It creates the database tables, columns, indexes on the fly depending on the data model you use.
I'm building a web app and would like to use an ORM to map the data from db with objects.
I have been struggling with this for a few days, and tried various implementations, including: Codeigniter ORM, Kohana ORM, PHP.ActiveRecords and Doctrine.
The last two I can't really use, because they depend on PHP 5.3 which is not installed (and can not be) on my shared hosting server.
The problem with all these ORM-s is inheritance (except for Doctrine, but I can' use it).
I know a lot of developers just do db-s without inheritance, but here is my problem:
My db needs to be multilingual, I designed my db by the following example:
Table Product -> id, category_id, price... (everything that is not translatable)
Table ProductTranslation-> id, language_id, name, description... (same id as product, adds language FK, and fields that are translatable)
Maybe there is a better approach, but I really like this, because its very flexible (can add new languages) and does not put a lot of NULL-s in the db, also joining is not that costly because the tables are relatively small (unlike the approach to put all translations in a single table, and reference that table from all other tables).
The only workaround, I found, to support ORMs is to put a has_one relation between
Product->ProductTranslation.
This way I could access the translated fields with something like:
DB::get_product_by_id(4)->translation->name
Nevermind the syntax, but the real problem with this approach is that I have to define new objects(models) for translation tables, and logically they don't belong there. They are not entities, just additional data for the entities.
So, my question here are:
Is there a better way to organize languages in the db, which is more ORM friendly ?
Are there any other PHP ORM-s (<5.3) which support inheritance ? - Btw. I found the Kohana ORM ihertiance module but it seems out-of-date, and doesn't work with the current framework version
Are there any other workaround for the inheritance problem?
Doctrine 2.0 requires PHP 5.3, but Doctrine 1.2.x works fine on PHP 5.2.3 or newer.
why not use Propel's i18n behavior ?