Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [SF][Symfony][Symfony2]exception `Unable to find entity` po dodaniu FOSUserBundle
Forum PHP.pl > Forum > PHP > Frameworki
twojastara
Robię ćwiczenia z książką (opartą o symf 2.0.10, używając u siebie `Symfony 2.6.1` i FOSUM 2.0).

Zrobiłem projekt z CRUD wyświetlający dane z bazy (Mountain controller w My/BackendBundle)

Teraz chcę wyświetlić projekt tylko dla zalogowanego użytkownika `admin` (with ROLE_SUPER_ADMIN, który istnieje w tabeli `for_user`w tej samej bazie co dane z CRUD).

Ale gdy otwieram ../web/ skutkuje to wyjątkiem `Unable to find Mountain entity` z `Mountain controller showAction($id)`

  1. #\src\My\BackendBundle\Controller\MountainController.php
  2. /**
  3.   * Finds and displays a Mountain entity.
  4.   *
  5.   * @Route("/{id}", name="mountain_show")
  6.   * @Method("GET")
  7.   * @Template()
  8.   */
  9. public function showAction($id)
  10. {
  11. $em = $this->getDoctrine()->getManager();
  12.  
  13. $entity = $em->getRepository('MyBackendBundle:Mountain')->find($id);
  14.  
  15. if (!$entity) {
  16. throw $this->createNotFoundException('Unable to find Mountain entity.');
  17. }
  18.  
  19. $deleteForm = $this->createDeleteForm($id);
  20.  
  21. return array(
  22. 'entity' => $entity,
  23. 'delete_form' => $deleteForm->createView(),
  24. );


  1. #\app\AppKernel.php
  2. new FOS\UserBundle\FOSUserBundle(),
  3. new My\UserBundle\MyUserBundle(),
  4. new My\BackendBundle\MyBackendBundle(),


Kod
#\app\config\security.yml
security:
    providers:
        fos_userbundle:
            id: fos_user.user_manager

    encoders:
        FOS\UserBundle\Model\UserInterface: sha512

    firewalls:
        main:
            pattern: ^/
            logout:       true
            anonymous:    true
            form_login:
                provider:                       fos_userbundle
                csrf_provider:                  form.csrf_provider
                login_path:                     /login
                use_forward:                    false
                check_path:                     /login_check
                post_only:                      true
                always_use_default_target_path: false
                default_target_path:            /
                target_path_parameter:          _target_path
                use_referer:                    false
                failure_path:                   null
                failure_forward:                false
                username_parameter:             _username
                password_parameter:             _password
                csrf_parameter:                 _csrf_token
                intention:                      authenticate

    access_control:
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/, role: ROLE_SUPER_ADMIN }


Kod
#\app\config\config.yml
imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: services.yml }

framework:
    #esi:             ~
    translator:      ~
    secret:          "%secret%"
    router:
        resource: "%kernel.root_dir%/config/routing.yml"
        strict_requirements: ~
    form:            ~
    csrf_protection: ~
    validation:      { enable_annotations: true }
    templating:
        engines: ['twig']
        #assets_version: SomeVersionScheme
    default_locale:  "%locale%"
    trusted_hosts:   ~
    trusted_proxies: ~
    session:
        # default_locale: pl
        # handler_id set to null will use default session handler from php.ini
        handler_id:  ~
    fragments:       ~
    http_method_override: true

# Twig Configuration
twig:
    debug:            "%kernel.debug%"
    strict_variables: "%kernel.debug%"

# Assetic Configuration
assetic:
    debug:          "%kernel.debug%"
    use_controller: false
    bundles:        [ ]
    #java: /usr/bin/java
    filters:
        cssrewrite: ~
        #closure:
        #    jar: "%kernel.root_dir%/Resources/java/compiler.jar"
        #yui_css:
        #    jar: "%kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar"

# Doctrine Configuration
doctrine:
    dbal:
        driver:   "%database_driver%"
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8
        # if using pdo_sqlite as your database driver:
        #   1. add the path in parameters.yml
        #     e.g. database_path: "%kernel.root_dir%/data/data.db3"
        #   2. Uncomment database_path in parameters.yml.dist
        #   3. Uncomment next line:
        #     path:     "%database_path%"

    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        auto_mapping: true

# Swiftmailer Configuration
swiftmailer:
    transport: "%mailer_transport%"
    host:      "%mailer_host%"
    username:  "%mailer_user%"
    password:  "%mailer_password%"
    spool:     { type: memory }


stof_doctrine_extensions:
    default_locale: en_US
    orm:
        default:
            tree: false
            loggable: false
            timestampable: false
            sluggable: false
            translatable: false

fos_user:
    db_driver: orm
    firewall_name: main
    user_class: My\UserBundle\Entity\User
#\app\config\routing.yml
MyBackendBundle:
    resource: "@MyBackendBundle/Controller/"
    type:     annotation
    prefix:   /

fos_user_security:
    resource: "@FOSUserBundle/Resources/config/routing/security.xml"


