@@ -336,6 +336,151 @@ the name indicates, loads ``Route`` entities from an ORM database.
336
336
You must install the CoreBundle to use this feature if your application
337
337
does not have at least DoctrineBundle 1.3.0.
338
338
339
+ .. _bundle-routing-route-entity :
340
+
341
+ The ORM Route entity
342
+ --------------------
343
+
344
+ The example in this section applies if you use the ORM route provider
345
+ (``Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\RouteProvider ``). It uses the
346
+ ``staticPrefix `` field of the
347
+ ``Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\Route `` to find route candidates.
348
+
349
+ Symfony Cmf routing system allows us loading whatever content from a route.
350
+ That means an entity route can reference to different types of entities.
351
+ But Doctrine ORM is not able to establish that kind of mapping associations.
352
+ To do that, the ORM RouteProvider follows the pattern of FQN:id. That is, the full
353
+ model class name, then a colon, then the id. You only need to add it to the
354
+ defaults parameters of the route with the ``RouteObjectInterface::CONTENT_ID ``
355
+ key. ``cmf_routing.content_repository `` service can help you to do it easily.
356
+ A new route can be created in PHP code as follows::
357
+
358
+ // src/AppBundle/DataFixtures/ORM/LoadPostData.php
359
+ namespace AppBundle\DataFixtures\ORM;
360
+
361
+ use AppBundle\Entity\Post;
362
+ use Doctrine\Common\DataFixtures\FixtureInterface;
363
+ use Doctrine\Common\Persistence\ObjectManager;
364
+ use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\Route;
365
+ use Symfony\Cmf\Component\Routing\RouteObjectInterface;
366
+ use Symfony\Component\DependencyInjection\ContainerAwareInterface;
367
+ use Symfony\Component\DependencyInjection\ContainerAwareTrait;
368
+
369
+ class LoadPostData implements FixtureInterface, ContainerAwareInterface
370
+ {
371
+ use ContainerAwareTrait;
372
+
373
+ /**
374
+ * @param ObjectManager $manager
375
+ */
376
+ public function load(ObjectManager $manager)
377
+ {
378
+ $post = new Post();
379
+ $post->setTitle('My Content');
380
+ $manager->persist($post);
381
+ $manager->flush(); // flush to be able to use the generated id
382
+
383
+ $contentRepository = $this->container->get('cmf_routing.content_repository');
384
+
385
+ $route = new Route();
386
+ $route->setName('my-content');
387
+ $route->setStaticPrefix('/my-content');
388
+ $route->setDefault(RouteObjectInterface::CONTENT_ID, $contentRepository->getContentId($post));
389
+ $route->setContent($post);
390
+ $post->addRoute($route); // Create the backlink from content to route
391
+
392
+ $manager->persist($post);
393
+ $manager->flush();
394
+ }
395
+ }
396
+
397
+ Now the CMF will be able to handle requests for the URL ``/my-content ``.
398
+
399
+ .. caution ::
400
+
401
+ Make sure that the content already has an id before you set it on the route.
402
+ The route to content link only works with single column ids.
403
+
404
+ The ``Post `` entity content in this example could be like this::
405
+
406
+ // src/AppBundle/Entity/Post.php
407
+ namespace AppBundle\Entity;
408
+
409
+ use Doctrine\Common\Collections\ArrayCollection;
410
+ use Doctrine\ORM\Mapping as ORM;
411
+ use Symfony\Cmf\Component\Routing\RouteObjectInterface;
412
+ use Symfony\Cmf\Component\Routing\RouteReferrersInterface;
413
+
414
+ /**
415
+ * @ORM\Table(name="post")
416
+ * @ORM\Entity(repositoryClass="AppBundle\Repository\PostRepository")
417
+ */
418
+ class Post implements RouteReferrersInterface
419
+ {
420
+ /** .. fields like title and body */
421
+
422
+ /**
423
+ * @var RouteObjectInterface[]|ArrayCollection
424
+ *
425
+ * @ORM\ManyToMany(targetEntity="Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\Route", cascade={"persist", "remove"})
426
+ */
427
+ private $routes;
428
+
429
+ public function __construct()
430
+ {
431
+ $this->routes = new ArrayCollection();
432
+ }
433
+
434
+ /**
435
+ * @return RouteObjectInterface[]|ArrayCollection
436
+ */
437
+ public function getRoutes()
438
+ {
439
+ return $this->routes;
440
+ }
441
+
442
+ /**
443
+ * @param RouteObjectInterface[]|ArrayCollection $routes
444
+ */
445
+ public function setRoutes($routes)
446
+ {
447
+ $this->routes = $routes;
448
+ }
449
+
450
+ /**
451
+ * @param RouteObjectInterface $route
452
+ *
453
+ * @return $this
454
+ */
455
+ public function addRoute($route)
456
+ {
457
+ $this->routes[] = $route;
458
+
459
+ return $this;
460
+ }
461
+
462
+ /**
463
+ * @param RouteObjectInterface $route
464
+ *
465
+ * @return $this
466
+ */
467
+ public function removeRoute($route)
468
+ {
469
+ $this->routes->removeElement($route);
470
+
471
+ return $this;
472
+ }
473
+ }
474
+
475
+ Because you set the ``content_id `` default value on the route, the controller
476
+ can expect the ``$contentDocument `` parameter. You can now configure which
477
+ controller should handle ``Post `` entities as explained in the
478
+ :ref: `Routing documentation <start-routing-getting-controller-template >`.
479
+
480
+ The ORM routes support more things, for example route parameters, requirements
481
+ and defaults. This is explained in the
482
+ :ref: `route document section <bundle-routing-document >`.
483
+
339
484
.. _bundles-routing-dynamic-generator :
340
485
341
486
URL generation with the DynamicRouter
0 commit comments