Browse Source

Use newEntity() to create session

Corey Taylor 5 years ago
parent
commit
ccec0ce00d

+ 8 - 10
src/Http/Session/DatabaseSession.php

@@ -18,7 +18,6 @@ declare(strict_types=1);
  */
 namespace Cake\Http\Session;
 
-use Cake\ORM\Entity;
 use Cake\ORM\Locator\LocatorAwareTrait;
 use SessionHandlerInterface;
 
@@ -150,14 +149,16 @@ class DatabaseSession implements SessionHandlerInterface
         if (!$id) {
             return false;
         }
-        $expires = time() + $this->_timeout;
-        $record = compact('data', 'expires');
+
         /** @var string $pkField */
         $pkField = $this->_table->getPrimaryKey();
-        $record[$pkField] = $id;
-        $result = $this->_table->save(new Entity($record));
+        $session = $this->_table->newEntity([
+            $pkField => $id,
+            'data' => $data,
+            'expires' => time() + $this->_timeout,
+        ], ['accessibleFields' => ['id' => true]]);
 
-        return (bool)$result;
+        return (bool)$this->_table->save($session);
     }
 
     /**
@@ -170,10 +171,7 @@ class DatabaseSession implements SessionHandlerInterface
     {
         /** @var string $pkField */
         $pkField = $this->_table->getPrimaryKey();
-        $this->_table->delete(new Entity(
-            [$pkField => $id],
-            ['markNew' => false]
-        ));
+        $this->_table->deleteAll([$pkField => $id]);
 
         return true;
     }

+ 1 - 1
tests/TestCase/Http/Session/DatabaseSessionTest.php

@@ -140,7 +140,7 @@ class DatabaseSessionTest extends TestCase
      */
     public function testDestroy()
     {
-        $this->storage->write('foo', 'Some value');
+        $this->assertTrue($this->storage->write('foo', 'Some value'));
 
         $this->assertTrue($this->storage->destroy('foo'), 'Destroy failed');
         $this->assertSame('', $this->storage->read('foo'), 'Value still present.');

+ 17 - 0
tests/test_app/TestApp/Model/Entity/Session.php

@@ -0,0 +1,17 @@
+<?php
+declare(strict_types=1);
+
+namespace TestApp\Model\Entity;
+
+use Cake\ORM\Entity;
+
+/**
+ * Marks id as protected
+ */
+class Session extends Entity
+{
+    protected $_accessible = [
+        'id' => false,
+        '*' => true,
+    ];
+}

+ 10 - 0
tests/test_app/TestApp/Model/Table/SessionsTable.php

@@ -0,0 +1,10 @@
+<?php
+declare(strict_types=1);
+
+namespace TestApp\Model\Table;
+
+use Cake\ORM\Table;
+
+class SessionsTable extends Table
+{
+}