Kod
#\parameters.yml
# This file is auto-generated during the composer install
parameters:
    database_driver: pdo_mysql
    database_host: 127.0.0.1
    database_port: null
    database_name: koronaziemi
    database_user: root
    database_password: null
    mailer_transport: smtp
    mailer_host: 127.0.0.1
    mailer_user: null
    mailer_password: null
    locale: pl
    secret: ThisTokenIsNotSoSecretChangeIt


Krótko mówiąc. Póki nie dodam linii
Kod
- { path: ^/, role: ROLE_SUPER_ADMIN }
w security.yml aplikacja wyświetla zawartość. Po dodaniu linii zaczyna jej brakować encji,z której wcześniej korzystała.
kpt_lucek
A encja znajduje się w katalogu Entity w MyBackendBundle ?

Dodatkowo pokaż mapowanie
twojastara


  1. #\src\My\BackendBundle\Entity\Mountain.php
  2. <?php
  3.  
  4. namespace My\BackendBundle\Entity;
  5.  
  6. use Doctrine\ORM\Mapping as ORM;
  7.  
  8. /**
  9.  * Mountain
  10.  *
  11.  * @ORM\Table()
  12.  * @ORM\Entity
  13.  */
  14. class Mountain
  15. {
  16. /**
  17.   * @var integer
  18.   *
  19.   * @ORM\Column(name="id", type="integer")
  20.   * @ORM\Id
  21.   * @ORM\GeneratedValue(strategy="AUTO")
  22.   */
  23. private $id;
  24.  
  25. /**
  26.   * @var string
  27.   *
  28.   * @ORM\Column(name="name", type="string", length=255)
  29.   */
  30. private $name;
  31.  
  32. /**
  33.   * @var string
  34.   *
  35.   * @ORM\Column(name="continent", type="string", length=255)
  36.   */
  37. private $continent;
  38.  
  39. /**
  40.   * @var integer
  41.   *
  42.   * @ORM\Column(name="height", type="integer")
  43.   */
  44. private $height;
  45.  
  46.  
  47. /**
  48.   * Get id
  49.   *
  50.   * @return integer
  51.   */
  52. public function getId()
  53. {
  54. return $this->id;
  55. }
  56.  
  57. /**
  58.   * Set name
  59.   *
  60.   * @param string $name
  61.   * @return Mountain
  62.   */
  63. public function setName($name)
  64. {
  65. $this->name = $name;
  66.  
  67. return $this;
  68. }
  69.  
  70. /**
  71.   * Get name
  72.   *
  73.   * @return string
  74.   */
  75. public function getName()
  76. {
  77. return $this->name;
  78. }
  79.  
  80. /**
  81.   * Set continent
  82.   *
  83.   * @param string $continent
  84.   * @return Mountain
  85.   */
  86. public function setContinent($continent)
  87. {
  88. $this->continent = $continent;
  89.  
  90. return $this;
  91. }
  92.  
  93. /**
  94.   * Get continent
  95.   *
  96.   * @return string
  97.   */
  98. public function getContinent()
  99. {
  100. return $this->continent;
  101. }
  102.  
  103. /**
  104.   * Set height
  105.   *
  106.   * @param integer $height
  107.   * @return Mountain
  108.   */
  109. public function setHeight($height)
  110. {
  111. $this->height = $height;
  112.  
  113. return $this;
  114. }
  115.  
  116. /**
  117.   * Get height
  118.   *
  119.   * @return integer
  120.   */
  121. public function getHeight()
  122. {
  123. return $this->height;
  124. }
  125. }
kpt_lucek
Dostajesz Exception że encja nie istnieje, bo fizyczny rekord dla danego $id nie istnieje w bazie.
twojastara
nie rozumiem dlaczego tu się w ogóle odzywa wyjątek z showAction, bez FOSUB adres ../web/ pokazuje indexAction. I jaki on niby parametr bierze za $id, że go nie może znaleźć w tabeli.

-----------
edit:

znalazłem rozwiązanie.

var_dump($id); ustawiony w showAction przed linią
$entity = $em->getRepository('MyBackendBundle:Mountain')->find($id);
drukuje
Kod
string [b]'login'[/b] (length=5)


gdy otwieram localhost/project/web/
i automatycznie przekierowuje na
localhost/project/web/login

czyli pobiera z URLa login jako parametr find($id) szuka takiego klucza w bazie danych.


Gdy dodam parametr requirements={"id": "\d+"} do route przed showAction
* @Route("/{id}", name="mountain_show", requirements={"id": "\d+"}))

to jako parametr nie bierze pod uwagę stringów. I wyświetla formularz do logowania i po zalogowaniu wyświetla wszystko jak należy.


Ale nie chce mi sie wierzyć, że trzeba uciekać się do takich trików za każdym razem gdy używa się logowania na stronie, która pobiera parametr z URL. Coś pewnie w plikach konfiguracyjnych musi być. Ma ktoś jakiś pomysł?
------------------------------------------------

Okazało się, że autor podręcznika pominął krok, w którym po prostu dla tej akcji, która została w całości wygenerowana razem z kontrolerem komendą doctrine:generate:crud zmodyfikował oryginalne @Route("/{id}" na @Route("/{id}/show" nie wspominając o tym.

wywalcie ten temat bo nie mogę patrzeć na swoje 3 dniowe męki.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.