Browse Source

Merge pull request #12222 from cakephp/entity-docs

Fix EntityTrait examples and route parsing with brace style routes.
Mark Story 7 years ago
parent
commit
34e90fa31e

+ 2 - 2
src/Datasource/EntityTrait.php

@@ -934,7 +934,7 @@ trait EntityTrait
      *
      * ```
      * // Sets the error messages for multiple fields at once
-     * $entity->errors(['salary' => ['message'], 'name' => ['another message']);
+     * $entity->setErrors(['salary' => ['message'], 'name' => ['another message']);
      * ```
      *
      * @param array $fields The array of errors to set.
@@ -960,7 +960,7 @@ trait EntityTrait
      *
      * ```
      * // Sets the error messages for a single field
-     * $entity->errors('salary', ['must be numeric', 'must be a positive number']);
+     * $entity->setError('salary', ['must be numeric', 'must be a positive number']);
      * ```
      *
      * @param string $field The field to get errors for, or the array of errors to set.

+ 4 - 0
src/Routing/Route/Route.php

@@ -866,6 +866,10 @@ class Route
         if ($routeKey !== false) {
             return substr($this->template, 0, $routeKey);
         }
+        $routeKey = strpos($this->template, '{');
+        if ($routeKey !== false && strpos($this->template, '}') !== false) {
+            return substr($this->template, 0, $routeKey);
+        }
         $star = strpos($this->template, '*');
         if ($star !== false) {
             $path = rtrim(substr($this->template, 0, $star), '/');

+ 20 - 0
tests/TestCase/Routing/Route/RouteTest.php

@@ -1728,6 +1728,26 @@ class RouteTest extends TestCase
         $route = new Route('/:controller/:action/*');
         $this->assertEquals('/', $route->staticPath());
 
+        $route = new Route('/api/{/:action/*');
+        $this->assertEquals('/api/{/', $route->staticPath());
+
+        $route = new Route('/books/reviews', ['controller' => 'Reviews', 'action' => 'index']);
+        $this->assertEquals('/books/reviews', $route->staticPath());
+    }
+
+    /**
+     * Test getting the static path for a route.
+     *
+     * @return void
+     */
+    public function testStaticPathBrace()
+    {
+        $route = new Route('/pages/{id}/*', ['controller' => 'Pages', 'action' => 'display']);
+        $this->assertEquals('/pages/', $route->staticPath());
+
+        $route = new Route('/{controller}/{action}/*');
+        $this->assertEquals('/', $route->staticPath());
+
         $route = new Route('/books/reviews', ['controller' => 'Reviews', 'action' => 'index']);
         $this->assertEquals('/books/reviews', $route->staticPath());
     }