@@ -86,7 +86,7 @@ |
||
86 | 86 | * @param string $columnName |
87 | 87 | * @param string $order |
88 | 88 | */ |
89 | - public function __construct($tableName=null, $columnName=null, $order=null) { |
|
89 | + public function __construct($tableName = null, $columnName = null, $order = null) { |
|
90 | 90 | $this->tableName = $tableName; |
91 | 91 | $this->columnName = $columnName; |
92 | 92 | $this->order = $order; |
@@ -59,7 +59,7 @@ |
||
59 | 59 | * |
60 | 60 | * @param FilterInterface $filter |
61 | 61 | */ |
62 | - public function __construct($filter=null) { |
|
62 | + public function __construct($filter = null) { |
|
63 | 63 | $this->filter = $filter; |
64 | 64 | } |
65 | 65 |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | * |
63 | 63 | * @param string $sqlString |
64 | 64 | */ |
65 | - public function __construct($sqlString=null) { |
|
65 | + public function __construct($sqlString = null) { |
|
66 | 66 | $this->sqlString = $sqlString; |
67 | 67 | } |
68 | 68 | |
@@ -95,32 +95,32 @@ discard block |
||
95 | 95 | // First, let's remove all the stuff in quotes: |
96 | 96 | |
97 | 97 | // Let's remove all the \' found |
98 | - $work_str = str_replace("\\'",'',$this->sqlString); |
|
98 | + $work_str = str_replace("\\'", '', $this->sqlString); |
|
99 | 99 | // Now, let's split the string using ' |
100 | 100 | $work_table = explode("'", $work_str); |
101 | 101 | |
102 | - if (count($work_table)==0) |
|
102 | + if (count($work_table) == 0) |
|
103 | 103 | return ''; |
104 | 104 | |
105 | 105 | // if we start with a ', let's remove the first text |
106 | - if (strstr($work_str,"'")===0) |
|
106 | + if (strstr($work_str, "'") === 0) |
|
107 | 107 | array_shift($work_table); |
108 | 108 | |
109 | - if (count($work_table)==0) |
|
109 | + if (count($work_table) == 0) |
|
110 | 110 | return ''; |
111 | 111 | |
112 | 112 | // Now, let's take only the stuff outside the quotes. |
113 | 113 | $work_str2 = ''; |
114 | 114 | |
115 | - $i=0; |
|
115 | + $i = 0; |
|
116 | 116 | foreach ($work_table as $str_fragment) { |
117 | - if (($i % 2) == 0) |
|
117 | + if (($i%2) == 0) |
|
118 | 118 | $work_str2 .= $str_fragment.' '; |
119 | 119 | $i++; |
120 | 120 | } |
121 | 121 | |
122 | 122 | // Now, let's run a regexp to find all the strings matching the pattern xxx.yyy |
123 | - preg_match_all('/([a-zA-Z_](?:[a-zA-Z0-9_]*))\.(?:[a-zA-Z_](?:[a-zA-Z0-9_]*))/', $work_str2,$capture_result); |
|
123 | + preg_match_all('/([a-zA-Z_](?:[a-zA-Z0-9_]*))\.(?:[a-zA-Z_](?:[a-zA-Z0-9_]*))/', $work_str2, $capture_result); |
|
124 | 124 | |
125 | 125 | $tables_used = $capture_result[1]; |
126 | 126 | // remove doubles: |
@@ -99,23 +99,27 @@ |
||
99 | 99 | // Now, let's split the string using ' |
100 | 100 | $work_table = explode("'", $work_str); |
101 | 101 | |
102 | - if (count($work_table)==0) |
|
103 | - return ''; |
|
102 | + if (count($work_table)==0) { |
|
103 | + return ''; |
|
104 | + } |
|
104 | 105 | |
105 | 106 | // if we start with a ', let's remove the first text |
106 | - if (strstr($work_str,"'")===0) |
|
107 | - array_shift($work_table); |
|
107 | + if (strstr($work_str,"'")===0) { |
|
108 | + array_shift($work_table); |
|
109 | + } |
|
108 | 110 | |
109 | - if (count($work_table)==0) |
|
110 | - return ''; |
|
111 | + if (count($work_table)==0) { |
|
112 | + return ''; |
|
113 | + } |
|
111 | 114 | |
112 | 115 | // Now, let's take only the stuff outside the quotes. |
113 | 116 | $work_str2 = ''; |
114 | 117 | |
115 | 118 | $i=0; |
116 | 119 | foreach ($work_table as $str_fragment) { |
117 | - if (($i % 2) == 0) |
|
118 | - $work_str2 .= $str_fragment.' '; |
|
120 | + if (($i % 2) == 0) { |
|
121 | + $work_str2 .= $str_fragment.' '; |
|
122 | + } |
|
119 | 123 | $i++; |
120 | 124 | } |
121 | 125 |
@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | * |
50 | 50 | * @param array<FilterInterface> $filters |
51 | 51 | */ |
52 | - public function __construct(array $filters=array()) { |
|
52 | + public function __construct(array $filters = array()) { |
|
53 | 53 | $this->filters = $filters; |
54 | 54 | } |
55 | 55 | |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | } |
92 | 92 | |
93 | 93 | if (count($filters_sql)>0) { |
94 | - return '('.implode(' AND ',$filters_sql).')'; |
|
94 | + return '('.implode(' AND ', $filters_sql).')'; |
|
95 | 95 | } else { |
96 | 96 | return ''; |
97 | 97 | } |
@@ -114,7 +114,7 @@ discard block |
||
114 | 114 | throw new TDBMException("Error in AndFilter: One of the parameters is not a filter."); |
115 | 115 | } |
116 | 116 | |
117 | - $tables = array_merge($tables,$filter->getUsedTables()); |
|
117 | + $tables = array_merge($tables, $filter->getUsedTables()); |
|
118 | 118 | } |
119 | 119 | // Remove tables in double. |
120 | 120 | $tables = array_flip(array_flip($tables)); |
@@ -9,14 +9,14 @@ |
||
9 | 9 | */ |
10 | 10 | class FilterUtils |
11 | 11 | { |
12 | - /** |
|
13 | - * @param string|null|DateTimeInterface $value |
|
14 | - */ |
|
15 | - public static function valueToSql($value, Connection $dbConnection) { |
|
16 | - if ($value instanceof DateTimeInterface) { |
|
17 | - return "'".$value->format('Y-m-d H:i:s')."'"; |
|
18 | - } else { |
|
19 | - return $dbConnection->quote($value); |
|
20 | - } |
|
21 | - } |
|
12 | + /** |
|
13 | + * @param string|null|DateTimeInterface $value |
|
14 | + */ |
|
15 | + public static function valueToSql($value, Connection $dbConnection) { |
|
16 | + if ($value instanceof DateTimeInterface) { |
|
17 | + return "'".$value->format('Y-m-d H:i:s')."'"; |
|
18 | + } else { |
|
19 | + return $dbConnection->quote($value); |
|
20 | + } |
|
21 | + } |
|
22 | 22 | } |
@@ -98,8 +98,8 @@ discard block |
||
98 | 98 | * |
99 | 99 | * @param string $tableName |
100 | 100 | * @param string $columnName |
101 | - * @param string $value1 |
|
102 | - * @param string $value2 |
|
101 | + * @param string $value1 |
|
102 | + * @param string $value2 |
|
103 | 103 | */ |
104 | 104 | public function __construct($tableName=null, $columnName=null, $value1=null, $value2=null) { |
105 | 105 | $this->tableName = $tableName; |
@@ -113,9 +113,9 @@ discard block |
||
113 | 113 | * |
114 | 114 | * @param Connection $dbConnection |
115 | 115 | * @return string |
116 | - * @throws \Mouf\Database\TDBM\TDBMException |
|
116 | + * @throws \Mouf\Database\TDBM\TDBMException |
|
117 | 117 | */ |
118 | - public function toSql(Connection $dbConnection) { |
|
118 | + public function toSql(Connection $dbConnection) { |
|
119 | 119 | if ($this->enableCondition != null && !$this->enableCondition->isOk()) { |
120 | 120 | return ""; |
121 | 121 | } |
@@ -101,7 +101,7 @@ |
||
101 | 101 | * @param string $value1 |
102 | 102 | * @param string $value2 |
103 | 103 | */ |
104 | - public function __construct($tableName=null, $columnName=null, $value1=null, $value2=null) { |
|
104 | + public function __construct($tableName = null, $columnName = null, $value1 = null, $value2 = null) { |
|
105 | 105 | $this->tableName = $tableName; |
106 | 106 | $this->columnName = $columnName; |
107 | 107 | $this->value1 = $value1; |
@@ -68,9 +68,9 @@ discard block |
||
68 | 68 | * |
69 | 69 | * @param Connection $dbConnection |
70 | 70 | * @return string |
71 | - * @throws TDBMException |
|
71 | + * @throws TDBMException |
|
72 | 72 | */ |
73 | - public function toSql(Connection $dbConnection) { |
|
73 | + public function toSql(Connection $dbConnection) { |
|
74 | 74 | if ($this->enableCondition != null && !$this->enableCondition->isOk()) { |
75 | 75 | return ""; |
76 | 76 | } |
@@ -102,22 +102,22 @@ discard block |
||
102 | 102 | * |
103 | 103 | * @return array<string> |
104 | 104 | */ |
105 | - public function getUsedTables() { |
|
106 | - if ($this->enableCondition != null && !$this->enableCondition->isOk()) { |
|
107 | - return array(); |
|
108 | - } |
|
109 | - |
|
110 | - $tables = array(); |
|
111 | - foreach ($this->filters as $filter) { |
|
112 | - |
|
113 | - if (!$filter instanceof FilterInterface) { |
|
114 | - throw new TDBMException("Error in OrFilter: One of the parameters is not a filter."); |
|
115 | - } |
|
116 | - |
|
117 | - $tables = array_merge($tables,$filter->getUsedTables()); |
|
118 | - } |
|
119 | - // Remove tables in double. |
|
120 | - $tables = array_flip(array_flip($tables)); |
|
121 | - return $tables; |
|
122 | - } |
|
105 | + public function getUsedTables() { |
|
106 | + if ($this->enableCondition != null && !$this->enableCondition->isOk()) { |
|
107 | + return array(); |
|
108 | + } |
|
109 | + |
|
110 | + $tables = array(); |
|
111 | + foreach ($this->filters as $filter) { |
|
112 | + |
|
113 | + if (!$filter instanceof FilterInterface) { |
|
114 | + throw new TDBMException("Error in OrFilter: One of the parameters is not a filter."); |
|
115 | + } |
|
116 | + |
|
117 | + $tables = array_merge($tables,$filter->getUsedTables()); |
|
118 | + } |
|
119 | + // Remove tables in double. |
|
120 | + $tables = array_flip(array_flip($tables)); |
|
121 | + return $tables; |
|
122 | + } |
|
123 | 123 | } |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | * |
60 | 60 | * @param array<FilterInterface> $filters |
61 | 61 | */ |
62 | - public function __construct(array $filters=array()) { |
|
62 | + public function __construct(array $filters = array()) { |
|
63 | 63 | $this->filters = $filters; |
64 | 64 | } |
65 | 65 | |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | } |
92 | 92 | |
93 | 93 | if (count($filters_sql)>0) { |
94 | - return '('.implode(' OR ',$filters_sql).')'; |
|
94 | + return '('.implode(' OR ', $filters_sql).')'; |
|
95 | 95 | } else { |
96 | 96 | return ''; |
97 | 97 | } |
@@ -114,7 +114,7 @@ discard block |
||
114 | 114 | throw new TDBMException("Error in OrFilter: One of the parameters is not a filter."); |
115 | 115 | } |
116 | 116 | |
117 | - $tables = array_merge($tables,$filter->getUsedTables()); |
|
117 | + $tables = array_merge($tables, $filter->getUsedTables()); |
|
118 | 118 | } |
119 | 119 | // Remove tables in double. |
120 | 120 | $tables = array_flip(array_flip($tables)); |
@@ -85,7 +85,7 @@ |
||
85 | 85 | * @param string $columnName |
86 | 86 | * @param string $value |
87 | 87 | */ |
88 | - public function __construct($tableName=null, $columnName=null, $value=null) { |
|
88 | + public function __construct($tableName = null, $columnName = null, $value = null) { |
|
89 | 89 | $this->tableName = $tableName; |
90 | 90 | $this->columnName = $columnName; |
91 | 91 | $this->value = $value; |
@@ -510,7 +510,7 @@ discard block |
||
510 | 510 | case TDBMObjectStateEnum::STATE_DETACHED: |
511 | 511 | throw new TDBMInvalidOperationException('Cannot delete a detached object'); |
512 | 512 | case TDBMObjectStateEnum::STATE_NEW: |
513 | - $this->deleteManyToManyRelationships($object); |
|
513 | + $this->deleteManyToManyRelationships($object); |
|
514 | 514 | foreach ($object->_getDbRows() as $dbRow) { |
515 | 515 | $this->removeFromToSaveObjectList($dbRow); |
516 | 516 | } |
@@ -521,7 +521,7 @@ discard block |
||
521 | 521 | } |
522 | 522 | case TDBMObjectStateEnum::STATE_NOT_LOADED: |
523 | 523 | case TDBMObjectStateEnum::STATE_LOADED: |
524 | - $this->deleteManyToManyRelationships($object); |
|
524 | + $this->deleteManyToManyRelationships($object); |
|
525 | 525 | // Let's delete db rows, in reverse order. |
526 | 526 | foreach (array_reverse($object->_getDbRows()) as $dbRow) { |
527 | 527 | $tableName = $dbRow->_getDbTableName(); |
@@ -541,63 +541,63 @@ discard block |
||
541 | 541 | $object->_setStatus(TDBMObjectStateEnum::STATE_DELETED); |
542 | 542 | } |
543 | 543 | |
544 | - /** |
|
545 | - * Removes all many to many relationships for this object. |
|
546 | - * @param AbstractTDBMObject $object |
|
547 | - */ |
|
548 | - private function deleteManyToManyRelationships(AbstractTDBMObject $object) { |
|
549 | - foreach ($object->_getDbRows() as $tableName => $dbRow) { |
|
550 | - $pivotTables = $this->tdbmSchemaAnalyzer->getPivotTableLinkedToTable($tableName); |
|
551 | - foreach ($pivotTables as $pivotTable) { |
|
552 | - $remoteBeans = $object->_getRelationships($pivotTable); |
|
553 | - foreach ($remoteBeans as $remoteBean) { |
|
554 | - $object->_removeRelationship($pivotTable, $remoteBean); |
|
555 | - } |
|
556 | - } |
|
557 | - } |
|
558 | - $this->persistManyToManyRelationships($object); |
|
559 | - } |
|
560 | - |
|
561 | - |
|
562 | - /** |
|
563 | - * This function removes the given object from the database. It will also remove all objects relied to the one given |
|
564 | - * by parameter before all. |
|
565 | - * |
|
566 | - * Notice: if the object has a multiple primary key, the function will not work. |
|
567 | - * |
|
568 | - * @param AbstractTDBMObject $objToDelete |
|
569 | - */ |
|
570 | - public function deleteCascade(AbstractTDBMObject $objToDelete) { |
|
571 | - $this->deleteAllConstraintWithThisObject($objToDelete); |
|
572 | - $this->delete($objToDelete); |
|
573 | - } |
|
574 | - |
|
575 | - /** |
|
576 | - * This function is used only in TDBMService (private function) |
|
577 | - * It will call deleteCascade function foreach object relied with a foreign key to the object given by parameter |
|
578 | - * |
|
579 | - * @param TDBMObject $obj |
|
580 | - * @return TDBMObjectArray |
|
581 | - */ |
|
582 | - private function deleteAllConstraintWithThisObject(TDBMObject $obj) { |
|
583 | - $tableFrom = $this->connection->escapeDBItem($obj->_getDbTableName()); |
|
584 | - $constraints = $this->connection->getConstraintsFromTable($tableFrom); |
|
585 | - foreach ($constraints as $constraint) { |
|
586 | - $tableTo = $this->connection->escapeDBItem($constraint["table1"]); |
|
587 | - $colFrom = $this->connection->escapeDBItem($constraint["col2"]); |
|
588 | - $colTo = $this->connection->escapeDBItem($constraint["col1"]); |
|
589 | - $idVarName = $this->connection->escapeDBItem($obj->getPrimaryKey()[0]); |
|
590 | - $idValue = $this->connection->quoteSmart($obj->TDBMObject_id); |
|
591 | - $sql = "SELECT DISTINCT ".$tableTo.".*" |
|
592 | - ." FROM ".$tableFrom |
|
593 | - ." LEFT JOIN ".$tableTo." ON ".$tableFrom.".".$colFrom." = ".$tableTo.".".$colTo |
|
594 | - ." WHERE ".$tableFrom.".".$idVarName."=".$idValue; |
|
595 | - $result = $this->getObjectsFromSQL($constraint["table1"], $sql); |
|
596 | - foreach ($result as $tdbmObj) { |
|
597 | - $this->deleteCascade($tdbmObj); |
|
598 | - } |
|
599 | - } |
|
600 | - } |
|
544 | + /** |
|
545 | + * Removes all many to many relationships for this object. |
|
546 | + * @param AbstractTDBMObject $object |
|
547 | + */ |
|
548 | + private function deleteManyToManyRelationships(AbstractTDBMObject $object) { |
|
549 | + foreach ($object->_getDbRows() as $tableName => $dbRow) { |
|
550 | + $pivotTables = $this->tdbmSchemaAnalyzer->getPivotTableLinkedToTable($tableName); |
|
551 | + foreach ($pivotTables as $pivotTable) { |
|
552 | + $remoteBeans = $object->_getRelationships($pivotTable); |
|
553 | + foreach ($remoteBeans as $remoteBean) { |
|
554 | + $object->_removeRelationship($pivotTable, $remoteBean); |
|
555 | + } |
|
556 | + } |
|
557 | + } |
|
558 | + $this->persistManyToManyRelationships($object); |
|
559 | + } |
|
560 | + |
|
561 | + |
|
562 | + /** |
|
563 | + * This function removes the given object from the database. It will also remove all objects relied to the one given |
|
564 | + * by parameter before all. |
|
565 | + * |
|
566 | + * Notice: if the object has a multiple primary key, the function will not work. |
|
567 | + * |
|
568 | + * @param AbstractTDBMObject $objToDelete |
|
569 | + */ |
|
570 | + public function deleteCascade(AbstractTDBMObject $objToDelete) { |
|
571 | + $this->deleteAllConstraintWithThisObject($objToDelete); |
|
572 | + $this->delete($objToDelete); |
|
573 | + } |
|
574 | + |
|
575 | + /** |
|
576 | + * This function is used only in TDBMService (private function) |
|
577 | + * It will call deleteCascade function foreach object relied with a foreign key to the object given by parameter |
|
578 | + * |
|
579 | + * @param TDBMObject $obj |
|
580 | + * @return TDBMObjectArray |
|
581 | + */ |
|
582 | + private function deleteAllConstraintWithThisObject(TDBMObject $obj) { |
|
583 | + $tableFrom = $this->connection->escapeDBItem($obj->_getDbTableName()); |
|
584 | + $constraints = $this->connection->getConstraintsFromTable($tableFrom); |
|
585 | + foreach ($constraints as $constraint) { |
|
586 | + $tableTo = $this->connection->escapeDBItem($constraint["table1"]); |
|
587 | + $colFrom = $this->connection->escapeDBItem($constraint["col2"]); |
|
588 | + $colTo = $this->connection->escapeDBItem($constraint["col1"]); |
|
589 | + $idVarName = $this->connection->escapeDBItem($obj->getPrimaryKey()[0]); |
|
590 | + $idValue = $this->connection->quoteSmart($obj->TDBMObject_id); |
|
591 | + $sql = "SELECT DISTINCT ".$tableTo.".*" |
|
592 | + ." FROM ".$tableFrom |
|
593 | + ." LEFT JOIN ".$tableTo." ON ".$tableFrom.".".$colFrom." = ".$tableTo.".".$colTo |
|
594 | + ." WHERE ".$tableFrom.".".$idVarName."=".$idValue; |
|
595 | + $result = $this->getObjectsFromSQL($constraint["table1"], $sql); |
|
596 | + foreach ($result as $tdbmObj) { |
|
597 | + $this->deleteCascade($tdbmObj); |
|
598 | + } |
|
599 | + } |
|
600 | + } |
|
601 | 601 | |
602 | 602 | /** |
603 | 603 | * This function performs a save() of all the objects that have been modified. |
@@ -997,8 +997,8 @@ discard block |
||
997 | 997 | } |
998 | 998 | |
999 | 999 | /** |
1000 | - * @param array<string, string> $tableToBeanMap |
|
1001 | - */ |
|
1000 | + * @param array<string, string> $tableToBeanMap |
|
1001 | + */ |
|
1002 | 1002 | public function setTableToBeanMap(array $tableToBeanMap) { |
1003 | 1003 | $this->tableToBeanMap = $tableToBeanMap; |
1004 | 1004 | } |
@@ -1045,7 +1045,7 @@ discard block |
||
1045 | 1045 | |
1046 | 1046 | // Let's save all references in NEW or DETACHED state (we need their primary key) |
1047 | 1047 | foreach ($references as $fkName => $reference) { |
1048 | - $refStatus = $reference->_getStatus(); |
|
1048 | + $refStatus = $reference->_getStatus(); |
|
1049 | 1049 | if ($refStatus === TDBMObjectStateEnum::STATE_NEW || $refStatus === TDBMObjectStateEnum::STATE_DETACHED) { |
1050 | 1050 | $this->save($reference); |
1051 | 1051 | } |
@@ -1204,94 +1204,94 @@ discard block |
||
1204 | 1204 | throw new TDBMInvalidOperationException("This object has been deleted. It cannot be saved."); |
1205 | 1205 | } |
1206 | 1206 | |
1207 | - // Finally, let's save all the many to many relationships to this bean. |
|
1208 | - $this->persistManyToManyRelationships($object); |
|
1207 | + // Finally, let's save all the many to many relationships to this bean. |
|
1208 | + $this->persistManyToManyRelationships($object); |
|
1209 | 1209 | } |
1210 | 1210 | |
1211 | - private function persistManyToManyRelationships(AbstractTDBMObject $object) { |
|
1212 | - foreach ($object->_getCachedRelationships() as $pivotTableName => $storage) { |
|
1213 | - $tableDescriptor = $this->tdbmSchemaAnalyzer->getSchema()->getTable($pivotTableName); |
|
1214 | - list($localFk, $remoteFk) = $this->getPivotTableForeignKeys($pivotTableName, $object); |
|
1215 | - |
|
1216 | - foreach ($storage as $remoteBean) { |
|
1217 | - /* @var $remoteBean AbstractTDBMObject */ |
|
1218 | - $statusArr = $storage[$remoteBean]; |
|
1219 | - $status = $statusArr['status']; |
|
1220 | - $reverse = $statusArr['reverse']; |
|
1221 | - if ($reverse) { |
|
1222 | - continue; |
|
1223 | - } |
|
1224 | - |
|
1225 | - if ($status === 'new') { |
|
1226 | - $remoteBeanStatus = $remoteBean->_getStatus(); |
|
1227 | - if ($remoteBeanStatus === TDBMObjectStateEnum::STATE_NEW || $remoteBeanStatus === TDBMObjectStateEnum::STATE_DETACHED) { |
|
1228 | - // Let's save remote bean if needed. |
|
1229 | - $this->save($remoteBean); |
|
1230 | - } |
|
1211 | + private function persistManyToManyRelationships(AbstractTDBMObject $object) { |
|
1212 | + foreach ($object->_getCachedRelationships() as $pivotTableName => $storage) { |
|
1213 | + $tableDescriptor = $this->tdbmSchemaAnalyzer->getSchema()->getTable($pivotTableName); |
|
1214 | + list($localFk, $remoteFk) = $this->getPivotTableForeignKeys($pivotTableName, $object); |
|
1215 | + |
|
1216 | + foreach ($storage as $remoteBean) { |
|
1217 | + /* @var $remoteBean AbstractTDBMObject */ |
|
1218 | + $statusArr = $storage[$remoteBean]; |
|
1219 | + $status = $statusArr['status']; |
|
1220 | + $reverse = $statusArr['reverse']; |
|
1221 | + if ($reverse) { |
|
1222 | + continue; |
|
1223 | + } |
|
1231 | 1224 | |
1232 | - $filters = $this->getPivotFilters($object, $remoteBean, $localFk, $remoteFk); |
|
1225 | + if ($status === 'new') { |
|
1226 | + $remoteBeanStatus = $remoteBean->_getStatus(); |
|
1227 | + if ($remoteBeanStatus === TDBMObjectStateEnum::STATE_NEW || $remoteBeanStatus === TDBMObjectStateEnum::STATE_DETACHED) { |
|
1228 | + // Let's save remote bean if needed. |
|
1229 | + $this->save($remoteBean); |
|
1230 | + } |
|
1233 | 1231 | |
1234 | - $types = []; |
|
1232 | + $filters = $this->getPivotFilters($object, $remoteBean, $localFk, $remoteFk); |
|
1235 | 1233 | |
1236 | - foreach ($filters as $columnName => $value) { |
|
1237 | - $columnDescriptor = $tableDescriptor->getColumn($columnName); |
|
1238 | - $types[] = $columnDescriptor->getType(); |
|
1239 | - } |
|
1234 | + $types = []; |
|
1235 | + |
|
1236 | + foreach ($filters as $columnName => $value) { |
|
1237 | + $columnDescriptor = $tableDescriptor->getColumn($columnName); |
|
1238 | + $types[] = $columnDescriptor->getType(); |
|
1239 | + } |
|
1240 | 1240 | |
1241 | - $this->connection->insert($pivotTableName, $filters, $types); |
|
1241 | + $this->connection->insert($pivotTableName, $filters, $types); |
|
1242 | 1242 | |
1243 | - // Finally, let's mark relationships as saved. |
|
1244 | - $statusArr['status'] = 'loaded'; |
|
1245 | - $storage[$remoteBean] = $statusArr; |
|
1246 | - $remoteStorage = $remoteBean->_getCachedRelationships()[$pivotTableName]; |
|
1247 | - $remoteStatusArr = $remoteStorage[$object]; |
|
1248 | - $remoteStatusArr['status'] = 'loaded'; |
|
1249 | - $remoteStorage[$object] = $remoteStatusArr; |
|
1243 | + // Finally, let's mark relationships as saved. |
|
1244 | + $statusArr['status'] = 'loaded'; |
|
1245 | + $storage[$remoteBean] = $statusArr; |
|
1246 | + $remoteStorage = $remoteBean->_getCachedRelationships()[$pivotTableName]; |
|
1247 | + $remoteStatusArr = $remoteStorage[$object]; |
|
1248 | + $remoteStatusArr['status'] = 'loaded'; |
|
1249 | + $remoteStorage[$object] = $remoteStatusArr; |
|
1250 | 1250 | |
1251 | - } elseif ($status === 'delete') { |
|
1252 | - $filters = $this->getPivotFilters($object, $remoteBean, $localFk, $remoteFk); |
|
1251 | + } elseif ($status === 'delete') { |
|
1252 | + $filters = $this->getPivotFilters($object, $remoteBean, $localFk, $remoteFk); |
|
1253 | 1253 | |
1254 | - $types = []; |
|
1254 | + $types = []; |
|
1255 | 1255 | |
1256 | - foreach ($filters as $columnName => $value) { |
|
1257 | - $columnDescriptor = $tableDescriptor->getColumn($columnName); |
|
1258 | - $types[] = $columnDescriptor->getType(); |
|
1259 | - } |
|
1256 | + foreach ($filters as $columnName => $value) { |
|
1257 | + $columnDescriptor = $tableDescriptor->getColumn($columnName); |
|
1258 | + $types[] = $columnDescriptor->getType(); |
|
1259 | + } |
|
1260 | + |
|
1261 | + $this->connection->delete($pivotTableName, $filters, $types); |
|
1262 | + |
|
1263 | + // Finally, let's remove relationships completely from bean. |
|
1264 | + $storage->detach($remoteBean); |
|
1265 | + $remoteBean->_getCachedRelationships()[$pivotTableName]->detach($object); |
|
1266 | + } |
|
1267 | + } |
|
1268 | + } |
|
1269 | + } |
|
1260 | 1270 | |
1261 | - $this->connection->delete($pivotTableName, $filters, $types); |
|
1262 | - |
|
1263 | - // Finally, let's remove relationships completely from bean. |
|
1264 | - $storage->detach($remoteBean); |
|
1265 | - $remoteBean->_getCachedRelationships()[$pivotTableName]->detach($object); |
|
1266 | - } |
|
1267 | - } |
|
1268 | - } |
|
1269 | - } |
|
1270 | - |
|
1271 | - private function getPivotFilters(AbstractTDBMObject $localBean, AbstractTDBMObject $remoteBean, ForeignKeyConstraint $localFk, ForeignKeyConstraint $remoteFk) { |
|
1272 | - $localBeanPk = $this->getPrimaryKeyValues($localBean); |
|
1273 | - $remoteBeanPk = $this->getPrimaryKeyValues($remoteBean); |
|
1274 | - $localColumns = $localFk->getLocalColumns(); |
|
1275 | - $remoteColumns = $remoteFk->getLocalColumns(); |
|
1276 | - |
|
1277 | - $localFilters = array_combine($localColumns, $localBeanPk); |
|
1278 | - $remoteFilters = array_combine($remoteColumns, $remoteBeanPk); |
|
1279 | - |
|
1280 | - return array_merge($localFilters, $remoteFilters); |
|
1281 | - } |
|
1282 | - |
|
1283 | - /** |
|
1284 | - * Returns the "values" of the primary key. |
|
1285 | - * This returns the primary key from the $primaryKey attribute, not the one stored in the columns. |
|
1286 | - * |
|
1287 | - * @param AbstractTDBMObject $bean |
|
1288 | - * @return array numerically indexed array of values. |
|
1289 | - */ |
|
1290 | - private function getPrimaryKeyValues(AbstractTDBMObject $bean) { |
|
1291 | - $dbRows = $bean->_getDbRows(); |
|
1292 | - $dbRow = reset($dbRows); |
|
1293 | - return array_values($dbRow->_getPrimaryKeys()); |
|
1294 | - } |
|
1271 | + private function getPivotFilters(AbstractTDBMObject $localBean, AbstractTDBMObject $remoteBean, ForeignKeyConstraint $localFk, ForeignKeyConstraint $remoteFk) { |
|
1272 | + $localBeanPk = $this->getPrimaryKeyValues($localBean); |
|
1273 | + $remoteBeanPk = $this->getPrimaryKeyValues($remoteBean); |
|
1274 | + $localColumns = $localFk->getLocalColumns(); |
|
1275 | + $remoteColumns = $remoteFk->getLocalColumns(); |
|
1276 | + |
|
1277 | + $localFilters = array_combine($localColumns, $localBeanPk); |
|
1278 | + $remoteFilters = array_combine($remoteColumns, $remoteBeanPk); |
|
1279 | + |
|
1280 | + return array_merge($localFilters, $remoteFilters); |
|
1281 | + } |
|
1282 | + |
|
1283 | + /** |
|
1284 | + * Returns the "values" of the primary key. |
|
1285 | + * This returns the primary key from the $primaryKey attribute, not the one stored in the columns. |
|
1286 | + * |
|
1287 | + * @param AbstractTDBMObject $bean |
|
1288 | + * @return array numerically indexed array of values. |
|
1289 | + */ |
|
1290 | + private function getPrimaryKeyValues(AbstractTDBMObject $bean) { |
|
1291 | + $dbRows = $bean->_getDbRows(); |
|
1292 | + $dbRow = reset($dbRows); |
|
1293 | + return array_values($dbRow->_getPrimaryKeys()); |
|
1294 | + } |
|
1295 | 1295 | |
1296 | 1296 | /** |
1297 | 1297 | * Returns a unique hash used to store the object based on its primary key. |
@@ -1774,39 +1774,39 @@ discard block |
||
1774 | 1774 | */ |
1775 | 1775 | public function _getRelatedBeans($pivotTableName, AbstractTDBMObject $bean) { |
1776 | 1776 | |
1777 | - list($localFk, $remoteFk) = $this->getPivotTableForeignKeys($pivotTableName, $bean); |
|
1778 | - /* @var $localFk ForeignKeyConstraint */ |
|
1779 | - /* @var $remoteFk ForeignKeyConstraint */ |
|
1780 | - $remoteTable = $remoteFk->getForeignTableName(); |
|
1777 | + list($localFk, $remoteFk) = $this->getPivotTableForeignKeys($pivotTableName, $bean); |
|
1778 | + /* @var $localFk ForeignKeyConstraint */ |
|
1779 | + /* @var $remoteFk ForeignKeyConstraint */ |
|
1780 | + $remoteTable = $remoteFk->getForeignTableName(); |
|
1781 | 1781 | |
1782 | 1782 | |
1783 | - $primaryKeys = $this->getPrimaryKeyValues($bean); |
|
1784 | - $columnNames = array_map(function($name) use ($pivotTableName) { return $pivotTableName.'.'.$name; }, $localFk->getLocalColumns()); |
|
1783 | + $primaryKeys = $this->getPrimaryKeyValues($bean); |
|
1784 | + $columnNames = array_map(function($name) use ($pivotTableName) { return $pivotTableName.'.'.$name; }, $localFk->getLocalColumns()); |
|
1785 | 1785 | |
1786 | - $filter = array_combine($columnNames, $primaryKeys); |
|
1786 | + $filter = array_combine($columnNames, $primaryKeys); |
|
1787 | 1787 | |
1788 | - return $this->findObjects($remoteTable, $filter); |
|
1788 | + return $this->findObjects($remoteTable, $filter); |
|
1789 | 1789 | } |
1790 | 1790 | |
1791 | - /** |
|
1792 | - * @param $pivotTableName |
|
1793 | - * @param AbstractTDBMObject $bean The LOCAL bean |
|
1794 | - * @return ForeignKeyConstraint[] First item: the LOCAL bean, second item: the REMOTE bean. |
|
1795 | - * @throws TDBMException |
|
1796 | - */ |
|
1797 | - private function getPivotTableForeignKeys($pivotTableName, AbstractTDBMObject $bean) { |
|
1798 | - $fks = array_values($this->tdbmSchemaAnalyzer->getSchema()->getTable($pivotTableName)->getForeignKeys()); |
|
1799 | - $table1 = $fks[0]->getForeignTableName(); |
|
1800 | - $table2 = $fks[1]->getForeignTableName(); |
|
1801 | - |
|
1802 | - $beanTables = array_map(function(DbRow $dbRow) { return $dbRow->_getDbTableName(); }, $bean->_getDbRows()); |
|
1803 | - |
|
1804 | - if (in_array($table1, $beanTables)) { |
|
1805 | - return [$fks[0], $fks[1]]; |
|
1806 | - } elseif (in_array($table2, $beanTables)) { |
|
1807 | - return [$fks[1], $fks[0]]; |
|
1808 | - } else { |
|
1809 | - throw new TDBMException("Unexpected bean type in getPivotTableForeignKeys. Awaiting beans from table {$table1} and {$table2}"); |
|
1810 | - } |
|
1811 | - } |
|
1791 | + /** |
|
1792 | + * @param $pivotTableName |
|
1793 | + * @param AbstractTDBMObject $bean The LOCAL bean |
|
1794 | + * @return ForeignKeyConstraint[] First item: the LOCAL bean, second item: the REMOTE bean. |
|
1795 | + * @throws TDBMException |
|
1796 | + */ |
|
1797 | + private function getPivotTableForeignKeys($pivotTableName, AbstractTDBMObject $bean) { |
|
1798 | + $fks = array_values($this->tdbmSchemaAnalyzer->getSchema()->getTable($pivotTableName)->getForeignKeys()); |
|
1799 | + $table1 = $fks[0]->getForeignTableName(); |
|
1800 | + $table2 = $fks[1]->getForeignTableName(); |
|
1801 | + |
|
1802 | + $beanTables = array_map(function(DbRow $dbRow) { return $dbRow->_getDbTableName(); }, $bean->_getDbRows()); |
|
1803 | + |
|
1804 | + if (in_array($table1, $beanTables)) { |
|
1805 | + return [$fks[0], $fks[1]]; |
|
1806 | + } elseif (in_array($table2, $beanTables)) { |
|
1807 | + return [$fks[1], $fks[0]]; |
|
1808 | + } else { |
|
1809 | + throw new TDBMException("Unexpected bean type in getPivotTableForeignKeys. Awaiting beans from table {$table1} and {$table2}"); |
|
1810 | + } |
|
1811 | + } |
|
1812 | 1812 | } |
@@ -985,7 +985,7 @@ discard block |
||
985 | 985 | * This is used internally by TDBM to add an object to the list of objects that have been |
986 | 986 | * created/updated but not saved yet. |
987 | 987 | * |
988 | - * @param AbstractTDBMObject $myObject |
|
988 | + * @param DbRow $myObject |
|
989 | 989 | */ |
990 | 990 | public function _addToToSaveObjectList(DbRow $myObject) { |
991 | 991 | $this->toSaveObjects[] = $myObject; |
@@ -1621,7 +1621,7 @@ discard block |
||
1621 | 1621 | } |
1622 | 1622 | |
1623 | 1623 | /** |
1624 | - * @param $table |
|
1624 | + * @param string $table |
|
1625 | 1625 | * @param array $primaryKeys |
1626 | 1626 | * @param array $additionalTablesFetch |
1627 | 1627 | * @param bool $lazy Whether to perform lazy loading on this object or not. |
@@ -1778,7 +1778,7 @@ discard block |
||
1778 | 1778 | /** |
1779 | 1779 | * @param $pivotTableName |
1780 | 1780 | * @param AbstractTDBMObject $bean |
1781 | - * @return AbstractTDBMObject[] |
|
1781 | + * @return ResultIterator |
|
1782 | 1782 | */ |
1783 | 1783 | public function _getRelatedBeans($pivotTableName, AbstractTDBMObject $bean) { |
1784 | 1784 |
@@ -22,16 +22,11 @@ |
||
22 | 22 | use Doctrine\Common\Cache\Cache; |
23 | 23 | use Doctrine\Common\Cache\VoidCache; |
24 | 24 | use Doctrine\DBAL\Connection; |
25 | -use Doctrine\DBAL\DBALException; |
|
26 | -use Doctrine\DBAL\Schema\Column; |
|
27 | 25 | use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
28 | -use Doctrine\DBAL\Schema\Schema; |
|
29 | 26 | use Mouf\Database\MagicQuery; |
30 | 27 | use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer; |
31 | 28 | use Mouf\Database\TDBM\Filters\OrderBySQLString; |
32 | 29 | use Mouf\Database\TDBM\Utils\TDBMDaoGenerator; |
33 | -use Mouf\Utils\Cache\CacheInterface; |
|
34 | -use SQLParser\Node\ColRef; |
|
35 | 30 | |
36 | 31 | /** |
37 | 32 | * The TDBMService class is the main TDBM class. It provides methods to retrieve TDBMObject instances |
@@ -699,9 +699,9 @@ discard block |
||
699 | 699 | // 4-2, let's take all the objects out of the orderby bag, and let's make objects from them |
700 | 700 | $orderby_bag2 = array(); |
701 | 701 | foreach ($orderby_bag as $thing) { |
702 | - if (is_a($thing,'Mouf\\Database\\TDBM\\Filters\\OrderBySQLString')) { |
|
702 | + if (is_a($thing, 'Mouf\\Database\\TDBM\\Filters\\OrderBySQLString')) { |
|
703 | 703 | $orderby_bag2[] = $thing; |
704 | - } elseif (is_a($thing,'Mouf\\Database\\TDBM\\Filters\\OrderByColumn')) { |
|
704 | + } elseif (is_a($thing, 'Mouf\\Database\\TDBM\\Filters\\OrderByColumn')) { |
|
705 | 705 | $orderby_bag2[] = $thing; |
706 | 706 | } elseif (is_string($thing)) { |
707 | 707 | $orderby_bag2[] = new OrderBySQLString($thing); |
@@ -1204,7 +1204,7 @@ discard block |
||
1204 | 1204 | $schemaAnalyzer = $this->schemaAnalyzer; |
1205 | 1205 | |
1206 | 1206 | foreach ($tables as $currentTable) { |
1207 | - $allParents = [ $currentTable ]; |
|
1207 | + $allParents = [$currentTable]; |
|
1208 | 1208 | $currentFk = null; |
1209 | 1209 | while ($currentFk = $schemaAnalyzer->getParentRelationship($currentTable)) { |
1210 | 1210 | $currentTable = $currentFk->getForeignTableName(); |
@@ -1246,7 +1246,7 @@ discard block |
||
1246 | 1246 | // Let's scan the parent tables |
1247 | 1247 | $currentTable = $table; |
1248 | 1248 | |
1249 | - $parentTables = [ ]; |
|
1249 | + $parentTables = []; |
|
1250 | 1250 | |
1251 | 1251 | // Get parent relationship |
1252 | 1252 | while ($currentFk = $schemaAnalyzer->getParentRelationship($currentTable)) { |
@@ -1336,7 +1336,7 @@ discard block |
||
1336 | 1336 | * @return ResultIterator An object representing an array of results. |
1337 | 1337 | * @throws TDBMException |
1338 | 1338 | */ |
1339 | - public function findObjects($mainTable, $filter=null, array $parameters = array(), $orderString=null, array $additionalTablesFetch = array(), $mode = null, $className=null) { |
|
1339 | + public function findObjects($mainTable, $filter = null, array $parameters = array(), $orderString = null, array $additionalTablesFetch = array(), $mode = null, $className = null) { |
|
1340 | 1340 | // $mainTable is not secured in MagicJoin, let's add a bit of security to avoid SQL injection. |
1341 | 1341 | if (!preg_match('/^[a-zA-Z_][a-zA-Z0-9_]*$/', $mainTable)) { |
1342 | 1342 | throw new TDBMException(sprintf("Invalid table name: '%s'", $mainTable)); |
@@ -1405,7 +1405,7 @@ discard block |
||
1405 | 1405 | throw new TDBMException("Unknown fetch mode: '".$this->mode."'"); |
1406 | 1406 | } |
1407 | 1407 | |
1408 | - $mode = $mode?:$this->mode; |
|
1408 | + $mode = $mode ?: $this->mode; |
|
1409 | 1409 | |
1410 | 1410 | return new ResultIterator($sql, $countSql, $parameters, $columnDescList, $this->objectStorage, $className, $this, $this->magicQuery, $mode); |
1411 | 1411 | } |
@@ -1419,7 +1419,7 @@ discard block |
||
1419 | 1419 | * @return AbstractTDBMObject |
1420 | 1420 | * @throws TDBMException |
1421 | 1421 | */ |
1422 | - public function findObjectByPk($table, array $primaryKeys, array $additionalTablesFetch = array(), $lazy = false, $className=null) { |
|
1422 | + public function findObjectByPk($table, array $primaryKeys, array $additionalTablesFetch = array(), $lazy = false, $className = null) { |
|
1423 | 1423 | $primaryKeys = $this->_getPrimaryKeysFromObjectData($table, $primaryKeys); |
1424 | 1424 | $hash = $this->getObjectHash($primaryKeys); |
1425 | 1425 | |
@@ -1439,7 +1439,7 @@ discard block |
||
1439 | 1439 | // Only allowed if no inheritance. |
1440 | 1440 | if (count($tables) === 1) { |
1441 | 1441 | if ($className === null) { |
1442 | - $className = isset($this->tableToBeanMap[$table])?$this->tableToBeanMap[$table]:"Mouf\\Database\\TDBM\\TDBMObject"; |
|
1442 | + $className = isset($this->tableToBeanMap[$table]) ? $this->tableToBeanMap[$table] : "Mouf\\Database\\TDBM\\TDBMObject"; |
|
1443 | 1443 | } |
1444 | 1444 | |
1445 | 1445 | // Let's construct the bean |
@@ -1468,11 +1468,11 @@ discard block |
||
1468 | 1468 | * @return AbstractTDBMObject|null The object we want, or null if no object matches the filters. |
1469 | 1469 | * @throws TDBMException |
1470 | 1470 | */ |
1471 | - public function findObject($mainTable, $filterString=null, array $parameters = array(), array $additionalTablesFetch = array(), $className = null) { |
|
1471 | + public function findObject($mainTable, $filterString = null, array $parameters = array(), array $additionalTablesFetch = array(), $className = null) { |
|
1472 | 1472 | $objects = $this->findObjects($mainTable, $filterString, $parameters, null, $additionalTablesFetch, self::MODE_ARRAY, $className); |
1473 | 1473 | $page = $objects->take(0, 2); |
1474 | 1474 | $count = $page->count(); |
1475 | - if ($count > 1) { |
|
1475 | + if ($count>1) { |
|
1476 | 1476 | throw new DuplicateRowException("Error while querying an object for table '$mainTable': More than 1 row have been returned, but we should have received at most one."); |
1477 | 1477 | } elseif ($count === 0) { |
1478 | 1478 | return null; |
@@ -1492,7 +1492,7 @@ discard block |
||
1492 | 1492 | * @return AbstractTDBMObject The object we want |
1493 | 1493 | * @throws TDBMException |
1494 | 1494 | */ |
1495 | - public function findObjectOrFail($mainTable, $filterString=null, array $parameters = array(), array $additionalTablesFetch = array(), $className = null) { |
|
1495 | + public function findObjectOrFail($mainTable, $filterString = null, array $parameters = array(), array $additionalTablesFetch = array(), $className = null) { |
|
1496 | 1496 | $bean = $this->findObject($mainTable, $filterString, $parameters, $additionalTablesFetch, $className); |
1497 | 1497 | if ($bean === null) { |
1498 | 1498 | throw new NoBeanFoundException("No result found for query on table '".$mainTable."'"); |
@@ -693,8 +693,9 @@ |
||
693 | 693 | // Fourth, let's apply the same steps to the orderby_bag |
694 | 694 | // 4-1 orderby_bag should be an array, if it is a singleton, let's put it in an array. |
695 | 695 | |
696 | - if (!is_array($orderby_bag)) |
|
697 | - $orderby_bag = array($orderby_bag); |
|
696 | + if (!is_array($orderby_bag)) { |
|
697 | + $orderby_bag = array($orderby_bag); |
|
698 | + } |
|
698 | 699 | |
699 | 700 | // 4-2, let's take all the objects out of the orderby bag, and let's make objects from them |
700 | 701 | $orderby_bag2 = array(); |