@@ -497,36 +497,7 @@ public function nullable()
497
497
}
498
498
499
499
500
- /**
501
- * Generate a CREATE TABLE query for the current table and fields.
502
- *
503
- * @return string
504
- */
505
- public function create ($ engine = 'InnoDB ' , $ charset = 'utf8mb4 ' , $ collate = 'utf8mb4_unicode_ci ' )
506
- {
507
- $ this ->addToFieldsAndResetFieldQuery ();
508
-
509
- $ sql = "CREATE TABLE IF NOT EXISTS ` {$ this ->table }` ( " ;
510
- $ sql .= $ this ->generateFieldQueryString ();
511
- $ sql .= ") ENGINE= {$ engine } DEFAULT CHARSET= {$ charset } COLLATE= {$ collate }; " ;
512
-
513
- // die("$sql\n");
514
- return DB ::query ($ sql );
515
- }
516
500
517
- /**
518
- * Generate an ALTER TABLE query for the current table and fields.
519
- *
520
- * @return string
521
- */
522
- public function change ()
523
- {
524
- $ this ->addToFieldsAndResetFieldQuery ();
525
- $ sql = "ALTER TABLE ` {$ this ->table }` " ;
526
- $ sql .= $ this ->generateFieldQueryString ();
527
-
528
- return DB ::query ($ sql );
529
- }
530
501
531
502
532
503
private function generateFieldQueryString (): string
@@ -600,6 +571,7 @@ public function dropColumn($name)
600
571
*/
601
572
public function renameColumn ($ current_name )
602
573
{
574
+ // $this->setFieldQuery('Action', "CHANGE IF EXISTS `$current_name`");
603
575
$ this ->change_action = "CHANGE IF EXISTS ` $ current_name` " ;
604
576
return $ this ;
605
577
}
@@ -612,6 +584,51 @@ public function after($column_name)
612
584
}
613
585
614
586
587
+ /**
588
+ * Drop an index from the table.
589
+ *
590
+ * @param string $name
591
+ * @return $this
592
+ */
593
+ public function dropIndex ($ name )
594
+ {
595
+ $ this ->setFieldQuery ('Action ' , 'DROP INDEX ' );
596
+ $ this ->setFieldQuery ('Field ' , "` $ name` " );
597
+ // $this->fields[] = " ";
598
+
599
+ return $ this ;
600
+ }
601
+
602
+ /**
603
+ * Add a foreign key constraint to the table.
604
+ *
605
+ * @param string $name
606
+ * @param string $column
607
+ * @param string $table
608
+ * @param string $references
609
+ * @param string $onDelete
610
+ * @param string $onUpdate
611
+ * @return $this
612
+ */
613
+ public function addForeign ($ name , $ column , $ table , $ references , $ onDelete = 'CASCADE ' , $ onUpdate = 'CASCADE ' )
614
+ {
615
+ $ this ->fields [] = "CONSTRAINT ` $ name` FOREIGN KEY (` $ column`) REFERENCES ` $ table` (` $ references`) ON DELETE $ onDelete ON UPDATE $ onUpdate " ;
616
+ return $ this ;
617
+ }
618
+
619
+ /**
620
+ * Drop a foreign key constraint from the table.
621
+ *
622
+ * @param string $name
623
+ * @return $this
624
+ */
625
+ public function dropForeign ($ name )
626
+ {
627
+ $ this ->fields [] = "DROP FOREIGN KEY ` $ name` " ;
628
+ return $ this ;
629
+ }
630
+
631
+
615
632
/**
616
633
* Set the character set and collation of the last added column in the $fields array to utf8mb4.
617
634
*
@@ -660,53 +677,48 @@ public function modifyColumn()
660
677
*/
661
678
public function addIndex ($ name , array $ columns , $ type = 'INDEX ' )
662
679
{
663
- // $this->setFieldQuery('Action', 'ADD');
680
+
664
681
$ this ->setFieldQuery ('Action ' , "ADD $ type " );
665
682
$ this ->setFieldQuery ('Field ' , "` $ name` " );
666
683
$ this ->setFieldQuery ('Extra ' , "( " . implode (', ' , $ columns ) . ") " );
684
+ $ this ->setFieldQuery ('Null ' , '' );
667
685
668
- // $this->fields[] = "ADD $type `$name` (" . implode(', ', $columns) . ")";
669
686
return $ this ;
670
687
}
671
688
672
- /**
673
- * Drop an index from the table.
674
- *
675
- * @param string $name
676
- * @return $this
677
- */
678
- public function dropIndex ($ name )
679
- {
680
- $ this ->fields [] = "DROP INDEX ` $ name` " ;
681
- return $ this ;
682
- }
689
+
690
+
691
+
683
692
684
693
/**
685
- * Add a foreign key constraint to the table.
694
+ * Generate a CREATE TABLE query for the current table and fields .
686
695
*
687
- * @param string $name
688
- * @param string $column
689
- * @param string $table
690
- * @param string $references
691
- * @param string $onDelete
692
- * @param string $onUpdate
693
- * @return $this
696
+ * @return string
694
697
*/
695
- public function addForeign ( $ name , $ column , $ table , $ references , $ onDelete = 'CASCADE ' , $ onUpdate = 'CASCADE ' )
698
+ public function create ( $ engine = ' InnoDB ' , $ charset = 'utf8mb4 ' , $ collate = 'utf8mb4_unicode_ci ' )
696
699
{
697
- $ this ->fields [] = "CONSTRAINT ` $ name` FOREIGN KEY (` $ column`) REFERENCES ` $ table` (` $ references`) ON DELETE $ onDelete ON UPDATE $ onUpdate " ;
698
- return $ this ;
700
+ $ this ->addToFieldsAndResetFieldQuery ();
701
+
702
+ $ sql = "CREATE TABLE IF NOT EXISTS ` {$ this ->table }` ( " ;
703
+ $ sql .= $ this ->generateFieldQueryString ();
704
+ $ sql .= ") ENGINE= {$ engine } DEFAULT CHARSET= {$ charset } COLLATE= {$ collate }; " ;
705
+
706
+ $ this ->reset ();
707
+ return DB ::query ($ sql );
699
708
}
700
709
701
710
/**
702
- * Drop a foreign key constraint from the table.
711
+ * Generate an ALTER TABLE query for the current table and fields .
703
712
*
704
- * @param string $name
705
- * @return $this
713
+ * @return string
706
714
*/
707
- public function dropForeign ( $ name )
715
+ public function change ( )
708
716
{
709
- $ this ->fields [] = "DROP FOREIGN KEY ` $ name` " ;
710
- return $ this ;
717
+ $ this ->addToFieldsAndResetFieldQuery ();
718
+ $ sql = "ALTER TABLE ` {$ this ->table }` " ;
719
+ $ sql .= $ this ->generateFieldQueryString ();
720
+
721
+ $ this ->reset ();
722
+ return DB ::query ($ sql );
711
723
}
712
724
}
0 commit comments