1- <?php namespace Kalnoy \ Nestedset ;
1+ <?php
22
3- use \Illuminate \Database \Connection ;
4- use \Illuminate \Database \Schema \Blueprint ;
3+ namespace Kalnoy \Nestedset ;
4+
5+ use Illuminate \Database \Connection ;
6+ use Illuminate \Database \Schema \Blueprint ;
57
68class NestedSet {
79
810 /**
9- * Add NestedSet columns to the table. Also create index and foreign key.
10- *
11- * @param Blueprint $table
11+ * Add default nested set columns to the table. Also create an index.
1212 *
13- * @return void
13+ * @param \Illuminate\Database\Schema\Blueprint $table
14+ * @param string $primaryKey
1415 */
15- static public function columns (Blueprint $ table , $ primaryKey = 'id ' )
16+ public static function columns (Blueprint $ table , $ primaryKey = 'id ' )
1617 {
1718 $ table ->integer (Node::LFT );
1819 $ table ->integer (Node::RGT );
1920 $ table ->unsignedInteger (Node::PARENT_ID )->nullable ();
2021
21- $ table ->index (array (Node::LFT , Node::RGT , Node::PARENT_ID ), 'nested_set_index ' );
22-
23- $ table
24- ->foreign (Node::PARENT_ID , self ::getForeignKeyName ($ table ->getTable ()))
25- ->references ($ primaryKey )
26- ->on ($ table ->getTable ())
27- ->onDelete ('cascade ' );
28- }
29-
30- /**
31- * Get foreign key name for the table.
32- *
33- * @param string $table
34- *
35- * @return string
36- */
37- protected static function getForeignKeyName ($ table )
38- {
39- return $ table .'_nested_set_foreign ' ;
22+ $ table ->index (self ::getDefaultColumns ());
4023 }
4124
4225 /**
4326 * Drop NestedSet columns.
4427 *
45- * @param Blueprint $table
46- *
47- * @return void
28+ * @param \Illuminate\Database\Schema\Blueprint $table
4829 */
49- static public function dropColumns (Blueprint $ table )
30+ public static function dropColumns (Blueprint $ table )
5031 {
51- $ table ->dropForeign ('nested_set_foreign ' );
52- $ table ->dropIndex ('nested_set_index ' );
53- $ table ->dropColumn (Node::LFT , Node::RGT , Node::PARENT_ID );
32+ $ columns = self ::getDefaultColumns ();
33+
34+ $ table ->dropIndex ($ columns );
35+ $ table ->dropColumn ($ columns );
5436 }
5537
5638 /**
57- * Create root node.
58- *
59- * @param string $table
60- * @param array $extra
61- * @param string $connection
62- *
63- * @return boolean
39+ * Get a list of default columns.
40+ *
41+ * @return array
6442 */
65- static function createRoot ( $ table , array $ extra = array (), $ connection = null )
43+ public static function getDefaultColumns ( )
6644 {
67- $ extra = array_merge ($ extra , array (
68- Node::LFT => 1 ,
69- Node::RGT => 2 ,
70- Node::PARENT_ID => NULL ,
71- ));
72-
73- return \DB ::connection ($ connection )->table ($ table )->insert ($ extra );
45+ return [ Node::LFT , Node::RGT , Node::PARENT_ID ];
7446 }
47+
7548}
0 commit comments