8
votes

I want to update MySQL field from text to longtext using Doctrine schema.

Now my code is like this:

/**
 *@var string
 *@ORM\Column(name="head_fa", type="string", length=1000, nullable=true)
 */
private $head_fa;

/**
 *@var string
 *@ORM\Column(name="head_en", type="string", length=1000, nullable=true)
 */
private $head_en;

/**
 *@var string
 *@ORM\Column(name="body_fa", type="text", length=1000, nullable=true)
 */
private $body_fa;

/**
 *@var string
 *@ORM\Column(name="body_en", type="text", length=1000, nullable=true)
 */
private $body_en;

and the problem is when i change this field to this code

/**
 *@var string
 *@ORM\Column(name="head_fa", type="string", length=1000, nullable=true)
 */
private $head_fa;

/**
 *@var string
 *@ORM\Column(name="head_en", type="string", length=1000, nullable=true)
 */
private $head_en;

/**
 *@var string
 *@ORM\Column(name="body_fa", type="text", nullable=true)
 */
private $body_fa;

/**
 *@var string
 *@ORM\Column(name="body_en", type="text", nullable=true)
 */
private $body_en;

and run "php app/console doctrine:schema:update --force" command on console it said that "Nothing to update - your database is already in sync with the current entity metadata." How to change this field to longtext on mysql database.

I do the same on different part of the project. this is the code

/**
 * @ORM\Column(name="body", type="text", nullable=true)
 */
protected $body;

and after executing the "php app/console doctrine:schema:update --force" command on terminal this field is changed to longtext on MySQL database.

2

2 Answers

12
votes

I ran into this same issue. I found a solution after referencing this page: http://doctrine-dbal.readthedocs.org/en/latest/reference/types.html

Specifying the length of the text field will have the correct type created in MySQL. For example: length=65535

See here: http://doctrine-dbal.readthedocs.org/en/latest/reference/types.html#id100

A length between 256 and 65535 to use a "text" field in the database.

0
votes

for symfony 4.2 on MySql DB with Doctrine Entity field (with validation) declaration works

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

your class name

    /**
 * @var string
 * @ORM\Column(type="text", length=65535)
 * @Assert\NotBlank(message="your message")
 * @Assert\Length(
 *     max=65535,
 *     maxMessage="your message"
 * )
 */
protected $docContent;

if you need more space for text (using CKeditor as text input need more characters for html tags than only text) you can create attachment class and link by relation many to one, and spread large content into separate paragraphs.