Skip to content

Commit

Permalink
Merge pull request corcel#85 from thefreshuk/db-connection-fix
Browse files Browse the repository at this point in the history
Fixing DB connection issue for general case
  • Loading branch information
jgrossi committed Mar 4, 2016
2 parents fccce4a + bdc9493 commit 7c33320
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 46 deletions.
4 changes: 4 additions & 0 deletions src/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@

class Comment extends Model
{
const CREATED_AT = 'comment_date';
const UPDATED_AT = null;

protected $table = 'comments';
protected $primaryKey = 'comment_ID';
protected $dates = ['comment_date'];

/**
* Post relationship
Expand Down
28 changes: 25 additions & 3 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
namespace Corcel;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand All @@ -24,10 +25,9 @@ public function hasMany($related, $foreignKey = null, $localKey = null)

$localKey = $localKey ?: $this->getKeyName();

return new HasMany($instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey);
return new HasMany($instance->newQuery(), $this, $foreignKey, $localKey);
}


public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null)
{
if (is_null($relation)) {
Expand All @@ -49,7 +49,6 @@ public function belongsTo($related, $foreignKey = null, $otherKey = null, $relat

return new BelongsTo($query, $this, $foreignKey, $otherKey, $relation);
}


public function belongsToMany($related, $table = null, $foreignKey = null, $otherKey = null, $relation = null)
{
Expand All @@ -72,4 +71,27 @@ public function belongsToMany($related, $table = null, $foreignKey = null, $othe

return new BelongsToMany($query, $this, $table, $foreignKey, $otherKey, $relation);
}

public function getRelationValue($key)
{
$relation = parent::getRelationValue($key);

if ($relation instanceof Collection) {
$relation->each(function ($model) {
$this->setRelationConnection($model);
});
return $relation;
}

$this->setRelationConnection($relation);

return $relation;
}

protected function setRelationConnection($model)
{
if ($model instanceof Eloquent) {
$model->setConnection($this->getConnectionName());
}
}
}
13 changes: 1 addition & 12 deletions src/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class User extends Model implements Authenticatable, CanResetPassword
public function setUpdatedAtAttribute($value) {
}


/**
* The accessors to append to the model's array form.
*
Expand Down Expand Up @@ -287,15 +287,4 @@ public function resetPassword($password)

$this->user_pass = $passwordService->wp_hash_password($password);
}

protected function getRelationshipFromMethod($method)
{
$relations = parent::getRelationshipFromMethod($method);

$relations->listen('set', function ($relation) {
$relation->setConnection($this->getConnectionName());
});

return $this->relations[$method] = $relations;
}
}
29 changes: 3 additions & 26 deletions src/UserMetaCollection.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
<?php

/**
* Corcel\UserMetaCollection
*
*
* @author Mickael Burguet <www.rundef.com>
*/

Expand All @@ -13,19 +13,17 @@
class UserMetaCollection extends Collection
{
protected $changedKeys = [];
protected $listeners = [];

/**
* Search for the desired key and return only the row that represent it
*
*
* @param string $key
* @return string
*/
public function __get($key)
{
foreach ($this->items as $item) {
if ($item->meta_key == $key) {
$this->notify('get', [$item]);
return $item->meta_value;
}
}
Expand All @@ -37,7 +35,6 @@ public function __set($key, $value)

foreach ($this->items as $item) {
if ($item->meta_key == $key) {
$this->notify('set', [$item]);
$item->meta_value = $value;
return;
}
Expand All @@ -48,8 +45,6 @@ public function __set($key, $value)
'meta_value' => $value,
));

$this->notify('set', [$item]);

$this->push($item);
}

Expand All @@ -62,22 +57,4 @@ public function save($userId)
}
});
}

public function listen($event, callable $listener)
{
if (! isset($this->listeners[$event])) {
$this->listeners[$event] = [];
}

$this->listeners[$event][] = $listener;
}

public function notify($event, $args = [])
{
if (isset($this->listeners[$event])) {
foreach ($this->listeners[$event] as $listener) {
call_user_func_array($listener, $args);
}
}
}
}
26 changes: 21 additions & 5 deletions tests/CommentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ public function testCommentConstructor()
$comment = new Comment;
$this->assertTrue($comment instanceof \Corcel\Comment);
}

public function testCommentId()
{
$comment = Comment::find(1);

if ($comment) {
//$this->assertInternalType('integer', $comment->comment_ID);
$this->assertEquals($comment->comment_ID, 1);
} else {
$this->assertNull($comment);
}
}

public function testCommentPost()
{
$comment = Comment::find(1);

$this->assertTrue($comment->Post()->first() instanceof \Corcel\Post);
$this->assertEquals($comment->Post()->first()->ID, 1);
}
Expand All @@ -42,7 +42,7 @@ public function testCommentPostId()
}

public function testOriginal()
{
{
$comment = Comment::find(2);

$this->assertTrue($comment->original()->first() instanceof \Corcel\Comment);
Expand Down Expand Up @@ -72,4 +72,20 @@ public function testCommentHasReplies()
$this->assertInternalType('boolean', $comment->hasReplies());
$this->assertTrue($comment->hasReplies());
}

public function testCommentEnforceConnection()
{
$comment = new Comment;
$comment->setConnection('no_prefix');
$comment->comment_content = 'Test content';
$comment->save();

$post = new Post;
$post->content = 'Test';
$comment->post()->associate($post);
$comment->save();

$this->assertEquals('no_prefix', $comment->getConnectionName());
$this->assertEquals('no_prefix', $comment->post->getConnectionName());
}
}

0 comments on commit 7c33320

Please sign in to comment.