I am using doctrine 2 in zend framework 2. Below is my entity file. The problem is, when I tried to validate schema using,
./vendor/bin/doctrine-module orm:validate-schema
command.
I am getting error,
[Doctrine\DBAL\Schema\SchemaException]
The table with name 'database.opportunitycriteria' already exists.
What should I do?
namespace Administration\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* OpportunityCriteria
*
* @ORM\Table(name="OpportunityCriteria")
* @ORM\Entity
*/
class Criteria
{
/**
* @var integer
* @ORM\Id
* @ORM\Column(name="criteria_id", type="integer", nullable=false)
*/
private $criteria_id;
/**
* @var string
*
* @ORM\Column(name="description", type="string", nullable=false)
*/
private $description;
}
and appropriate getter and setter methods..
Using the OBJECT_ID and the IF ELSE statement to check whether a table exists or not. Alternative 2 : Using the INFORMATION_SCHEMA. TABLES and SQL EXISTS Operator to check whether a table exists or not.
SELECT TABLE_NAME FROM INFORMATION_SCHEMA. TABLES WHERE TABLE_SCHEMA = 'yourDatabaseName' AND TABLE_NAME = 'yourTableName'; Let us implement the above syntax in order to check if a table already exists in the database.
I finally figured it out. OP's use case may be different, but in my case, this was because of a misconfigured bidirectional many-to-many relationship.
I had the following entities:
class Cuisine {
/**
* @ManyToMany(targetEntity="Dish")
* @ORM\JoinTable(name="CuisineDish", ...)
*/
protected $dishes;
}
class Dish {
/**
* @ORM\ManyToMany(targetEntity="Cuisine")
* @ORM\JoinTable(name="CuisineDish", ...)
*/
protected $cuisines;
}
What was missing was the inversedBy
and mappedBy
properties of the @ManyToMany
annotations. These are only required when the association is bi-directional.
So now the correctly mapped entities look like:
class Cuisine {
/**
* @ManyToMany(targetEntity="Dish", inversedBy="cuisines")
* @ORM\JoinTable(name="CuisineDish", )
*/
protected $dishes;
}
class Dish {
/**
* @ORM\ManyToMany(targetEntity="Cuisine", mappedBy="dishes")
* @ORM\JoinTable(name="CuisineDish", ...)
*/
protected $cuisines;
}
And orm:validate-schema
does not exit with an exception any more.
The exception message is just misleading, as the database is not altered by this operation. Furthermore, this issue is only spotted when validating the sync with the database, not when validating the mapping only (--skip-sync
), where it should.
I just reported this bug.
it can cause this error message if you want to use a table name which is already used by one of the installed bundles.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With