add fetch_one method to repository
This commit is contained in:
parent
8cb4d0061e
commit
5d2a9f27ad
|
@ -510,6 +510,31 @@ abstract class Repository {
|
|||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch one Entity based on query args.
|
||||
*
|
||||
* Note: Does not work with Item_Metadata Repository
|
||||
*
|
||||
* @param array $args Query Args as expected by fetch
|
||||
*
|
||||
* @return false|\Tainacan\Entities The entity or false if none was found
|
||||
*/
|
||||
public function fetch_one($args) {
|
||||
if ($this->get_name() == 'Item_Metadata') {
|
||||
return false;
|
||||
}
|
||||
|
||||
$args['posts_per_page'] = 1;
|
||||
|
||||
$results = $this->fetch($args, 'OBJECT');
|
||||
|
||||
if (is_array($results) && sizeof($results) > 0 && $results[0] instanceof \Tainacan\Entities\Entity) {
|
||||
return $results[0];
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $object
|
||||
*
|
||||
|
|
|
@ -131,5 +131,46 @@ class Objects extends TAINACAN_UnitTestCase {
|
|||
$this->assertEquals('', $newCol->get_name());
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
function test_fetch_one() {
|
||||
|
||||
|
||||
$collection = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
array(
|
||||
'name' => 'teste',
|
||||
'status' => 'publish'
|
||||
),
|
||||
true
|
||||
);
|
||||
|
||||
$collection2 = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
array(
|
||||
'name' => 'teste2',
|
||||
'status' => 'publish'
|
||||
),
|
||||
true
|
||||
);
|
||||
|
||||
$collection3 = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
array(
|
||||
'name' => 'teste3',
|
||||
'status' => 'publish'
|
||||
),
|
||||
true
|
||||
);
|
||||
|
||||
|
||||
|
||||
$Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance();
|
||||
|
||||
$one = $Tainacan_Collections->fetch_one(['name' => 'teste2']);
|
||||
|
||||
$this->assertTrue( $one instanceof \Tainacan\Entities\Collection );
|
||||
$this->assertEquals( 'teste2', $one->get_name() );
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue