I am trying to create pimcore custom plugin .
I gone through its documentation and so far i have created demo plugin and its working fine .
Now i want to create custom table via plugin and needs to store data in the custom table, but not getting idea how to achieve that ? I know in plugin.php -> install method , we need to define database related queries.
But how to write those queries in pimcore way that I am not getting .
I have already try below code :
public static function install()
{
// sql to create table
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
// implement your own logic here
return true;
}
Hope the question make sense.
You're going to want to then actually execute that query on your DB. The Install method is what is called when a user clicks the install button for the plugin in the Extensions panel. Your query looks good, but you need to actually run it now. Try something like this:
$db = \Pimcore\Db::get();
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
$db->query($sql);
return true;
Instead of just returning true, it would be a good idea to check to make sure the table was created. You could try something like this:
$result = $db->describeTable("MyGuests");
if($result){
return true;
}
else{
return false;
}
but there are plenty of other ways of checking to make sure it was successful.
If you only have the red uninstall button, you need to adjust your plugin's isInstalled method. The plugin decides whether to run the install or uninstall method depending on whether or not the plugin is reporting it is installed. If your isInstalled method is returning true, you will have the option to uninstall. If it is returning false, you will have the option to install.
That said, you should check to make sure the table exists in your isInstalled method, and return true or false accordingly.