Completed
Branch develop (f7dc53)
by Anton
05:49
created

FindTrait::findByPK()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
namespace Spiral\ODM\Traits;
9
10
use Spiral\ODM\Document;
11
use Spiral\ODM\Entities\DocumentSelector;
12
use Spiral\ODM\Entities\DocumentSource;
13
use Spiral\ODM\ODM;
14
use Spiral\ORM\Exceptions\ORMException;
15
use Spiral\ORM\RecordEntity;
16
17
/**
18
 * Static record functionality including create and find methods.
19
 */
20
trait FindTrait
21
{
22
    /**
23
     * Find multiple records based on provided query.
24
     *
25
     * Example:
26
     * User::find(['status' => 'active'], ['profile']);
27
     *
28
     * @param array $where Selection WHERE statement.
29
     * @return DocumentSelector
30
     */
31
    public static function find($where = [])
32
    {
33
        return static::source()->find($where);
34
    }
35
36
    /**
37
     * Fetch one record based on provided query or return null. Use second argument to specify
38
     * relations to be loaded.
39
     *
40
     * Example:
41
     * User::findOne(['name' => 'Wolfy-J'], ['profile'], ['id' => 'DESC']);
42
     *
43
     * @param array $where  Selection WHERE statement.
44
     * @param array $sortBy Sort by.
45
     * @return RecordEntity|null
46
     */
47
    public static function findOne($where = [], array $sortBy = [])
48
    {
49
        return static::source()->findOne($where, $sortBy);
50
    }
51
52
    /**
53
     * Find record using it's primary key. Relation data can be preloaded with found record.
54
     *
55
     * Example:
56
     * User::findByID(1, ['profile']);
57
     *
58
     * @param mixed $primaryKey Primary key.
59
     * @return Document|null
60
     */
61
    public static function findByPK($primaryKey)
62
    {
63
        return static::source()->findByPK($primaryKey);
64
    }
65
66
    /**
67
     * Instance of ORM Selector associated with specific document.
68
     *
69
     * @see   Component::staticContainer()
70
     * @param ODM $odm ODM component, global container will be called if not instance provided.
71
     * @return DocumentSource
72
     * @throws ORMException
73
     */
74 View Code Duplication
    public static function source(ODM $odm = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76
        /**
77
         * Using global container as fallback.
78
         *
79
         * @var ODM $odm
80
         */
81
        if (empty($odm)) {
82
            //Using global container as fallback
83
            $odm = self::staticContainer()->get(ODM::class);
84
        }
85
86
        return $odm->source(static::class);
87
    }
88
}