@@ -408,6 +408,151 @@ the name indicates, loads ``Route`` entities from an ORM database.
408
408
You must install the CoreBundle to use this feature if your application
409
409
does not have at least DoctrineBundle 1.3.0.
410
410
411
+ .. _bundle-routing-route-entity :
412
+
413
+ The ORM Route entity
414
+ --------------------
415
+
416
+ The example in this section applies if you use the ORM route provider
417
+ (``Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\RouteProvider ``). It uses the
418
+ ``staticPrefix `` field of the
419
+ ``Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\Route `` to find route candidates.
420
+
421
+ Symfony Cmf routing system allows us loading whatever content from a route.
422
+ That means an entity route can reference to different types of entities.
423
+ But Doctrine ORM is not able to establish that kind of mapping associations.
424
+ To do that, the ORM RouteProvider follows the pattern of FQN:id. That is, the full
425
+ model class name, then a colon, then the id. You only need to add it to the
426
+ defaults parameters of the route with the ``RouteObjectInterface::CONTENT_ID ``
427
+ key. ``cmf_routing.content_repository `` service can help you to do it easily.
428
+ A new route can be created in PHP code as follows::
429
+
430
+ // src/AppBundle/DataFixtures/ORM/LoadPostData.php
431
+ namespace AppBundle\DataFixtures\ORM;
432
+
433
+ use AppBundle\Entity\Post;
434
+ use Doctrine\Common\DataFixtures\FixtureInterface;
435
+ use Doctrine\Common\Persistence\ObjectManager;
436
+ use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\Route;
437
+ use Symfony\Cmf\Component\Routing\RouteObjectInterface;
438
+ use Symfony\Component\DependencyInjection\ContainerAwareInterface;
439
+ use Symfony\Component\DependencyInjection\ContainerAwareTrait;
440
+
441
+ class LoadPostData implements FixtureInterface, ContainerAwareInterface
442
+ {
443
+ use ContainerAwareTrait;
444
+
445
+ /**
446
+ * @param ObjectManager $manager
447
+ */
448
+ public function load(ObjectManager $manager)
449
+ {
450
+ $post = new Post();
451
+ $post->setTitle('My Content');
452
+ $manager->persist($post);
453
+ $manager->flush(); // flush to be able to use the generated id
454
+
455
+ $contentRepository = $this->container->get('cmf_routing.content_repository');
456
+
457
+ $route = new Route();
458
+ $route->setName('my-content');
459
+ $route->setStaticPrefix('/my-content');
460
+ $route->setDefault(RouteObjectInterface::CONTENT_ID, $contentRepository->getContentId($post));
461
+ $route->setContent($post);
462
+ $post->addRoute($route); // Create the backlink from content to route
463
+
464
+ $manager->persist($post);
465
+ $manager->flush();
466
+ }
467
+ }
468
+
469
+ Now the CMF will be able to handle requests for the URL ``/my-content ``.
470
+
471
+ .. caution ::
472
+
473
+ Make sure that the content already has an id before you set it on the route.
474
+ The route to content link only works with single column ids.
475
+
476
+ The ``Post `` entity content in this example could be like this::
477
+
478
+ // src/AppBundle/Entity/Post.php
479
+ namespace AppBundle\Entity;
480
+
481
+ use Doctrine\Common\Collections\ArrayCollection;
482
+ use Doctrine\ORM\Mapping as ORM;
483
+ use Symfony\Cmf\Component\Routing\RouteObjectInterface;
484
+ use Symfony\Cmf\Component\Routing\RouteReferrersInterface;
485
+
486
+ /**
487
+ * @ORM\Table(name="post")
488
+ * @ORM\Entity(repositoryClass="AppBundle\Repository\PostRepository")
489
+ */
490
+ class Post implements RouteReferrersInterface
491
+ {
492
+ /** .. fields like title and body */
493
+
494
+ /**
495
+ * @var RouteObjectInterface[]|ArrayCollection
496
+ *
497
+ * @ORM\ManyToMany(targetEntity="Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\Route", cascade={"persist", "remove"})
498
+ */
499
+ private $routes;
500
+
501
+ public function __construct()
502
+ {
503
+ $this->routes = new ArrayCollection();
504
+ }
505
+
506
+ /**
507
+ * @return RouteObjectInterface[]|ArrayCollection
508
+ */
509
+ public function getRoutes()
510
+ {
511
+ return $this->routes;
512
+ }
513
+
514
+ /**
515
+ * @param RouteObjectInterface[]|ArrayCollection $routes
516
+ */
517
+ public function setRoutes($routes)
518
+ {
519
+ $this->routes = $routes;
520
+ }
521
+
522
+ /**
523
+ * @param RouteObjectInterface $route
524
+ *
525
+ * @return $this
526
+ */
527
+ public function addRoute($route)
528
+ {
529
+ $this->routes[] = $route;
530
+
531
+ return $this;
532
+ }
533
+
534
+ /**
535
+ * @param RouteObjectInterface $route
536
+ *
537
+ * @return $this
538
+ */
539
+ public function removeRoute($route)
540
+ {
541
+ $this->routes->removeElement($route);
542
+
543
+ return $this;
544
+ }
545
+ }
546
+
547
+ Because you set the ``content_id `` default value on the route, the controller
548
+ can expect the ``$contentDocument `` parameter. You can now configure which
549
+ controller should handle ``Post `` entities as explained in the
550
+ :ref: `Routing documentation <start-routing-getting-controller-template >`.
551
+
552
+ The ORM routes support more things, for example route parameters, requirements
553
+ and defaults. This is explained in the
554
+ :ref: `route document section <bundle-routing-document >`.
555
+
411
556
.. _bundles-routing-dynamic-generator :
412
557
413
558
URL generation with the DynamicRouter
0 commit comments