Doctrine2 / MySQL - creating an auto-increment field on multiple columns

I wanted to know if it is possible to have an auto-increment field based on multiple columns?

Here is my problem: I have a table that stores CARTS in rows. I have a unique identifier for each basket, but I also want to create a unique identifier for each row. So that I can have a unique CART identifier inside the string.

Actually, I want to do exactly the same as in this example from a MySQL document:

http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

+--------+----+---------+
| grp    | id | name    |
+--------+----+---------+
| fish   |  1 | lax     |
| mammal |  1 | dog     |
| mammal |  2 | cat     |
| mammal |  3 | whale   |
| bird   |  1 | penguin |
| bird   |  2 | ostrich |
+--------+----+---------+

The identifier is incremented in the GRP column.

I'm a bit lost with the primary key / index, and I don't know if I need to use them to generate my identifiers.

Symfony, .

 * @ORM\Table(indexes={@ORM\Index(name="line_idx", columns={"line_id"})})
+4
2

, , , , , :

cart_id:

/**
 * @var integer
 *
 * @ORM\Column(name="cart_id", type="integer", nullable=false)
*/
private $cart_id;

, , :

 * @ORM\Table(uniqueConstraints={@UniqueConstraint(name="cart_idx", columns={"line_id", "cart_id"})})

, cart_id ( ManyToOne:

<?php

namespace CM\PlatformBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * CartRepository
 */
class CartRepository extends EntityRepository
{
    public function getLastCartIdForLine($lineId) {
            return $this->createQueryBuilder('c')
                        ->select('c.cart_id')
                        ->join('c.line','l')
                        ->andWhere('l.id = :lineId')
                        ->setParameter('lineId', $lineId)
                        ->orderBy('c.id', 'DESC')
                        ->setMaxResults(1)
                        ->getQuery()
                        ->getSingleScalarResult();
    }       
}

, , :

$cart = new Cart;
$lastCartId = $em->getRepository('CMPlatformBundle:Cart')->getLastCartIdForLine($lineId);
$cart->setCartId($lastCartId+1);
$em->persist($cart);

, , . , - .

+1

, , , , :

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Class Animal
 * @package AppBundle\Entity
 *
 * @ORM\Table(name="animals")
 * @ORM\Entity
 */
class Animal
{
    /**
     * @var integer
     *
     * @ORM\Id
     * @ORM\Column(name="grp", type="string", length=255, nullable=false)
     */
    private $grp;

    /**
     * @var integer
     *
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     * @ORM\Column(name="id", type="integer", nullable=false)
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255, nullable=false)
     */
    private $name;
}

id grp @Id, . id @GeneratedValue, .

, MyISAM, , , InnoDB.

. @GeneratedValue @Id, .

0

Source: https://habr.com/ru/post/1599299/


All Articles