{"action":"opened","number":4573,"pull_request":{"url":"https://api.github.com/repos/PrestaShop/PrestaShop/pulls/4573","id":53427011,"html_url":"https://github.com/PrestaShop/PrestaShop/pull/4573","diff_url":"https://github.com/PrestaShop/PrestaShop/pull/4573.diff","patch_url":"https://github.com/PrestaShop/PrestaShop/pull/4573.patch","issue_url":"https://api.github.com/repos/PrestaShop/PrestaShop/issues/4573","number":4573,"state":"open","locked":false,"title":"checkout if a field exists","user":{"login":"jonathanparentlevesque","id":15891137,"avatar_url":"https://avatars.githubusercontent.com/u/15891137?v=3","gravatar_id":"","url":"https://api.github.com/users/jonathanparentlevesque","html_url":"https://github.com/jonathanparentlevesque","followers_url":"https://api.github.com/users/jonathanparentlevesque/followers","following_url":"https://api.github.com/users/jonathanparentlevesque/following{/other_user}","gists_url":"https://api.github.com/users/jonathanparentlevesque/gists{/gist_id}","starred_url":"https://api.github.com/users/jonathanparentlevesque/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jonathanparentlevesque/subscriptions","organizations_url":"https://api.github.com/users/jonathanparentlevesque/orgs","repos_url":"https://api.github.com/users/jonathanparentlevesque/repos","events_url":"https://api.github.com/users/jonathanparentlevesque/events{/privacy}","received_events_url":"https://api.github.com/users/jonathanparentlevesque/received_events","type":"User","site_admin":false},"body":"Hello,\r\n\r\nI searched quite a lot, but didn't found any function allowing to checkout if a field already exists in a table.\r\n\r\nBy example, in the install function of my module, I want to add a field in a prestashop table (which will causes an error if it already exists):\r\n\r\n\r\n    if(!$this->isFieldExists(_DB_PREFIX_ . \"category\", \"allow_appointment\")\r\n        and !$success = Db::getInstance()->execute(\r\n            \"ALTER TABLE `\" . _DB_PREFIX_ . \"category`\r\n            ADD `allow_appointment` TINYINT(1) NOT NULL DEFAULT 0\"\r\n        )\r\n    ) {\r\n        $this->_errors[] = $this->l(\r\n            'An error occured while altering the category table.'\r\n        );\r\n    }\r\n\r\nYou could add a function similar to this one in the `dbCore` class:\r\n\r\n     /**\r\n     * Determine if a field exist in a table (of the active database) or not.\r\n     *\r\n     * @param $tableName String\r\n     * @param $fieldName String\r\n     *\r\n     * @return Boolean\r\n     */\r\n     private function isFieldExists($tableName, $fieldName)\r\n     {\r\n          $exists = false;\r\n          \r\n          if ($result = Db::getInstance()->executeS(\r\n                    \"(SELECT DATABASE() as db_name)\"\r\n               ) and !empty(Db::getInstance()->executeS(\r\n                    \"SELECT NULL\r\n                    FROM INFORMATION_SCHEMA.COLUMNS\r\n                    WHERE table_name = '\" . pSQL($tableName) . \"'\r\n                    AND table_schema = '\" . pSQL($result[0]['db_name']) . \"'\r\n                    AND column_name = '\" . pSQL($fieldName) . \"'\"\r\n               ))\r\n          ) {\r\n               $exists = true;     \r\n          }\r\n          \r\n          return $exists;\r\n     }\r\n\r\n**Note**: I would had done it in a single SQL query, but `executeS()` Do not allow query like theses\r\n\r\n    SET @name = (SELECT DATABASE());\r\n    \r\n    SELECT NULL\r\n    FROM INFORMATION_SCHEMA.COLUMNS\r\n    WHERE table_name = 'ps_category'\r\n    AND table_schema = @name\r\n    AND column_name = 'allow_appointment'\r\n\r\nand that query do not work (as it considers `INFORMATION_SCHEMA` as the active database):\r\n\r\n    SELECT NULL\r\n    FROM INFORMATION_SCHEMA.COLUMNS\r\n    WHERE table_name = 'ps_category'\r\n    AND table_schema = (SELECT DATABASE())\r\n    AND column_name = 'allow_appointment\r\n\r\n\r\nOne of my main issue being that I didn't found anyway to know what is the name of active table during runtime unless I read the configuration file.\r\n\r\n\r\nThank you,\r\nJonathan Parent-Lévesque from Montreal","created_at":"2015-12-11T18:29:08Z","updated_at":"2015-12-11T18:29:08Z","closed_at":null,"merged_at":null,"merge_commit_sha":null,"assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/PrestaShop/PrestaShop/pulls/4573/commits","review_comments_url":"https://api.github.com/repos/PrestaShop/PrestaShop/pulls/4573/comments","review_comment_url":"https://api.github.com/repos/PrestaShop/PrestaShop/pulls/comments{/number}","comments_url":"https://api.github.com/repos/PrestaShop/PrestaShop/issues/4573/comments","statuses_url":"https://api.github.com/repos/PrestaShop/PrestaShop/statuses/48be420a1696c4ff5ab8e934705d351e3e9ef0db","head":{"label":"PrestaShop:develop","ref":"develop","sha":"48be420a1696c4ff5ab8e934705d351e3e9ef0db","user":{"login":"PrestaShop","id":2815696,"avatar_url":"https://avatars.githubusercontent.com/u/2815696?v=3","gravatar_id":"","url":"https://api.github.com/users/PrestaShop","html_url":"https://github.com/PrestaShop","followers_url":"https://api.github.com/users/PrestaShop/followers","following_url":"https://api.github.com/users/PrestaShop/following{/other_user}","gists_url":"https://api.github.com/users/PrestaShop/gists{/gist_id}","starred_url":"https://api.github.com/users/PrestaShop/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PrestaShop/subscriptions","organizations_url":"https://api.github.com/users/PrestaShop/orgs","repos_url":"https://api.github.com/users/PrestaShop/repos","events_url":"https://api.github.com/users/PrestaShop/events{/privacy}","received_events_url":"https://api.github.com/users/PrestaShop/received_events","type":"Organization","site_admin":false},"repo":{"id":6763587,"name":"PrestaShop","full_name":"PrestaShop/PrestaShop","owner":{"login":"PrestaShop","id":2815696,"avatar_url":"https://avatars.githubusercontent.com/u/2815696?v=3","gravatar_id":"","url":"https://api.github.com/users/PrestaShop","html_url":"https://github.com/PrestaShop","followers_url":"https://api.github.com/users/PrestaShop/followers","following_url":"https://api.github.com/users/PrestaShop/following{/other_user}","gists_url":"https://api.github.com/users/PrestaShop/gists{/gist_id}","starred_url":"https://api.github.com/users/PrestaShop/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PrestaShop/subscriptions","organizations_url":"https://api.github.com/users/PrestaShop/orgs","repos_url":"https://api.github.com/users/PrestaShop/repos","events_url":"https://api.github.com/users/PrestaShop/events{/privacy}","received_events_url":"https://api.github.com/users/PrestaShop/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/PrestaShop/PrestaShop","description":"PrestaShop offers a free, fully scalable, Open-source e-commerce solution.","fork":false,"url":"https://api.github.com/repos/PrestaShop/PrestaShop","forks_url":"https://api.github.com/repos/PrestaShop/PrestaShop/forks","keys_url":"https://api.github.com/repos/PrestaShop/PrestaShop/keys{/key_id}","collaborators_url":"https://api.github.com/repos/PrestaShop/PrestaShop/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/PrestaShop/PrestaShop/teams","hooks_url":"https://api.github.com/repos/PrestaShop/PrestaShop/hooks","issue_events_url":"https://api.github.com/repos/PrestaShop/PrestaShop/issues/events{/number}","events_url":"https://api.github.com/repos/PrestaShop/PrestaShop/events","assignees_url":"https://api.github.com/repos/PrestaShop/PrestaShop/assignees{/user}","branches_url":"https://api.github.com/repos/PrestaShop/PrestaShop/branches{/branch}","tags_url":"https://api.github.com/repos/PrestaShop/PrestaShop/tags","blobs_url":"https://api.github.com/repos/PrestaShop/PrestaShop/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/PrestaShop/PrestaShop/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/PrestaShop/PrestaShop/git/refs{/sha}","trees_url":"https://api.github.com/repos/PrestaShop/PrestaShop/git/trees{/sha}","statuses_url":"https://api.github.com/repos/PrestaShop/PrestaShop/statuses/{sha}","languages_url":"https://api.github.com/repos/PrestaShop/PrestaShop/languages","stargazers_url":"https://api.github.com/repos/PrestaShop/PrestaShop/stargazers","contributors_url":"https://api.github.com/repos/PrestaShop/PrestaShop/contributors","subscribers_url":"https://api.github.com/repos/PrestaShop/PrestaShop/subscribers","subscription_url":"https://api.github.com/repos/PrestaShop/PrestaShop/subscription","commits_url":"https://api.github.com/repos/PrestaShop/PrestaShop/commits{/sha}","git_commits_url":"https://api.github.com/repos/PrestaShop/PrestaShop/git/commits{/sha}","comments_url":"https://api.github.com/repos/PrestaShop/PrestaShop/comments{/number}","issue_comment_url":"https://api.github.com/repos/PrestaShop/PrestaShop/issues/comments{/number}","contents_url":"https://api.github.com/repos/PrestaShop/PrestaShop/contents/{+path}","compare_url":"https://api.github.com/repos/PrestaShop/PrestaShop/compare/{base}...{head}","merges_url":"https://api.github.com/repos/PrestaShop/PrestaShop/merges","archive_url":"https://api.github.com/repos/PrestaShop/PrestaShop/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/PrestaShop/PrestaShop/downloads","issues_url":"https://api.github.com/repos/PrestaShop/PrestaShop/issues{/number}","pulls_url":"https://api.github.com/repos/PrestaShop/PrestaShop/pulls{/number}","milestones_url":"https://api.github.com/repos/PrestaShop/PrestaShop/milestones{/number}","notifications_url":"https://api.github.com/repos/PrestaShop/PrestaShop/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/PrestaShop/PrestaShop/labels{/name}","releases_url":"https://api.github.com/repos/PrestaShop/PrestaShop/releases{/id}","created_at":"2012-11-19T16:41:31Z","updated_at":"2015-12-11T15:12:38Z","pushed_at":"2015-12-11T18:29:09Z","git_url":"git://github.com/PrestaShop/PrestaShop.git","ssh_url":"git@github.com:PrestaShop/PrestaShop.git","clone_url":"https://github.com/PrestaShop/PrestaShop.git","svn_url":"https://github.com/PrestaShop/PrestaShop","homepage":"http://www.prestashop.com/","size":241614,"stargazers_count":1537,"watchers_count":1537,"language":"PHP","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":1699,"mirror_url":null,"open_issues_count":397,"forks":1699,"open_issues":397,"watchers":1537,"default_branch":"develop"}},"base":{"label":"PrestaShop:1.6.1.x","ref":"1.6.1.x","sha":"61693f00812a74a4f0c257a02767b4441eb2f04b","user":{"login":"PrestaShop","id":2815696,"avatar_url":"https://avatars.githubusercontent.com/u/2815696?v=3","gravatar_id":"","url":"https://api.github.com/users/PrestaShop","html_url":"https://github.com/PrestaShop","followers_url":"https://api.github.com/users/PrestaShop/followers","following_url":"https://api.github.com/users/PrestaShop/following{/other_user}","gists_url":"https://api.github.com/users/PrestaShop/gists{/gist_id}","starred_url":"https://api.github.com/users/PrestaShop/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PrestaShop/subscriptions","organizations_url":"https://api.github.com/users/PrestaShop/orgs","repos_url":"https://api.github.com/users/PrestaShop/repos","events_url":"https://api.github.com/users/PrestaShop/events{/privacy}","received_events_url":"https://api.github.com/users/PrestaShop/received_events","type":"Organization","site_admin":false},"repo":{"id":6763587,"name":"PrestaShop","full_name":"PrestaShop/PrestaShop","owner":{"login":"PrestaShop","id":2815696,"avatar_url":"https://avatars.githubusercontent.com/u/2815696?v=3","gravatar_id":"","url":"https://api.github.com/users/PrestaShop","html_url":"https://github.com/PrestaShop","followers_url":"https://api.github.com/users/PrestaShop/followers","following_url":"https://api.github.com/users/PrestaShop/following{/other_user}","gists_url":"https://api.github.com/users/PrestaShop/gists{/gist_id}","starred_url":"https://api.github.com/users/PrestaShop/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PrestaShop/subscriptions","organizations_url":"https://api.github.com/users/PrestaShop/orgs","repos_url":"https://api.github.com/users/PrestaShop/repos","events_url":"https://api.github.com/users/PrestaShop/events{/privacy}","received_events_url":"https://api.github.com/users/PrestaShop/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/PrestaShop/PrestaShop","description":"PrestaShop offers a free, fully scalable, Open-source e-commerce solution.","fork":false,"url":"https://api.github.com/repos/PrestaShop/PrestaShop","forks_url":"https://api.github.com/repos/PrestaShop/PrestaShop/forks","keys_url":"https://api.github.com/repos/PrestaShop/PrestaShop/keys{/key_id}","collaborators_url":"https://api.github.com/repos/PrestaShop/PrestaShop/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/PrestaShop/PrestaShop/teams","hooks_url":"https://api.github.com/repos/PrestaShop/PrestaShop/hooks","issue_events_url":"https://api.github.com/repos/PrestaShop/PrestaShop/issues/events{/number}","events_url":"https://api.github.com/repos/PrestaShop/PrestaShop/events","assignees_url":"https://api.github.com/repos/PrestaShop/PrestaShop/assignees{/user}","branches_url":"https://api.github.com/repos/PrestaShop/PrestaShop/branches{/branch}","tags_url":"https://api.github.com/repos/PrestaShop/PrestaShop/tags","blobs_url":"https://api.github.com/repos/PrestaShop/PrestaShop/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/PrestaShop/PrestaShop/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/PrestaShop/PrestaShop/git/refs{/sha}","trees_url":"https://api.github.com/repos/PrestaShop/PrestaShop/git/trees{/sha}","statuses_url":"https://api.github.com/repos/PrestaShop/PrestaShop/statuses/{sha}","languages_url":"https://api.github.com/repos/PrestaShop/PrestaShop/languages","stargazers_url":"https://api.github.com/repos/PrestaShop/PrestaShop/stargazers","contributors_url":"https://api.github.com/repos/PrestaShop/PrestaShop/contributors","subscribers_url":"https://api.github.com/repos/PrestaShop/PrestaShop/subscribers","subscription_url":"https://api.github.com/repos/PrestaShop/PrestaShop/subscription","commits_url":"https://api.github.com/repos/PrestaShop/PrestaShop/commits{/sha}","git_commits_url":"https://api.github.com/repos/PrestaShop/PrestaShop/git/commits{/sha}","comments_url":"https://api.github.com/repos/PrestaShop/PrestaShop/comments{/number}","issue_comment_url":"https://api.github.com/repos/PrestaShop/PrestaShop/issues/comments{/number}","contents_url":"https://api.github.com/repos/PrestaShop/PrestaShop/contents/{+path}","compare_url":"https://api.github.com/repos/PrestaShop/PrestaShop/compare/{base}...{head}","merges_url":"https://api.github.com/repos/PrestaShop/PrestaShop/merges","archive_url":"https://api.github.com/repos/PrestaShop/PrestaShop/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/PrestaShop/PrestaShop/downloads","issues_url":"https://api.github.com/repos/PrestaShop/PrestaShop/issues{/number}","pulls_url":"https://api.github.com/repos/PrestaShop/PrestaShop/pulls{/number}","milestones_url":"https://api.github.com/repos/PrestaShop/PrestaShop/milestones{/number}","notifications_url":"https://api.github.com/repos/PrestaShop/PrestaShop/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/PrestaShop/PrestaShop/labels{/name}","releases_url":"https://api.github.com/repos/PrestaShop/PrestaShop/releases{/id}","created_at":"2012-11-19T16:41:31Z","updated_at":"2015-12-11T15:12:38Z","pushed_at":"2015-12-11T18:29:09Z","git_url":"git://github.com/PrestaShop/PrestaShop.git","ssh_url":"git@github.com:PrestaShop/PrestaShop.git","clone_url":"https://github.com/PrestaShop/PrestaShop.git","svn_url":"https://github.com/PrestaShop/PrestaShop","homepage":"http://www.prestashop.com/","size":241614,"stargazers_count":1537,"watchers_count":1537,"language":"PHP","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":1699,"mirror_url":null,"open_issues_count":397,"forks":1699,"open_issues":397,"watchers":1537,"default_branch":"develop"}},"_links":{"self":{"href":"https://api.github.com/repos/PrestaShop/PrestaShop/pulls/4573"},"html":{"href":"https://github.com/PrestaShop/PrestaShop/pull/4573"},"issue":{"href":"https://api.github.com/repos/PrestaShop/PrestaShop/issues/4573"},"comments":{"href":"https://api.github.com/repos/PrestaShop/PrestaShop/issues/4573/comments"},"review_comments":{"href":"https://api.github.com/repos/PrestaShop/PrestaShop/pulls/4573/comments"},"review_comment":{"href":"https://api.github.com/repos/PrestaShop/PrestaShop/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/PrestaShop/PrestaShop/pulls/4573/commits"},"statuses":{"href":"https://api.github.com/repos/PrestaShop/PrestaShop/statuses/48be420a1696c4ff5ab8e934705d351e3e9ef0db"}},"merged":false,"mergeable":false,"mergeable_state":"dirty","merged_by":null,"comments":0,"review_comments":0,"commits":1464,"additions":6435,"deletions":1761,"changed_files":301}}