From 17ccd8d7d1c651beb1d72b9dedb105511f836777 Mon Sep 17 00:00:00 2001 From: Thomas Nadin Date: Sat, 6 Oct 2018 14:42:35 +0100 Subject: [PATCH] Fixes bug with Image fields that prevented them loading with the parent Post's connection --- src/Field/Image.php | 13 +++++++------ tests/CorcelIntegrationTest.php | 26 ++++++++++++++++++++++++++ tests/config/bootstrap.php | 4 ++++ 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/Field/Image.php b/src/Field/Image.php index f26b4fc..cf9d568 100644 --- a/src/Field/Image.php +++ b/src/Field/Image.php @@ -3,7 +3,6 @@ namespace Corcel\Acf\Field; use Corcel\Model\Post; -use Corcel\Model\Meta\PostMeta; use Corcel\Acf\FieldInterface; use Illuminate\Database\Eloquent\Collection; @@ -133,9 +132,10 @@ protected function fillThumbnailFields(array $data) */ protected function fetchMetadataValue(Post $attachment) { - $meta = PostMeta::where('post_id', $attachment->ID) - ->where('meta_key', '_wp_attachment_metadata') - ->first(); + $meta = $this->postMeta + ->where('post_id', $attachment->ID) + ->where('meta_key', '_wp_attachment_metadata') + ->first(); return unserialize($meta->meta_value); } @@ -150,10 +150,11 @@ protected function fetchMultipleMetadataValues(Collection $attachments) $ids = $attachments->pluck('ID')->toArray(); $metadataValues = []; - $metaRows = PostMeta::whereIn("post_id", $ids) + $metaRows = $this->postMeta + ->whereIn("post_id", $ids) ->where('meta_key', '_wp_attachment_metadata') ->get(); - + foreach ($metaRows as $meta) { $metadataValues[$meta->post_id] = unserialize($meta->meta_value); } diff --git a/tests/CorcelIntegrationTest.php b/tests/CorcelIntegrationTest.php index bae0e8e..0418ff1 100644 --- a/tests/CorcelIntegrationTest.php +++ b/tests/CorcelIntegrationTest.php @@ -1,6 +1,17 @@ setDefaultConnection('default'); + } + public function testIfCorcelIntegrationIsWorking() { $post = Post::find(56); @@ -27,4 +45,12 @@ public function testFunctionHelperWithSnakeCaseFieldType() $this->assertEquals('10/13/2016', $post->acf->fake_date_picker->format('m/d/Y')); $this->assertEquals('10/13/2016', $post->acf->datePicker('fake_date_picker')->format('m/d/Y')); } + + public function testPostWithNonDefaultConnectionsLoadsFieldsUsingSameConnection() + { + Eloquent::getConnectionResolver()->setDefaultConnection('missing'); + + $post = AlternatePost::find(38); + $this->assertEquals('http://wordpress.corcel.dev/wp-content/uploads/2016/10/maxresdefault-1.jpg', $post->acf->image('fake_image')->url); + } } diff --git a/tests/config/bootstrap.php b/tests/config/bootstrap.php index 6b46dfc..f565c60 100644 --- a/tests/config/bootstrap.php +++ b/tests/config/bootstrap.php @@ -11,3 +11,7 @@ 'password' => '', 'host' => '127.0.0.1', ]); + +// Create a copy of the default connection called alternate: +$config = $capsule->getContainer()->make('config')->get('database.connections')['default']; +$capsule->addConnection($config, 'alternate');