Skip to content

Bugfix for Image fields when using a non-default connection #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/Field/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
26 changes: 26 additions & 0 deletions tests/CorcelIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
<?php

use Corcel\Model\Post;
use Illuminate\Database\Eloquent\Model as Eloquent;

class AlternatePost extends Post
{
/**
* The connection name for the model.
*
* @var string
*/
protected $connection = 'alternate';
}

/**
* Class CorcelIntegrationTest.
Expand All @@ -9,6 +20,13 @@
*/
class CorcelIntegrationTest extends PHPUnit_Framework_TestCase
{
public function tearDown()
{
parent::tearDown();

Eloquent::getConnectionResolver()->setDefaultConnection('default');
}

public function testIfCorcelIntegrationIsWorking()
{
$post = Post::find(56);
Expand All @@ -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);
}
}
4 changes: 4 additions & 0 deletions tests/config/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');