0
votes

Tags Entity :

<?php

namespace App\Entity;

use App\Entity;
use Doctrine\ORM\Mapping;

/**
 * @Entity
 * @Table(name="tags", options={"collate":"utf8_general_ci", "charset":"utf8", "engine":"MyISAM"})
 */
class Tags extends Entity {

    /**
     * Many Tags have Many HolidayPackages.
     * @ManyToMany(targetEntity="HolidayPackages", mappedBy="tags")
     * @JoinTable(name="holiday_tags",
     *      joinColumns={@JoinColumn(name="tid", referencedColumnName="id")},
     *      inverseJoinColumns={@JoinColumn(name="hpid", referencedColumnName="id")}
     *      )
     */
    protected $holiday_packages;

    /**
     * @Column(type="string", length=255)
     * @var string
     */
    protected $tags;

    /**
     * @return mixed
     */
    public function getHolidayPackages() {
        return $this->holiday_packages;
    }

    /**
     * @param mixed $holiday_packages
     */
    public function setHolidayPackages($holiday_packages) {
        $this->holiday_packages = $holiday_packages;
    }

    /**
     * @return string
     */
    public function getTags() {
        return $this->tags;
    }

    /**
     * @param string $tags
     */
    public function setTags($tags) {
        $this->tags = $tags;
    }
}

Holiday Packages :

<?php

namespace App\Entity;

use App\Entity;
use Doctrine\ORM\Mapping;

/**
 * @Entity
 * @Table(name="holiday_packages", options={"collate":"utf8_general_ci", "charset":"utf8", "engine":"MyISAM"})
 */
class HolidayPackages extends Entity {

    /**
     * Many HolidayPackages have Many Tags.
     * @ManyToMany(targetEntity="Tags", inversedBy="holiday_packages")
     * @JoinTable(name="holiday_tags",
     *      joinColumns={@JoinColumn(name="hpid", referencedColumnName="id")},
     *      inverseJoinColumns={@JoinColumn(name="tid", referencedColumnName="id")}
     *      )
     */
    protected $tags;

    /**
     * @return mixed
     */
    public function getTags() {
        return $this->tags;
    }

    /**
     * @param mixed $tags
     */
    public function setTags($tags) {
        $this->tags = $tags;
    }
}

I am trying to create many-to-many assocation mapping. I follow this link : http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html to make this mapping.

But when i try to update doctrine, error occure :

[Doctrine\DBAL\DBALException]
An exception occurred while executing 'ALTER TABLE holiday_tags DROP PRIMARY KEY':
SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key

[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key

orm:schema-tool:update [--complete] [--dump-sql] [--force]

UPDATE

Entity Class :

<?php

namespace App;

use Doctrine\ORM\Mapping as ORM;

/**
 * @MappedSuperclass
 * @HasLifecycleCallbacks()
 */
abstract class Entity
{
    /**
     * @var integer
     *
     * @Column(name="id", type="integer")
     * @Id
     * @GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @Column(type="datetime")
     * @var \DateTime
     */
    protected $created_at;

    /**
     * @Column(type="datetime", nullable=true)
     * @var \DateTime
     */
    protected $updated_at;

    /**
     * Constructor
     */
    public function __construct() {
        $this->setCreatedAt(new \DateTime());
        $this->setUpdatedAt(new \DateTime());
    }

    /**
     * @PreUpdate
     */
    public function setUpdatedValue() {
        $this->setUpdatedAt(new \DateTime());
    }

    /**
     * Get id
     *
     * @return integer
     */
    public function getId() {
        return $this->id;
    }

    /**
     * @param $created_at
     */
    public function setCreatedAt($created_at) {
        $this->created_at = $created_at;
    }

    /**
     * @return \DateTime
     */
    public function getCreatedAt() {
        return $this->created_at->format('d/m/Y H:i');
    }

    /**
     * @param $updated_at
     */
    public function setUpdatedAt($updated_at) {
        $this->updated_at = $updated_at;
    }

    /**
     * @return \DateTime
     */
    public function getUpdatedAt() {
        return $this->updated_at;
    }
}

It creates a class holidaypackages_tags but still give error and i specified name as holiday_tags but it named holidaypackages_tags...

1
Both fields which has @ManyToMany annotations, needs to be initialized with ArrayCollection in the constructor.Dan Costinel

1 Answers

0
votes

You are trying to create the same table twice "holiday_tags"!

Try this:

class Tags extends Entity {

    /**
     * Many Tags have Many HolidayPackages.
     * @ManyToMany(targetEntity="HolidayPackages", mappedBy="tags")
     */
    protected $holiday_packages;

//...
}


class HolidayPackages extends Entity {

    /**
     * Many HolidayPackages have Many Tags.
     * @ManyToMany(targetEntity="Tags", inversedBy="holiday_packages")
     * @JoinTable(name="holiday_tags",
     *      joinColumns={@JoinColumn(name="hpid", referencedColumnName="id")},
     *      inverseJoinColumns={@JoinColumn(name="tid", referencedColumnName="id")}
     *      )
     */
    protected $tags;
//...
}

Notice that there is no annotation to create the same table again on $holiday_packages field