Giter Site home page Giter Site logo

closure-table-manager's People

Contributors

nineinchnick avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

closure-table-manager's Issues

Notice for mysql when trying to set path

Notice: Undefined variable: parent in [...]/vendor/nineinchnick/closure-table-manager/src/closureTable/mysql/Schema.php on line 104

should probably be $parentKey instead of $parent ?

Info:

Base table:

CREATE TABLE `page` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `parent_id` bigint(20) NOT NULL,
  `title` varchar(255) NOT NULL COMMENT 'used for slugs',
  `slug` varchar(255) CHARACTER SET ascii COLLATE ascii_bin NOT NULL COMMENT 'generated from title',
  `path` text NOT NULL COMMENT 'generated by triggers',
  `position` int(11) NOT NULL COMMENT 'for sorting',
  PRIMARY KEY (`id`),
  KEY `parent_id` (`parent_id`),
  CONSTRAINT `page_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `page` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8
ini_set('display_errors', 1);
error_reporting(-1);
$manager = new \nineinchnick\closureTable\Manager();

$queries = $manager->getQueries(
    $dsn = 'mysql',
    $tableName = 'page',
    $parentKey = 'parent_id',
    $primaryKey = 'id',
    $primaryKeyType = 'BIGINT',
    $path = 'path',
    $pathFrom = 'slug',
    $pathSeparator = '/',
    $tableNameSuffix = '_tree',
    $schemaName = null
);

The trigger that (not tested against db, just assuming) will fail:

DELIMITER $$
CREATE TRIGGER `page_tree_bi_path` BEFORE INSERT ON `page`
FOR EACH ROW BEGIN
  # WHY??
  IF NEW."" IS NULL THEN
    SET NEW."path" = NEW."slug";
  ELSE
    SELECT "path" || '/' || NEW."slug" INTO NEW."path"
    FROM "page"
    WHERE "id" = NEW."parent_id";
  END IF;
END;$$
DELIMITER ;

When using sqlite, I think this is the corresponding query:

CREATE TRIGGER "page_tree_ai_path_1" AFTER INSERT ON "page"
FOR EACH ROW WHEN NEW."parent_id" IS NULL
BEGIN
  UPDATE "page" SET "path" = "slug" WHERE "id" = NEW."id";
END;

and pgsql:

-- Generate path urls based on slug and position in the tree.

CREATE OR REPLACE FUNCTION "page_tree_bi_path"() RETURNS TRIGGER AS
$BODY$
BEGIN
  IF NEW."parent_id" IS NULL THEN
    NEW."path" := NEW."slug";
  ELSE
    SELECT "path" || '/' || NEW."slug" INTO NEW."path"
    FROM "page"
    WHERE "id" = NEW."parent_id";
  END IF;
  RETURN NEW;
END;
$BODY$
LANGUAGE 'plpgsql'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.