One2OneBeanRelationship::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2 1
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3
/*********************************************************************************
4
 * SugarCRM Community Edition is a customer relationship management program developed by
5
 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
6
7
 * SuiteCRM is an extension to SugarCRM Community Edition developed by Salesagility Ltd.
8
 * Copyright (C) 2011 - 2014 Salesagility Ltd.
9
 *
10
 * This program is free software; you can redistribute it and/or modify it under
11
 * the terms of the GNU Affero General Public License version 3 as published by the
12
 * Free Software Foundation with the addition of the following permission added
13
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
14
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
15
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
16
 *
17
 * This program is distributed in the hope that it will be useful, but WITHOUT
18
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
20
 * details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License along with
23
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
24
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25
 * 02110-1301 USA.
26
 *
27
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
28
 * SW2-130, Cupertino, CA 95014, USA. or at email address [email protected].
29
 *
30
 * The interactive user interfaces in modified source and object code versions
31
 * of this program must display Appropriate Legal Notices, as required under
32
 * Section 5 of the GNU Affero General Public License version 3.
33
 *
34
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
35
 * these Appropriate Legal Notices must retain the display of the "Powered by
36
 * SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
37
 * reasonably feasible for  technical reasons, the Appropriate Legal Notices must
38
 * display the words  "Powered by SugarCRM" and "Supercharged by SuiteCRM".
39
 ********************************************************************************/
40
41
42 1
require_once("data/Relationships/One2MBeanRelationship.php");
43
44
/**
45
 * 1-1 Bean relationship
46
 * @api
47
 */
48
class One2OneBeanRelationship extends One2MBeanRelationship
49
{
50
51 3
    public function __construct($def)
52
    {
53 3
        parent::__construct($def);
54 3
    }
55
    /**
56
     * @param  $lhs SugarBean left side bean to add to the relationship.
57
     * @param  $rhs SugarBean right side bean to add to the relationship.
58
     * @param  $additionalFields key=>value pairs of fields to save on the relationship
59
     * @return boolean true if successful
60
     */
61
    public function add($lhs, $rhs, $additionalFields = array())
62
    {
63
        $lhsLinkName = $this->lhsLink;
64
        //In a one to one, any existing links from both sides must be removed first.
65
        //one2Many will take care of the right side, so we'll do the left.
66
        $lhs->load_relationship($lhsLinkName);
67
        $this->removeAll($lhs->$lhsLinkName);
68
69
        return parent::add($lhs, $rhs, $additionalFields);
70
    }
71
72
    protected function updateLinks($lhs, $lhsLinkName, $rhs, $rhsLinkName)
73
    {
74
        //RHS and LHS only ever have one bean
75
        if (isset($lhs->$lhsLinkName))
76
            $lhs->$lhsLinkName->beans = array($rhs->id => $rhs);
77
78
        if (isset($rhs->$rhsLinkName))
79
            $rhs->$rhsLinkName->beans = array($lhs->id => $lhs);
80
    }
81
82 3
    public function getJoin($link, $params = array(), $return_array = false)
83
    {
84 3
        $linkIsLHS = $link->getSide() == REL_LHS;
85 3
        $startingTable = $link->getFocus()->table_name;
86 3
        $startingKey = $linkIsLHS ? $this->def['lhs_key'] : $this->def['rhs_key'];
87 3
        $targetTable = $linkIsLHS ? $this->def['rhs_table'] : $this->def['lhs_table'];
88 3
        $targetTableWithAlias = $targetTable;
89 3
        $targetKey = $linkIsLHS ? $this->def['rhs_key'] : $this->def['lhs_key'];
90 3
        $join_type= isset($params['join_type']) ? $params['join_type'] : ' INNER JOIN ';
91
92 3
        $join = '';
93
94
        //Set up any table aliases required
95 3
        if ( ! empty($params['join_table_alias']))
96
        {
97 3
            $targetTableWithAlias = $targetTable . " ". $params['join_table_alias'];
98 3
            $targetTable = $params['join_table_alias'];
99
        }
100
101 3
        $deleted = !empty($params['deleted']) ? 1 : 0;
102
103
        //join the related module's table
104 3
        $join .= "$join_type $targetTableWithAlias ON $targetTable.$targetKey=$startingTable.$startingKey"
105 3
               . " AND $targetTable.deleted=$deleted\n"
106
        //Next add any role filters
107 3
               . $this->getRoleWhere();
108
109 3
        if($return_array){
110
            return array(
111 3
                'join' => $join,
112 3
                'type' => $this->type,
0 ignored issues
show
Documentation introduced by
The property $type is declared private in One2MBeanRelationship. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
113 3
                'rel_key' => $targetKey,
114 3
                'join_tables' => array($targetTable),
115 3
                'where' => "",
116 3
                'select' => "$targetTable.id",
117
            );
118
        }
119
        return $join;
120
    }
121
}
122