aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Doctrine/Mapping/PrefixedNamingStrategy.php
blob: 861a60ea408fe137bd7d53c87f71c78e1e400ada (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php

namespace Wallabag\CoreBundle\Doctrine\Mapping;

use Doctrine\ORM\Mapping\NamingStrategy;

/**
 * Puts a prefix to each table.
 *
 * Solution from :
 *      - http://stackoverflow.com/a/23860613/569101
 *      - http://doctrine-orm.readthedocs.org/en/latest/reference/namingstrategy.html
 */
class PrefixedNamingStrategy implements NamingStrategy
{
    protected $prefix = '';

    public function __construct($prefix)
    {
        $this->prefix = (string) $prefix;
    }

    /**
     * {@inheritdoc}
     */
    public function classToTableName($className)
    {
        return strtolower($this->prefix . substr($className, strrpos($className, '\\') + 1));
    }

    /**
     * {@inheritdoc}
     */
    public function propertyToColumnName($propertyName, $className = null)
    {
        return $propertyName;
    }

    /**
     * {@inheritdoc}
     */
    public function referenceColumnName()
    {
        return 'id';
    }

    /**
     * {@inheritdoc}
     */
    public function joinColumnName($propertyName)
    {
        return $propertyName . '_' . $this->referenceColumnName();
    }

    /**
     * {@inheritdoc}
     */
    public function joinTableName($sourceEntity, $targetEntity, $propertyName = null)
    {
        // for join table we don't want to have both table concatenated AND prefixed
        // we just want the whole table to prefixed once
        // ie: not "wallabag_entry_wallabag_tag" but "wallabag_entry_tag"
        $target = substr($targetEntity, strrpos($targetEntity, '\\') + 1);

        return strtolower($this->classToTableName($sourceEntity) . '_' .$target);
    }

    /**
     * {@inheritdoc}
     */
    public function joinKeyColumnName($entityName, $referencedColumnName = null)
    {
        return strtolower($this->classToTableName($entityName) . '_' .($referencedColumnName ?: $this->referenceColumnName()));
    }
}